From 74a496e44ad196feff294f659e9c4d9a3724f53b Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 30 Jun 2019 05:33:34 +0200 Subject: [PATCH] Add prefix sync for s3 buckets Signed-off-by: Knut Ahlers --- providers/s3/file.go | 4 +++- providers/s3/provider.go | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/providers/s3/file.go b/providers/s3/file.go index 8f31d41..0fc8075 100644 --- a/providers/s3/file.go +++ b/providers/s3/file.go @@ -5,6 +5,7 @@ import ( "fmt" "hash" "io" + "strings" "time" "github.com/aws/aws-sdk-go/aws" @@ -22,11 +23,12 @@ type File struct { s3Conn *s3.S3 bucket string + prefix string } func (f File) Info() providers.FileInfo { return providers.FileInfo{ - RelativeName: f.key, + RelativeName: strings.Trim(strings.TrimPrefix(f.key, f.prefix), "/"), LastModified: f.lastModified, Checksum: f.checksum, Size: f.size, diff --git a/providers/s3/provider.go b/providers/s3/provider.go index f59c302..194f110 100644 --- a/providers/s3/provider.go +++ b/providers/s3/provider.go @@ -24,6 +24,7 @@ type Provider struct { bucket string bucketRegion string defaultACL string + prefix string s3 *s3.S3 } @@ -56,6 +57,7 @@ func New(uri string) (providers.CloudProvider, error) { bucket: u.Host, bucketRegion: region, defaultACL: s3.ObjectCannedACLPrivate, + prefix: strings.Trim(u.Path, "/"), s3: svc, } @@ -75,7 +77,7 @@ func (p *Provider) GetChecksumMethod() hash.Hash { return md5.New() } func (p *Provider) DeleteFile(relativeName string) error { _, err := p.s3.DeleteObject(&s3.DeleteObjectInput{ Bucket: aws.String(p.bucket), - Key: aws.String(relativeName), + Key: p.relativeNameToKey(relativeName), }) return errors.Wrap(err, "Unable to delete object") @@ -84,7 +86,7 @@ func (p *Provider) DeleteFile(relativeName string) error { func (p *Provider) GetFile(relativeName string) (providers.File, error) { resp, err := p.s3.HeadObject(&s3.HeadObjectInput{ Bucket: aws.String(p.bucket), - Key: aws.String(relativeName), + Key: p.relativeNameToKey(relativeName), }) if err != nil { return nil, errors.Wrap(err, "Unable to fetch head information") @@ -106,6 +108,7 @@ func (p *Provider) ListFiles() ([]providers.File, error) { err := p.s3.ListObjectsPages(&s3.ListObjectsInput{ Bucket: aws.String(p.bucket), + Prefix: aws.String(p.prefix), }, func(out *s3.ListObjectsOutput, lastPage bool) bool { for _, obj := range out.Contents { files = append(files, File{ @@ -116,6 +119,7 @@ func (p *Provider) ListFiles() ([]providers.File, error) { s3Conn: p.s3, bucket: p.bucket, + prefix: p.prefix, }) } @@ -141,7 +145,7 @@ func (p *Provider) PutFile(f providers.File) (providers.File, error) { ACL: aws.String(p.getFileACL(f.Info().RelativeName)), Body: bytes.NewReader(buf.Bytes()), Bucket: aws.String(p.bucket), - Key: aws.String(f.Info().RelativeName), + Key: p.relativeNameToKey(f.Info().RelativeName), }); err != nil { return nil, errors.Wrap(err, "Unable to write file") } @@ -153,7 +157,7 @@ func (p *Provider) Share(relativeName string) (string, error) { _, err := p.s3.PutObjectAcl(&s3.PutObjectAclInput{ ACL: aws.String(s3.ObjectCannedACLPublicRead), Bucket: aws.String(p.bucket), - Key: aws.String(relativeName), + Key: p.relativeNameToKey(relativeName), }) if err != nil { return "", errors.Wrap(err, "Unable to publish file") @@ -165,7 +169,7 @@ func (p *Provider) Share(relativeName string) (string, error) { func (p *Provider) getFileACL(relativeName string) string { objACL, err := p.s3.GetObjectAcl(&s3.GetObjectAclInput{ Bucket: aws.String(p.bucket), - Key: aws.String(relativeName), + Key: p.relativeNameToKey(relativeName), }) if err != nil { @@ -183,3 +187,8 @@ func (p *Provider) getFileACL(relativeName string) string { return p.defaultACL } + +func (p Provider) relativeNameToKey(relativeName string) *string { + key := strings.Join([]string{p.prefix, relativeName}, "/") + return &key +}