Skip to content

Commit 92224a2

Browse files
authored
feat: add delete to internal storage module (#45)
1 parent 98782bf commit 92224a2

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

internal/storage/local/local.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,16 @@ func (s Service) Exists(ctx context.Context, bucketName, key string) (bool, erro
9999
func (s Service) GeneratePresignedURL(ctx context.Context, bucketName, key string) (string, error) {
100100
return "", errors.New("not implemented")
101101
}
102+
103+
// Delete deletes an object from the local file system.
104+
func (s Service) Delete(ctx context.Context, bucket, key string) error {
105+
106+
// Get the file path.
107+
name := filepath.Join(s.root, bucket, key)
108+
err := os.Remove(name)
109+
if err != nil {
110+
return err
111+
}
112+
113+
return nil
114+
}

internal/storage/minio/minio.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func New(endpoint, accessKey, secretKey string) (Service, error) {
4343
return Service{s3Client}, nil
4444
}
4545

46-
// Upload uploads an object to s3.
46+
// Upload uploads an object to the remote storage,.
4747
func (s Service) Upload(ctx context.Context, bucket, key string,
4848
file io.Reader) error {
4949

@@ -134,3 +134,16 @@ func (s Service) GeneratePresignedURL(ctx context.Context, bucketName,
134134
}
135135
return presignedURL.String(), nil
136136
}
137+
138+
// Delete deletes an object from the remote storage.
139+
func (s Service) Delete(ctx context.Context, bucket, key string) error {
140+
141+
opts := mio.RemoveObjectOptions{}
142+
143+
err := s.client.RemoveObject(ctx, bucket, key, opts)
144+
if err != nil {
145+
return err
146+
}
147+
148+
return nil
149+
}

internal/storage/s3/s3.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ package s3
66

77
import (
88
"context"
9+
"io"
10+
"time"
11+
912
"github.com/aws/aws-sdk-go/aws"
1013
"github.com/aws/aws-sdk-go/aws/awserr"
1114
"github.com/aws/aws-sdk-go/aws/credentials"
1215
"github.com/aws/aws-sdk-go/aws/session"
1316
awss3 "github.com/aws/aws-sdk-go/service/s3"
1417
"github.com/aws/aws-sdk-go/service/s3/s3manager"
15-
"io"
16-
"time"
1718
)
1819

1920
// Service provides abstraction to cloud object storage.
@@ -64,6 +65,7 @@ func New(region, accessKey, secretKey string) (Service, error) {
6465
// Create a downloader with S3 client and custom options
6566
downloader := s3manager.NewDownloaderWithClient(s3Svc,
6667
func(u *s3manager.Downloader) {
68+
u.Concurrency = 1 // Guarantee sequential writes
6769
u.PartSize = 5 * 1024 * 1024 // 5MB per part
6870
})
6971

@@ -169,3 +171,20 @@ func (s Service) GeneratePresignedURL(ctx context.Context, bucketName, key strin
169171

170172
return urlStr, nil
171173
}
174+
175+
// Delete removes an object from the store.
176+
func (s Service) Delete(ctx context.Context, bucket, key string) (bool, error) {
177+
178+
// Prepare the delete object input.
179+
input := &awss3.DeleteObjectInput{
180+
Bucket: aws.String(bucket),
181+
Key: aws.String(key),
182+
}
183+
184+
_, err := s.s3svc.DeleteObjectWithContext(ctx, input)
185+
if err != nil {
186+
return false, err
187+
}
188+
189+
return true, nil
190+
}

0 commit comments

Comments
 (0)