Skip to content

[S3] Trim left slashes #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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), "/")
}