@@ -33,6 +33,7 @@ import (
33
33
34
34
const (
35
35
s3URLKey = "s3Url"
36
+ publicURLKey = "publicUrl"
36
37
kmsKeyIDKey = "kmsKeyId"
37
38
s3ForcePathStyleKey = "s3ForcePathStyle"
38
39
bucketKey = "bucket"
@@ -41,6 +42,7 @@ const (
41
42
type objectStore struct {
42
43
log logrus.FieldLogger
43
44
s3 * s3.S3
45
+ preSignS3 * s3.S3
44
46
s3Uploader * s3manager.Uploader
45
47
kmsKeyID string
46
48
}
@@ -53,6 +55,7 @@ func (o *objectStore) Init(config map[string]string) error {
53
55
var (
54
56
region = config [regionKey ]
55
57
s3URL = config [s3URLKey ]
58
+ publicURL = config [publicURLKey ]
56
59
kmsKeyID = config [kmsKeyIDKey ]
57
60
s3ForcePathStyleVal = config [s3ForcePathStyleKey ]
58
61
@@ -82,20 +85,52 @@ func (o *objectStore) Init(config map[string]string) error {
82
85
}
83
86
}
84
87
88
+ serverConfig , err := newAWSConfig (s3URL , region , s3ForcePathStyle )
89
+ if err != nil {
90
+ return err
91
+ }
92
+
93
+ serverSession , err := getSession (serverConfig )
94
+ if err != nil {
95
+ return err
96
+ }
97
+
98
+ o .s3 = s3 .New (serverSession )
99
+ o .s3Uploader = s3manager .NewUploader (serverSession )
100
+ o .kmsKeyID = kmsKeyID
101
+
102
+ if publicURL != "" {
103
+ publicConfig , err := newAWSConfig (publicURL , region , s3ForcePathStyle )
104
+ if err != nil {
105
+ return err
106
+ }
107
+ publicSession , err := getSession (publicConfig )
108
+ if err != nil {
109
+ return err
110
+ }
111
+ o .preSignS3 = s3 .New (publicSession )
112
+ } else {
113
+ o .preSignS3 = o .s3
114
+ }
115
+
116
+ return nil
117
+ }
118
+
119
+ func newAWSConfig (url , region string , forcePathStyle bool ) (* aws.Config , error ) {
85
120
awsConfig := aws .NewConfig ().
86
121
WithRegion (region ).
87
- WithS3ForcePathStyle (s3ForcePathStyle )
122
+ WithS3ForcePathStyle (forcePathStyle )
88
123
89
- if s3URL != "" {
90
- if ! IsValidS3URLScheme (s3URL ) {
91
- return errors .Errorf ("Invalid s3Url : %s" , s3URL )
124
+ if url != "" {
125
+ if ! IsValidS3URLScheme (url ) {
126
+ return nil , errors .Errorf ("Invalid s3 url : %s" , url )
92
127
}
93
128
94
129
awsConfig = awsConfig .WithEndpointResolver (
95
130
endpoints .ResolverFunc (func (service , region string , optFns ... func (* endpoints.Options )) (endpoints.ResolvedEndpoint , error ) {
96
131
if service == endpoints .S3ServiceID {
97
132
return endpoints.ResolvedEndpoint {
98
- URL : s3URL ,
133
+ URL : url ,
99
134
}, nil
100
135
}
101
136
@@ -104,16 +139,7 @@ func (o *objectStore) Init(config map[string]string) error {
104
139
)
105
140
}
106
141
107
- sess , err := getSession (awsConfig )
108
- if err != nil {
109
- return err
110
- }
111
-
112
- o .s3 = s3 .New (sess )
113
- o .s3Uploader = s3manager .NewUploader (sess )
114
- o .kmsKeyID = kmsKeyID
115
-
116
- return nil
142
+ return awsConfig , nil
117
143
}
118
144
119
145
func (o * objectStore ) PutObject (bucket , key string , body io.Reader ) error {
@@ -202,7 +228,7 @@ func (o *objectStore) DeleteObject(bucket, key string) error {
202
228
}
203
229
204
230
func (o * objectStore ) CreateSignedURL (bucket , key string , ttl time.Duration ) (string , error ) {
205
- req , _ := o .s3 .GetObjectRequest (& s3.GetObjectInput {
231
+ req , _ := o .preSignS3 .GetObjectRequest (& s3.GetObjectInput {
206
232
Bucket : aws .String (bucket ),
207
233
Key : aws .String (key ),
208
234
})
0 commit comments