Skip to content

Commit

Permalink
feat: add delete bucket function (#115)
Browse files Browse the repository at this point in the history
* feat: add delete bucket function

* refactor: use hashes in tests
  • Loading branch information
jobstoit authored Oct 27, 2024
1 parent f232a29 commit 50785cd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
10 changes: 10 additions & 0 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions s3io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions s3io_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package s3io_test

import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
Expand Down Expand Up @@ -60,17 +59,21 @@ 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"},
)
if err != nil {
t.Errorf("unable to execute template: %v", err)
}

if buff.String() != "<p>hello world</p>" {
expectedHash := sha256.New()
_, _ = io.WriteString(expectedHash, "<p>hello world</p>")

if a, e := fmt.Sprintf("%x", hash.Sum(nil)), fmt.Sprintf("%x", expectedHash.Sum(nil)); e != a {
t.Errorf("error unexpected message")
}
})
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit 50785cd

Please sign in to comment.