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 82a453d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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, "/")
}
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 82a453d

Please sign in to comment.