-
Notifications
You must be signed in to change notification settings - Fork 17
Delete dailies after rolling up #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9203da2
faa9a27
ec8a94b
1abf550
47a759b
0b83789
8861f72
81d1564
3e87160
a6e8444
c2fe571
33b267f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "io" | ||
| "log/slog" | ||
| "os" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
|
|
@@ -138,3 +139,46 @@ func GetS3File(ctx context.Context, s3Client *s3x.Service, bucket, key string) ( | |
|
|
||
| return output.Body, nil | ||
| } | ||
|
|
||
| // DeleteS3Files deletes multiple files from S3, automatically batching into requests of 1000 keys | ||
| func DeleteS3Files(ctx context.Context, s3Client *s3x.Service, bucket string, keys []string) (int, error) { | ||
| if len(keys) == 0 { | ||
| return 0, nil | ||
| } | ||
|
|
||
| totalDeleted := 0 | ||
| var lastErr error | ||
|
|
||
| for batch := range slices.Chunk(keys, 1000) { | ||
| objects := make([]types.ObjectIdentifier, len(batch)) | ||
| for i, key := range batch { | ||
| objects[i] = types.ObjectIdentifier{Key: aws.String(key)} | ||
| } | ||
|
|
||
| output, err := s3Client.Client.DeleteObjects(ctx, &s3.DeleteObjectsInput{ | ||
| Bucket: aws.String(bucket), | ||
| Delete: &types.Delete{Objects: objects, Quiet: aws.Bool(true)}, | ||
| }) | ||
| if err != nil { | ||
| lastErr = fmt.Errorf("error batch deleting S3 objects bucket=%s: %w", bucket, err) | ||
| continue | ||
| } | ||
|
|
||
| // check for individual errors | ||
| if len(output.Errors) > 0 { | ||
| for _, e := range output.Errors { | ||
| slog.Error("error deleting S3 object in batch", "bucket", bucket, "key", *e.Key, "error", *e.Message) | ||
| } | ||
| totalDeleted += len(batch) - len(output.Errors) | ||
| lastErr = fmt.Errorf("%d errors deleting S3 objects", len(output.Errors)) | ||
|
||
| } else { | ||
| totalDeleted += len(batch) | ||
| } | ||
| } | ||
|
|
||
| if totalDeleted > 0 { | ||
| slog.Debug("deleted S3 files", "bucket", bucket, "count", totalDeleted) | ||
| } | ||
|
|
||
| return totalDeleted, lastErr | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential nil pointer dereference. The code assumes that
e.Keyande.Messageare non-nil, but the AWS SDK types may return nil pointers for these fields in error cases. Consider adding nil checks before dereferencing these pointers to avoid panics.