Skip to content

Commit

Permalink
[S3] Trim left slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneM committed Aug 26, 2019
1 parent d035717 commit e8553fe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
9 changes: 9 additions & 0 deletions storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storage
import (
"context"
"io"
"strings"
"time"

"github.com/Scalingo/go-utils/logger"
Expand Down Expand Up @@ -75,6 +76,7 @@ func NewS3(cfg S3Config, opts ...s3Opt) *S3 {
}

func (s *S3) Get(ctx context.Context, path string) (io.ReadCloser, error) {
path = fullPath(path)
log := logger.Get(ctx)
log.WithField("path", path).Info("Get object")

Expand All @@ -90,6 +92,7 @@ func (s *S3) Get(ctx context.Context, path string) (io.ReadCloser, error) {
}

func (s *S3) Upload(ctx context.Context, file io.Reader, path string) error {
path = fullPath(path)
input := &s3manager.UploadInput{
Body: file,
Bucket: &s.cfg.Bucket,
Expand All @@ -107,6 +110,7 @@ func (s *S3) Upload(ctx context.Context, file io.Reader, path string) error {
// implemented because of the eventual consistency of S3 backends NotFound
// error are sometimes returned when the object was just uploaded.
func (s *S3) Size(ctx context.Context, path string) (int64, error) {
path = fullPath(path)
var res int64
err := s.retryWrapper(ctx, SizeMethod, func(ctx context.Context) error {
log := logger.Get(ctx).WithField("key", path)
Expand All @@ -128,6 +132,7 @@ func (s *S3) Size(ctx context.Context, path string) (int64, error) {
}

func (s *S3) Delete(ctx context.Context, path string) error {
path = fullPath(path)
input := &s3.DeleteObjectInput{Bucket: &s.cfg.Bucket, Key: &path}
req := s.s3client.DeleteObjectRequest(input)
_, err := req.Send(ctx)
Expand Down Expand Up @@ -182,3 +187,7 @@ func s3Config(cfg S3Config) aws.Config {

return config
}

func fullPath(path string) string {
return strings.TrimLeft("/"+path, "/")
}
8 changes: 4 additions & 4 deletions storage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestS3_Size(t *testing.T) {
"it should make a HEAD request on the object": {
expectMock: func(t *testing.T, m *s3mock.MockS3Client) {
m.EXPECT().HeadObjectRequest(&s3.HeadObjectInput{
Bucket: aws.String("bucket"), Key: aws.String("/key"),
Bucket: aws.String("bucket"), Key: aws.String("key"),
}).Return(s3.HeadObjectRequest{Request: &aws.Request{
// Mandatory to create an empty request, otherwise it panics
HTTPRequest: new(http.Request),
Expand All @@ -52,14 +52,14 @@ func TestS3_Size(t *testing.T) {
"it should retry if the first HEAD request return 404": {
expectMock: func(t *testing.T, m *s3mock.MockS3Client) {
m.EXPECT().HeadObjectRequest(&s3.HeadObjectInput{
Bucket: aws.String("bucket"), Key: aws.String("/key"),
Bucket: aws.String("bucket"), Key: aws.String("key"),
}).Return(s3.HeadObjectRequest{Request: &aws.Request{
HTTPRequest: new(http.Request),
Error: KeyNotFoundErr{},
}})

m.EXPECT().HeadObjectRequest(&s3.HeadObjectInput{
Bucket: aws.String("bucket"), Key: aws.String("/key"),
Bucket: aws.String("bucket"), Key: aws.String("key"),
}).Return(s3.HeadObjectRequest{Request: &aws.Request{
// Mandatory to create an empty request, otherwise it panics
HTTPRequest: new(http.Request),
Expand All @@ -70,7 +70,7 @@ func TestS3_Size(t *testing.T) {
"it should fail if the max amount of retried is passed": {
expectMock: func(t *testing.T, m *s3mock.MockS3Client) {
m.EXPECT().HeadObjectRequest(&s3.HeadObjectInput{
Bucket: aws.String("bucket"), Key: aws.String("/key"),
Bucket: aws.String("bucket"), Key: aws.String("key"),
}).Return(s3.HeadObjectRequest{Request: &aws.Request{
HTTPRequest: new(http.Request),
Error: KeyNotFoundErr{},
Expand Down
2 changes: 1 addition & 1 deletion storage/swift.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,5 @@ func (s *Swift) segmentPath(path string) (string, error) {
}

func (s *Swift) fullPath(path string) string {
return strings.TrimLeft(s.cfg.Prefix+"/"+path, "/")
return strings.TrimLeft(s.cfg.Prefix+"/"+fullPath(path), "/")
}

0 comments on commit e8553fe

Please sign in to comment.