delete old tab bundles before making new ones
This commit is contained in:
parent
a977a8c0b3
commit
21f2a75ed3
2 changed files with 43 additions and 1 deletions
|
|
@ -3,11 +3,13 @@ package main
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
)
|
||||
|
||||
var s3Client *s3.Client
|
||||
|
|
@ -44,3 +46,35 @@ func s3UploadBundle(ctx context.Context, bucket, key string, data []byte) error
|
|||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// s3DeletePrefix deletes all objects under a prefix in S3.
|
||||
func s3DeletePrefix(ctx context.Context, bucket, prefix string) error {
|
||||
paginator := s3.NewListObjectsV2Paginator(s3Client, &s3.ListObjectsV2Input{
|
||||
Bucket: aws.String(bucket),
|
||||
Prefix: aws.String(prefix),
|
||||
})
|
||||
|
||||
for paginator.HasMorePages() {
|
||||
page, err := paginator.NextPage(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(page.Contents) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
var objects []types.ObjectIdentifier
|
||||
for _, obj := range page.Contents {
|
||||
objects = append(objects, types.ObjectIdentifier{Key: obj.Key})
|
||||
}
|
||||
|
||||
_, err = s3Client.DeleteObjects(ctx, &s3.DeleteObjectsInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Delete: &types.Delete{Objects: objects},
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete batch: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue