-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
363 lines (304 loc) 路 10.4 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package s3
import (
"context"
"io"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/rs/zerolog/log"
"github.com/soapiestwaffles/s3-nuke/pkg/aws/config"
)
// Service defines functions related to S3 operations
type Service interface {
// ListBuckets loads buckets into memory
GetAllBuckets(ctx context.Context) ([]Bucket, error)
// CreateBucketSimple creates a new, simple S3 bucket in the current/default region
//
// (This function is mainly used in s3-nuke for testing)
CreateBucketSimple(ctx context.Context, bucketName string, region string, versioned bool) error
// PutBucketObject puts an object in an S3 bucket
//
// returns Etag, VersionID, and Error
// (This function is mainly used in s3-nuke for testing)
PutObjectSimple(ctx context.Context, bucketName string, keyName string, body io.Reader) (*string, *string, error)
// GetBucketRegion will return the region of a bucket
GetBucektRegion(ctx context.Context, bucketName string) (string, error)
// ListObjects will return some or all (up to 1,000) of the objects in a bucket with each request.
// Objects are returned sorted in an ascending order of the respective key names in the list.
// use continuationToken to list the next page of objects. For first call, set continuationToken to nil
//
// prefix limits the response to keys that begin with the specified prefix. Set to nil if not used.
//
// returns:
// `[]string`` contains the slice of keys returned by this request
// `*string` contains the continuation token, if any
// `error` is returned not nil if an error has occurred requesting the list
ListObjects(ctx context.Context, bucketName string, continuationToken *string, prefix *string) ([]string, *string, error)
// ListObjectVersions will return version information
//
// returns:
// `[]ObjectVersion` contains object version information
// `*string` contains the keyMarker which marks the last key returned in a truncated response
// `*string` contains the versionIDMarker which marks the last version of the key returned in a truncated response
// `error` is returned not nil if an error has occurred requesting the object version list
ListObjectVersions(ctx context.Context, bucketName string, keyMarker *string, versionIDMarker *string, prefix *string) ([]ObjectVersion, *string, *string, error)
// DeleteObjects will bulk delete up to 1000 objects in one call
//
// returns:
// `[]ObjectIdentifier` contains list of objects deleted
// `error` is returned not nil if an error has occurred requesting the object deletion
DeleteObjects(ctx context.Context, bucketName string, objects []ObjectIdentifier) ([]ObjectIdentifier, error)
}
// Bucket contains information about an S3 bucket
type Bucket struct {
CreationDate *time.Time
Name *string
}
// ObjectVersion defines an object and version information from an S3 bucket
type ObjectVersion struct {
ObjectIdentifier
IsDeleteMarker bool
}
// ObjectIdentifier is used to identify a specific S3 object and version
type ObjectIdentifier struct {
Key *string
VersionID *string
}
// ServiceOption is used with NewS3Service and configures the newly created s3Service
type ServiceOption func(s *service)
type service struct {
client S3API
awsEndpoint string
region string
}
// NewService returns an initialized S3Service
func NewService(opts ...ServiceOption) Service {
svc := &service{}
for _, opt := range opts {
opt(svc)
}
if svc.client == nil {
if svc.region == "" {
svc.client = newS3Client(os.Getenv("AWS_REGION"), svc.awsEndpoint)
} else {
svc.client = newS3Client(svc.region, svc.awsEndpoint)
}
}
return svc
}
// WithS3API should be used if you want to initialize your own S3 client (such as in cases of a mock S3 client for testing)
// This cannot be used with WithAWSEndpoint
func WithS3API(s3Client S3API) ServiceOption {
return func(s *service) {
s.client = s3Client
}
}
// WithAWSEndpoint sets endpoint to be used by the AWS client
// This cannot be used with WithS3API
func WithAWSEndpoint(awsEndpoint string) ServiceOption {
return func(s *service) {
s.awsEndpoint = awsEndpoint
}
}
// WithRegion sets the AWS client region
func WithRegion(region string) ServiceOption {
return func(s *service) {
s.region = region
}
}
func (s *service) GetAllBuckets(ctx context.Context) ([]Bucket, error) {
log.Debug().Msg("s3: getting list of all buckets")
result, err := s.client.ListBuckets(ctx, &s3.ListBucketsInput{})
if err != nil {
return nil, err
}
buckets := make([]Bucket, 0)
for _, b := range result.Buckets {
buckets = append(buckets,
Bucket{
CreationDate: b.CreationDate,
Name: b.Name,
},
)
}
return buckets, nil
}
func (s *service) CreateBucketSimple(ctx context.Context, bucketName string, region string, versioned bool) error {
_, err := s.client.CreateBucket(ctx, &s3.CreateBucketInput{
Bucket: &bucketName,
ACL: types.BucketCannedACLPrivate,
CreateBucketConfiguration: &types.CreateBucketConfiguration{
LocationConstraint: types.BucketLocationConstraint(region),
},
})
if err != nil {
return err
}
if versioned {
_, err := s.client.PutBucketVersioning(ctx, &s3.PutBucketVersioningInput{
Bucket: &bucketName,
VersioningConfiguration: &types.VersioningConfiguration{
Status: "Enabled",
},
})
if err != nil {
return err
}
}
return nil
}
func (s *service) PutObjectSimple(ctx context.Context, bucketName string, keyName string, body io.Reader) (*string, *string, error) {
result, err := s.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: &bucketName,
Key: &keyName,
Body: body,
})
if err != nil {
return nil, nil, err
}
return result.ETag, result.VersionId, nil
}
func (s *service) GetBucektRegion(ctx context.Context, bucketName string) (string, error) {
log.Debug().Str("bucket", bucketName).Msg("s3: looking up bucket region")
result, err := s.client.GetBucketLocation(ctx, &s3.GetBucketLocationInput{
Bucket: &bucketName,
})
if err != nil {
return "", err
}
if result.LocationConstraint == "" {
return "us-east-1", nil
}
return string(result.LocationConstraint), nil
}
func (s *service) ListObjects(ctx context.Context, bucketName string, continuationToken *string, prefix *string) ([]string, *string, error) {
log.Debug().Str("bucket", bucketName).Msg("s3: list objects")
result, err := s.client.ListObjectsV2(ctx, &s3.ListObjectsV2Input{
Bucket: &bucketName,
ContinuationToken: continuationToken,
MaxKeys: aws.Int32(1000),
Prefix: prefix,
})
if err != nil {
return nil, nil, err
}
keys := []string{}
for _, object := range result.Contents {
keys = append(keys, *object.Key)
}
if aws.ToBool(result.IsTruncated) {
return keys, result.NextContinuationToken, nil
}
return keys, nil, nil
}
func (s *service) ListObjectVersions(ctx context.Context, bucketName string, keyMarker *string, versionIDMarker *string, prefix *string) ([]ObjectVersion, *string, *string, error) {
log.Debug().Str("bucket", bucketName).
Interface("keyMarker", keyMarker).
Interface("versionIDMarker", versionIDMarker).
Interface("prefix", prefix).
Msg("s3: list object versions")
result, err := s.client.ListObjectVersions(ctx, &s3.ListObjectVersionsInput{
Bucket: &bucketName,
KeyMarker: keyMarker,
MaxKeys: aws.Int32(1000),
Prefix: prefix,
VersionIdMarker: versionIDMarker,
})
if err != nil {
return nil, nil, nil, err
}
versions := []ObjectVersion{}
for _, version := range result.Versions {
versions = append(versions, ObjectVersion{
ObjectIdentifier: ObjectIdentifier{
Key: version.Key,
VersionID: version.VersionId,
},
IsDeleteMarker: false,
})
}
for _, deleteMarker := range result.DeleteMarkers {
versions = append(versions, ObjectVersion{
ObjectIdentifier: ObjectIdentifier{
Key: deleteMarker.Key,
VersionID: deleteMarker.VersionId,
},
IsDeleteMarker: true,
})
}
return versions, result.NextKeyMarker, result.NextVersionIdMarker, nil
}
func (s *service) DeleteObjects(ctx context.Context, bucketName string, objects []ObjectIdentifier) ([]ObjectIdentifier, error) {
log.Debug().Str("bucket", bucketName).Interface("objects", objects).Msg("delete objects")
deleteObjects := []types.ObjectIdentifier{}
for _, object := range objects {
deleteObjects = append(deleteObjects, types.ObjectIdentifier{
Key: object.Key,
VersionId: object.VersionID,
})
}
result, err := s.client.DeleteObjects(ctx, &s3.DeleteObjectsInput{
Bucket: &bucketName,
Delete: &types.Delete{
Objects: deleteObjects,
Quiet: aws.Bool(false),
},
})
if err != nil {
return nil, err
}
returnValue := []ObjectIdentifier{}
for _, object := range result.Deleted {
returnValue = append(returnValue, ObjectIdentifier{
Key: object.Key,
VersionID: object.VersionId,
})
}
return returnValue, nil
}
func newS3Client(region string, awsEndpoint string) *s3.Client {
// Default to us-east-1 if no region is provided
if region == "" {
region = "us-east-1"
}
// Initialize AWS S3 Client
cfg, err := config.New(region)
if err != nil {
return nil
}
return s3.NewFromConfig(cfg, func(o *s3.Options) {
if awsEndpoint != "" {
o.BaseEndpoint = &awsEndpoint
}
})
}
// =====
// S3API defines the interface for AWS S3 SDK functions
type S3API interface {
ListBuckets(ctx context.Context,
params *s3.ListBucketsInput,
optFns ...func(*s3.Options)) (*s3.ListBucketsOutput, error)
CreateBucket(ctx context.Context,
params *s3.CreateBucketInput,
optFns ...func(*s3.Options)) (*s3.CreateBucketOutput, error)
PutBucketVersioning(ctx context.Context,
params *s3.PutBucketVersioningInput,
optFns ...func(*s3.Options)) (*s3.PutBucketVersioningOutput, error)
PutObject(ctx context.Context,
params *s3.PutObjectInput,
optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
GetBucketLocation(ctx context.Context,
params *s3.GetBucketLocationInput,
optFns ...func(*s3.Options)) (*s3.GetBucketLocationOutput, error)
ListObjectsV2(ctx context.Context,
params *s3.ListObjectsV2Input,
optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error)
ListObjectVersions(ctx context.Context,
params *s3.ListObjectVersionsInput,
optFns ...func(*s3.Options)) (*s3.ListObjectVersionsOutput, error)
DeleteObjects(ctx context.Context,
params *s3.DeleteObjectsInput,
optFns ...func(*s3.Options)) (*s3.DeleteObjectsOutput, error)
}