Skip to content

Commit e8553fe

Browse files
committed
[S3] Trim left slashes
1 parent d035717 commit e8553fe

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

storage/s3.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package storage
33
import (
44
"context"
55
"io"
6+
"strings"
67
"time"
78

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

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

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

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

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

183188
return config
184189
}
190+
191+
func fullPath(path string) string {
192+
return strings.TrimLeft("/"+path, "/")
193+
}

storage/s3_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestS3_Size(t *testing.T) {
4141
"it should make a HEAD request on the object": {
4242
expectMock: func(t *testing.T, m *s3mock.MockS3Client) {
4343
m.EXPECT().HeadObjectRequest(&s3.HeadObjectInput{
44-
Bucket: aws.String("bucket"), Key: aws.String("/key"),
44+
Bucket: aws.String("bucket"), Key: aws.String("key"),
4545
}).Return(s3.HeadObjectRequest{Request: &aws.Request{
4646
// Mandatory to create an empty request, otherwise it panics
4747
HTTPRequest: new(http.Request),
@@ -52,14 +52,14 @@ func TestS3_Size(t *testing.T) {
5252
"it should retry if the first HEAD request return 404": {
5353
expectMock: func(t *testing.T, m *s3mock.MockS3Client) {
5454
m.EXPECT().HeadObjectRequest(&s3.HeadObjectInput{
55-
Bucket: aws.String("bucket"), Key: aws.String("/key"),
55+
Bucket: aws.String("bucket"), Key: aws.String("key"),
5656
}).Return(s3.HeadObjectRequest{Request: &aws.Request{
5757
HTTPRequest: new(http.Request),
5858
Error: KeyNotFoundErr{},
5959
}})
6060

6161
m.EXPECT().HeadObjectRequest(&s3.HeadObjectInput{
62-
Bucket: aws.String("bucket"), Key: aws.String("/key"),
62+
Bucket: aws.String("bucket"), Key: aws.String("key"),
6363
}).Return(s3.HeadObjectRequest{Request: &aws.Request{
6464
// Mandatory to create an empty request, otherwise it panics
6565
HTTPRequest: new(http.Request),
@@ -70,7 +70,7 @@ func TestS3_Size(t *testing.T) {
7070
"it should fail if the max amount of retried is passed": {
7171
expectMock: func(t *testing.T, m *s3mock.MockS3Client) {
7272
m.EXPECT().HeadObjectRequest(&s3.HeadObjectInput{
73-
Bucket: aws.String("bucket"), Key: aws.String("/key"),
73+
Bucket: aws.String("bucket"), Key: aws.String("key"),
7474
}).Return(s3.HeadObjectRequest{Request: &aws.Request{
7575
HTTPRequest: new(http.Request),
7676
Error: KeyNotFoundErr{},

storage/swift.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,5 @@ func (s *Swift) segmentPath(path string) (string, error) {
118118
}
119119

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

0 commit comments

Comments
 (0)