Skip to content

Commit

Permalink
Merge pull request #91 from ekristen/s3-delete-notify
Browse files Browse the repository at this point in the history
feat: write log message if s3 bucket deletion is going to take a while
  • Loading branch information
ekristen authored Feb 23, 2024
2 parents da5ac7a + 238c985 commit bf62237
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions resources/s3-buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package resources

import (
"context"

"fmt"
"time"

"github.com/gotidy/ptr"
"github.com/sirupsen/logrus"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand Down Expand Up @@ -118,7 +118,7 @@ type S3Bucket struct {
tags []*s3.Tag
}

func (e *S3Bucket) Remove(_ context.Context) error {
func (e *S3Bucket) Remove(ctx context.Context) error {
_, err := e.svc.DeleteBucketPolicy(&s3.DeleteBucketPolicyInput{
Bucket: &e.name,
})
Expand All @@ -134,12 +134,12 @@ func (e *S3Bucket) Remove(_ context.Context) error {
return err
}

err = e.RemoveAllVersions()
err = e.RemoveAllVersions(ctx)
if err != nil {
return err
}

err = e.RemoveAllObjects()
err = e.RemoveAllObjects(ctx)
if err != nil {
return err
}
Expand All @@ -151,22 +151,22 @@ func (e *S3Bucket) Remove(_ context.Context) error {
return err
}

func (e *S3Bucket) RemoveAllVersions() error {
func (e *S3Bucket) RemoveAllVersions(ctx context.Context) error {
params := &s3.ListObjectVersionsInput{
Bucket: &e.name,
}

iterator := newS3DeleteVersionListIterator(e.svc, params)
return s3manager.NewBatchDeleteWithClient(e.svc).Delete(aws.BackgroundContext(), iterator)
return s3manager.NewBatchDeleteWithClient(e.svc).Delete(ctx, iterator)
}

func (e *S3Bucket) RemoveAllObjects() error {
func (e *S3Bucket) RemoveAllObjects(ctx context.Context) error {
params := &s3.ListObjectsInput{
Bucket: &e.name,
}

iterator := s3manager.NewDeleteListIterator(e.svc, params)
return s3manager.NewBatchDeleteWithClient(e.svc).Delete(aws.BackgroundContext(), iterator)
return s3manager.NewBatchDeleteWithClient(e.svc).Delete(ctx, iterator)
}

func (e *S3Bucket) Properties() types.Properties {
Expand All @@ -186,9 +186,10 @@ func (e *S3Bucket) String() string {
}

type s3DeleteVersionListIterator struct {
Bucket *string
Paginator request.Pagination
objects []*s3.ObjectVersion
Bucket *string
Paginator request.Pagination
objects []*s3.ObjectVersion
lastNotify time.Time
}

func newS3DeleteVersionListIterator(
Expand All @@ -213,6 +214,7 @@ func newS3DeleteVersionListIterator(
for _, opt := range opts {
opt(iter)
}

return iter
}

Expand All @@ -234,6 +236,13 @@ func (iter *s3DeleteVersionListIterator) Next() bool {
}
}

if len(iter.objects) > 500 && (iter.lastNotify.IsZero() || time.Since(iter.lastNotify) > 120*time.Second) {
logrus.Infof(
"S3Bucket: %s - empty bucket operation in progress, this could take a while, please be patient",
*iter.Bucket)
iter.lastNotify = time.Now().UTC()
}

return len(iter.objects) > 0
}

Expand Down

0 comments on commit bf62237

Please sign in to comment.