@@ -8,30 +8,30 @@ import (
8
8
"sync"
9
9
"time"
10
10
11
- "github.com/aws/aws-sdk-go/aws"
12
- "github.com/aws/aws-sdk-go/aws/credentials"
13
- "github.com/aws/aws-sdk-go/aws/session"
14
- "github.com/aws/aws-sdk-go/service/s3"
11
+ "github.com/aws/aws-sdk-go-v2/aws"
12
+ "github.com/aws/aws-sdk-go-v2/config"
13
+ "github.com/aws/aws-sdk-go-v2/credentials"
14
+ "github.com/aws/aws-sdk-go-v2/service/s3"
15
+ "github.com/aws/aws-sdk-go-v2/service/s3/types"
15
16
)
16
17
17
18
// Service is simple abstraction layer to work with a S3-compatible storage service
18
19
type Service struct {
19
- Client * s3.S3
20
+ Client * s3.Client
20
21
urler ObjectURLer
21
22
}
22
23
23
24
// NewService creates a new S3 service with the given credentials and configuration
24
25
func NewService (accessKey , secretKey , region , endpoint string , minio bool ) (* Service , error ) {
25
- cfg := & aws.Config {
26
- Region : aws .String (region ),
27
- Endpoint : aws .String (endpoint ),
28
- S3ForcePathStyle : aws .Bool (minio ), // urls as endpoint/bucket/key instead of bucket.endpoint/key
29
- MaxRetries : aws .Int (3 ),
30
- }
31
- if accessKey != "" || secretKey != "" {
32
- cfg .Credentials = credentials .NewStaticCredentials (accessKey , secretKey , "" )
26
+ opts := []func (* config.LoadOptions ) error {config .WithRegion (region )}
27
+
28
+ if accessKey != "" && secretKey != "" {
29
+ opts = append (opts , config .WithCredentialsProvider (credentials.StaticCredentialsProvider {Value : aws.Credentials {
30
+ AccessKeyID : accessKey , SecretAccessKey : secretKey ,
31
+ }}))
33
32
}
34
- s , err := session .NewSession (cfg )
33
+
34
+ cfg , err := config .LoadDefaultConfig (context .TODO (), opts ... )
35
35
if err != nil {
36
36
return nil , err
37
37
}
@@ -43,7 +43,15 @@ func NewService(accessKey, secretKey, region, endpoint string, minio bool) (*Ser
43
43
urler = AWSURLer (region )
44
44
}
45
45
46
- return & Service {Client : s3 .New (s ), urler : urler }, nil
46
+ client := s3 .NewFromConfig (cfg , func (o * s3.Options ) {
47
+ o .UsePathStyle = minio // urls as endpoint/bucket/key instead of bucket.endpoint/key
48
+
49
+ if endpoint != "" {
50
+ o .BaseEndpoint = aws .String (endpoint )
51
+ }
52
+ })
53
+
54
+ return & Service {Client : client , urler : urler }, nil
47
55
}
48
56
49
57
// ObjectURL returns the publicly accessible URL for the given object
@@ -53,13 +61,13 @@ func (s *Service) ObjectURL(bucket, key string) string {
53
61
54
62
// Test is a convenience method to HEAD a bucket to test if it exists and we can access it
55
63
func (s * Service ) Test (ctx context.Context , bucket string ) error {
56
- _ , err := s .Client .HeadBucket (& s3.HeadBucketInput {Bucket : aws .String (bucket )})
64
+ _ , err := s .Client .HeadBucket (ctx , & s3.HeadBucketInput {Bucket : aws .String (bucket )})
57
65
return err
58
66
}
59
67
60
68
// GetObject is a convenience method to get an object from S3 and read its contents into a byte slice
61
69
func (s * Service ) GetObject (ctx context.Context , bucket , key string ) (string , []byte , error ) {
62
- out , err := s .Client .GetObjectWithContext (ctx , & s3.GetObjectInput {
70
+ out , err := s .Client .GetObject (ctx , & s3.GetObjectInput {
63
71
Bucket : aws .String (bucket ),
64
72
Key : aws .String (key ),
65
73
})
@@ -72,17 +80,17 @@ func (s *Service) GetObject(ctx context.Context, bucket, key string) (string, []
72
80
return "" , nil , fmt .Errorf ("error reading S3 object: %w" , err )
73
81
}
74
82
75
- return aws .StringValue (out .ContentType ), body , nil
83
+ return aws .ToString (out .ContentType ), body , nil
76
84
}
77
85
78
86
// PutObject is a convenience method to put the given object and return its publicly accessible URL
79
- func (s * Service ) PutObject (ctx context.Context , bucket , key string , contentType string , body []byte , acl string ) (string , error ) {
80
- _ , err := s .Client .PutObjectWithContext (ctx , & s3.PutObjectInput {
87
+ func (s * Service ) PutObject (ctx context.Context , bucket , key string , contentType string , body []byte , acl types. ObjectCannedACL ) (string , error ) {
88
+ _ , err := s .Client .PutObject (ctx , & s3.PutObjectInput {
81
89
Bucket : aws .String (bucket ),
82
90
Body : bytes .NewReader (body ),
83
91
Key : aws .String (key ),
84
92
ContentType : aws .String (contentType ),
85
- ACL : aws . String ( acl ) ,
93
+ ACL : acl ,
86
94
})
87
95
if err != nil {
88
96
return "" , fmt .Errorf ("error putting S3 object: %w" , err )
@@ -96,7 +104,7 @@ type Upload struct {
96
104
Key string
97
105
ContentType string
98
106
Body []byte
99
- ACL string
107
+ ACL types. ObjectCannedACL
100
108
101
109
// set by BatchPut
102
110
URL string
@@ -154,12 +162,12 @@ func (s *Service) batchWorker(ctx context.Context, uploads chan *Upload, errors
154
162
uctx , cancel := context .WithTimeout (ctx , time .Second * 15 )
155
163
defer cancel ()
156
164
157
- _ , err = s .Client .PutObjectWithContext (uctx , & s3.PutObjectInput {
165
+ _ , err = s .Client .PutObject (uctx , & s3.PutObjectInput {
158
166
Bucket : aws .String (u .Bucket ),
159
167
Body : bytes .NewReader (u .Body ),
160
168
Key : aws .String (u .Key ),
161
169
ContentType : aws .String (u .ContentType ),
162
- ACL : aws . String ( u .ACL ) ,
170
+ ACL : u .ACL ,
163
171
})
164
172
165
173
if err == nil {
@@ -186,27 +194,27 @@ func (s *Service) EmptyBucket(ctx context.Context, bucket string) error {
186
194
request := & s3.ListObjectsV2Input {Bucket : aws .String (bucket )}
187
195
188
196
for {
189
- response , err := s .Client .ListObjectsV2WithContext (ctx , request )
197
+ response , err := s .Client .ListObjectsV2 (ctx , request )
190
198
if err != nil {
191
199
return fmt .Errorf ("error listing S3 objects: %w" , err )
192
200
}
193
201
194
202
if len (response .Contents ) > 0 {
195
- del := & s3 .Delete {}
203
+ del := & types .Delete {}
196
204
197
205
for _ , obj := range response .Contents {
198
- del .Objects = append (del .Objects , & s3 .ObjectIdentifier {Key : obj .Key })
206
+ del .Objects = append (del .Objects , types .ObjectIdentifier {Key : obj .Key })
199
207
}
200
208
201
- _ , err = s .Client .DeleteObjectsWithContext (ctx , & s3.DeleteObjectsInput {Bucket : aws .String (bucket ), Delete : del })
209
+ _ , err = s .Client .DeleteObjects (ctx , & s3.DeleteObjectsInput {Bucket : aws .String (bucket ), Delete : del })
202
210
if err != nil {
203
211
return fmt .Errorf ("error deleting S3 objects: %w" , err )
204
212
}
205
213
}
206
214
207
215
request .ContinuationToken = response .NextContinuationToken
208
216
209
- if ! aws .BoolValue (response .IsTruncated ) {
217
+ if ! aws .ToBool (response .IsTruncated ) {
210
218
break
211
219
}
212
220
}
0 commit comments