/
main.go
186 lines (164 loc) · 4.81 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"log/slog"
"os"
"sync"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/urfave/cli/v2"
)
func deleteObjects(svc *s3.Client, bucketName string, keys []string, wg *sync.WaitGroup, counter *atomic.Uint64) {
defer wg.Done()
_, err := svc.DeleteObjects(context.TODO(), &s3.DeleteObjectsInput{
Bucket: &bucketName,
Delete: &types.Delete{
Objects: func() []types.ObjectIdentifier {
identifiers := make([]types.ObjectIdentifier, len(keys))
for i := range keys {
key := keys[i]
identifiers[i] = types.ObjectIdentifier{
Key: &key,
}
}
return identifiers
}(),
},
})
if err != nil {
slog.Error("failed to delete objects", "keys", keys, "error", err)
return
}
for _, key := range keys {
slog.Debug("deleted object", "key", key)
}
counter.Add(uint64(len(keys)))
}
func main() {
app := &cli.App{
Name: "s3purge",
Usage: "Delete all files in an S3-compatible bucket",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "endpoint",
Usage: "S3-compatible endpoint URL",
Required: true,
},
&cli.StringFlag{
Name: "bucket",
Usage: "Name of the S3 bucket",
Required: true,
},
&cli.StringFlag{
Name: "accessKey",
Usage: "Access key ID",
Required: true,
},
&cli.StringFlag{
Name: "secretKey",
Usage: "Secret access key",
Required: true,
},
&cli.Int64Flag{
Name: "concurrency",
Usage: "Number of concurrent deletions",
Value: 250,
},
&cli.DurationFlag{
Name: "rateDisplayInterval",
Usage: "Interval to display deletion rate",
Value: 5 * time.Second,
},
&cli.StringFlag{
Name: "logLevel",
Usage: "Log level (debug, info, warn, error)",
Value: "info",
},
},
Action: func(c *cli.Context) error {
endpoint := c.String("endpoint")
bucketName := c.String("bucket")
accessKeyID := c.String("accessKey")
secretAccessKey := c.String("secretKey")
logLvl := new(slog.LevelVar)
logLvl.UnmarshalText([]byte(c.String("logLevel")))
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: logLvl,
})))
slog.Info("Starting S3 purge", "endpoint", endpoint, "bucket", bucketName, "concurrency", c.Int64("concurrency"))
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithEndpointResolver(aws.EndpointResolverFunc(
func(service, region string) (aws.Endpoint, error) {
return aws.Endpoint{
URL: endpoint,
}, nil
},
)),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyID, secretAccessKey, "")),
)
if err != nil {
return fmt.Errorf("unable to load SDK config: %v", err)
}
svc := s3.NewFromConfig(cfg)
// Paginator to list all the objects in the bucket
paginator := s3.NewListObjectsV2Paginator(svc, &s3.ListObjectsV2Input{
Bucket: &bucketName,
})
var wg sync.WaitGroup
deleteCounter := atomic.Uint64{}
startTime := time.Now()
go func() {
for {
time.Sleep(c.Duration("rateDisplayInterval"))
duration := time.Since(startTime).Seconds()
rate := float64(deleteCounter.Load()) / duration
slog.Info(fmt.Sprintf("Current deletion rate: %.3f items/second", rate))
}
}()
sem := make(chan struct{}, c.Int64("concurrency"))
const batchSize = 500 // Group objects into batches of 500
var objectKeys []string // This slice will accumulate keys to delete in a batch
for paginator.HasMorePages() {
output, err := paginator.NextPage(context.TODO())
if err != nil {
return fmt.Errorf("failed to list objects: %v", err)
}
for _, item := range output.Contents {
objectKeys = append(objectKeys, aws.ToString(item.Key))
// If we have reached the batchSize, delete these objects as a batch
if len(objectKeys) == batchSize {
sem <- struct{}{} // Acquire concurrency slot
wg.Add(1)
go func(keysToDelete []string) {
defer func() {
<-sem // Release concurrency slot
}()
deleteObjects(svc, bucketName, keysToDelete, &wg, &deleteCounter)
}(objectKeys)
objectKeys = nil // Reset the slice for the next batch
}
}
}
// After exiting the loop, check if there are any remaining keys to delete
if len(objectKeys) > 0 {
sem <- struct{}{} // Acquire concurrency slot
wg.Add(1)
go deleteObjects(svc, bucketName, objectKeys, &wg, &deleteCounter)
}
wg.Wait() // Wait for all deletions to complete
slog.Info(fmt.Sprintf("Deleted %d objects", deleteCounter.Load()))
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}