-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
221 lines (182 loc) · 4.97 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
//go:generate mockgen -source=$GOFILE -destination=s3_mock.go -package=$GOPACKAGE -write_package_comment=false
package client
import (
"context"
"runtime"
"strings"
"sync"
"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"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
)
const S3DeleteObjectsSizeLimit = 1000
var SleepTimeSecForS3 = 10
type IS3 interface {
DeleteBucket(ctx context.Context, bucketName *string) error
DeleteObjects(ctx context.Context, bucketName *string, objects []types.ObjectIdentifier) ([]types.Error, error)
ListObjectVersions(ctx context.Context, bucketName *string) ([]types.ObjectIdentifier, error)
CheckBucketExists(ctx context.Context, bucketName *string) (bool, error)
}
var _ IS3 = (*S3)(nil)
type S3 struct {
client *s3.Client
}
func NewS3(client *s3.Client) *S3 {
return &S3{
client,
}
}
func (s *S3) DeleteBucket(ctx context.Context, bucketName *string) error {
input := &s3.DeleteBucketInput{
Bucket: bucketName,
}
_, err := s.client.DeleteBucket(ctx, input)
if err != nil {
return &ClientError{
ResourceName: bucketName,
Err: err,
}
}
return nil
}
func (s *S3) DeleteObjects(ctx context.Context, bucketName *string, objects []types.ObjectIdentifier) ([]types.Error, error) {
errors := []types.Error{}
if len(objects) == 0 {
return errors, nil
}
eg, ctx := errgroup.WithContext(ctx)
outputsCh := make(chan *s3.DeleteObjectsOutput, int64(runtime.NumCPU()))
sem := semaphore.NewWeighted(int64(runtime.NumCPU()))
wg := sync.WaitGroup{}
nextObjects := make([]types.ObjectIdentifier, len(objects))
copy(nextObjects, objects)
wg.Add(1)
go func() {
defer wg.Done()
for outputErrors := range outputsCh {
outputErrors := outputErrors
if len(outputErrors.Errors) > 0 {
errors = append(errors, outputErrors.Errors...)
}
}
}()
for {
inputObjects := []types.ObjectIdentifier{}
if len(nextObjects) > S3DeleteObjectsSizeLimit {
inputObjects = append(inputObjects, nextObjects[:S3DeleteObjectsSizeLimit]...)
nextObjects = nextObjects[S3DeleteObjectsSizeLimit:]
} else {
inputObjects = append(inputObjects, nextObjects...)
nextObjects = nil
}
input := &s3.DeleteObjectsInput{
Bucket: bucketName,
Delete: &types.Delete{
Objects: inputObjects,
Quiet: *aws.Bool(true),
},
}
if err := sem.Acquire(ctx, 1); err != nil {
return errors, &ClientError{
ResourceName: bucketName,
Err: err,
}
}
eg.Go(func() error {
defer sem.Release(1)
retryable := func(err error) bool {
return strings.Contains(err.Error(), "api error SlowDown")
}
optFn := func(o *s3.Options) {
o.Retryer = NewRetryer(retryable, SleepTimeSecForS3)
}
output, err := s.client.DeleteObjects(ctx, input, optFn)
if err != nil {
return err // return non wrapping error because wrap after eg.Wait()
}
outputsCh <- output
return nil
})
if len(nextObjects) == 0 {
break
}
}
go func() {
eg.Wait()
close(outputsCh)
}()
if err := eg.Wait(); err != nil {
return nil, &ClientError{
ResourceName: bucketName,
Err: err,
}
}
// wait errors set before access an errors var at below return (for race)
wg.Wait()
return errors, nil
}
func (s *S3) ListObjectVersions(ctx context.Context, bucketName *string) ([]types.ObjectIdentifier, error) {
var keyMarker *string
var versionIdMarker *string
objectIdentifiers := []types.ObjectIdentifier{}
for {
select {
case <-ctx.Done():
return objectIdentifiers, &ClientError{
ResourceName: bucketName,
Err: ctx.Err(),
}
default:
}
input := &s3.ListObjectVersionsInput{
Bucket: bucketName,
KeyMarker: keyMarker,
VersionIdMarker: versionIdMarker,
}
output, err := s.client.ListObjectVersions(ctx, input)
if err != nil {
return nil, &ClientError{
ResourceName: bucketName,
Err: err,
}
}
for _, version := range output.Versions {
objectIdentifier := types.ObjectIdentifier{
Key: version.Key,
VersionId: version.VersionId,
}
objectIdentifiers = append(objectIdentifiers, objectIdentifier)
}
for _, deleteMarker := range output.DeleteMarkers {
objectIdentifier := types.ObjectIdentifier{
Key: deleteMarker.Key,
VersionId: deleteMarker.VersionId,
}
objectIdentifiers = append(objectIdentifiers, objectIdentifier)
}
keyMarker = output.NextKeyMarker
versionIdMarker = output.NextVersionIdMarker
if keyMarker == nil && versionIdMarker == nil {
break
}
}
return objectIdentifiers, nil
}
func (s *S3) CheckBucketExists(ctx context.Context, bucketName *string) (bool, error) {
input := &s3.ListBucketsInput{}
output, err := s.client.ListBuckets(ctx, input)
if err != nil {
return false, &ClientError{
ResourceName: bucketName,
Err: err,
}
}
for _, bucket := range output.Buckets {
if *bucket.Name == *bucketName {
return true, nil
}
}
return false, nil
}