diff --git a/bucket.go b/bucket.go index b0345e5..56602be 100644 --- a/bucket.go +++ b/bucket.go @@ -12,6 +12,7 @@ import ( "strings" "sync/atomic" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/aws/smithy-go" @@ -215,6 +216,15 @@ func (b *Bucket) Exists(ctx context.Context, key string) (bool, error) { return true, nil } +// DeleteBucket deletes the whole bucket +func (b *Bucket) DeleteBucket(ctx context.Context) error { + _, err := b.cli.DeleteBucket(ctx, &s3.DeleteBucketInput{ + Bucket: aws.String(b.name), + }) + + return err +} + // Delete deletes the given object keys func (b *Bucket) Delete(ctx context.Context, keys ...string) error { var err error diff --git a/bucket_test.go b/bucket_test.go index 559b057..87f0104 100644 --- a/bucket_test.go +++ b/bucket_test.go @@ -64,7 +64,11 @@ func TestReadFrom(t *testing.T) { uploadClient: s, } - bucket := s3io.NewRawBucket("testing", s3io.DefaultChunkSize, s3io.DefaultChunkSize, 1, noopLogger, cli) + bucket := s3io.NewRawBucket( + "testing", + s3io.DefaultChunkSize, + s3io.DefaultChunkSize, + 1, noopLogger, cli) var amount int64 = 1024 * 1024 * 12 @@ -183,6 +187,10 @@ func (b *BucketLoggingClient) ListObjectsV2( return out, nil } +func (b *BucketLoggingClient) DeleteBucket(_ context.Context, _ *s3.DeleteBucketInput, optFns ...func(*s3.Options)) (*s3.DeleteBucketOutput, error) { + panic("not implemented") +} + func (b *BucketLoggingClient) DeleteObjects(_ context.Context, input *s3.DeleteObjectsInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectsOutput, error) { panic("not implemented") } diff --git a/s3io.go b/s3io.go index 0856631..8f5d1da 100644 --- a/s3io.go +++ b/s3io.go @@ -42,6 +42,7 @@ type BucketApiClient interface { s3.ListObjectsV2APIClient DeleteObject(ctx context.Context, input *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) DeleteObjects(ctx context.Context, input *s3.DeleteObjectsInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectsOutput, error) + DeleteBucket(ctx context.Context, input *s3.DeleteBucketInput, optFns ...func(*s3.Options)) (*s3.DeleteBucketOutput, error) } type concurrencyLock struct { diff --git a/s3io_test.go b/s3io_test.go index 5d7306d..9936285 100644 --- a/s3io_test.go +++ b/s3io_test.go @@ -1,7 +1,6 @@ package s3io_test import ( - "bytes" "context" "crypto/rand" "crypto/sha256" @@ -60,9 +59,10 @@ func TestBucketFS(t *testing.T) { t.Fatalf("unable to initialize template engine: %v", err) } - buff := &bytes.Buffer{} + hash := sha256.New() + err = engine.ExecuteTemplate( - buff, + hash, "index.html.tmpl", map[string]string{"Message": "hello world"}, ) @@ -70,7 +70,10 @@ func TestBucketFS(t *testing.T) { t.Errorf("unable to execute template: %v", err) } - if buff.String() != "

hello world

" { + expectedHash := sha256.New() + _, _ = io.WriteString(expectedHash, "

hello world

") + + if a, e := fmt.Sprintf("%x", hash.Sum(nil)), fmt.Sprintf("%x", expectedHash.Sum(nil)); e != a { t.Errorf("error unexpected message") } }) @@ -134,7 +137,6 @@ func BenchmarkAgainstManager(b *testing.B) { b.Run("fs download", func(b *testing.B) { rd := bucket.NewReader(ctx, fileName, s3io.WithReaderLogger(noopLogger)) - // buf := bytes.NewBuffer(make([]byte, fileSize)) buf := io.Discard _, err = io.Copy(buf, rd) if err != nil { @@ -145,7 +147,6 @@ func BenchmarkAgainstManager(b *testing.B) { b.Run("manager download", func(b *testing.B) { downloader := manager.NewDownloader(bucket.Client()) - // buf := discardWriterAt{} buf := manager.NewWriteAtBuffer(make([]byte, fileSize)) _, err := downloader.Download(context.Background(), buf, &s3.GetObjectInput{ Bucket: &bucketName,