-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleter.go
58 lines (50 loc) · 1.18 KB
/
deleter.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
package main
import (
"sync"
"github.com/aws/aws-sdk-go/aws"
awss3 "github.com/aws/aws-sdk-go/service/s3"
log "github.com/sirupsen/logrus"
)
const (
MaxBulkOpSize = 1000
)
func doDelete(s3 *awss3.S3, params *awss3.DeleteObjectsInput, status *Status) {
_, err := s3.DeleteObjects(params)
if err != nil {
if status.IncrementErrors() == 1 {
log.WithError(err).Error("Failed to delete objects")
}
} else {
lastObject := params.Delete.Objects[len(params.Delete.Objects)-1]
status.Update(MaxBulkOpSize, *lastObject.Key)
}
}
func DeleteObjects(bucketName string, s3 *awss3.S3, status *Status, objChan ObjectChannel, wg *sync.WaitGroup) {
buffer := [MaxBulkOpSize]*awss3.ObjectIdentifier{}
current := 0
for obj := range objChan {
buffer[current] = obj
current++
if current < MaxBulkOpSize {
continue
}
current = 0
p := awss3.DeleteObjectsInput{
Bucket: aws.String(bucketName),
Delete: &awss3.Delete{
Objects: buffer[:],
},
}
doDelete(s3, &p, status)
}
if current != 0 {
p := awss3.DeleteObjectsInput{
Bucket: aws.String(bucketName),
Delete: &awss3.Delete{
Objects: buffer[:current],
},
}
doDelete(s3, &p, status)
}
wg.Done()
}