From 5afc55acb76a4988acd198a9df110b68b75f8ba1 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 11 Nov 2015 16:06:36 -0800 Subject: [PATCH] s3: Change s3 behavior - for virtual style requests filter buckets out. Fixes #1264 --- ls.go | 11 ++++---- pkg/client/s3/s3.go | 64 +++++++++++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/ls.go b/ls.go index 713b27a2..46fe16ff 100644 --- a/ls.go +++ b/ls.go @@ -31,7 +31,6 @@ import ( ) /// ls - related internal functions - const ( printDate = "2006-01-02 15:04:05 MST" ) @@ -102,18 +101,18 @@ func parseContent(c *client.Content) contentMessage { } // trimContent to fancify the output for directories -// -// TODO: Find a way to simplify this fancification. func trimContent(parentContent, childContent *client.Content, recursive bool) *client.Content { if recursive { - // If recursive remove the unnecessary '/', in the beginning + // If recursive remove the unnecessary parentContent prefix. '/', in the beginning trimmedContent := new(client.Content) trimmedContent = childContent - if strings.Index(childContent.URL.Path, string(childContent.URL.Separator)) == 0 { + if strings.HasSuffix(parentContent.URL.Path, string(parentContent.URL.Separator)) { + trimmedContent.URL.Path = strings.TrimPrefix(trimmedContent.URL.Path, parentContent.URL.Path) + } + if strings.Index(trimmedContent.URL.Path, string(trimmedContent.URL.Separator)) == 0 { if len(trimmedContent.URL.Path) > 0 { trimmedContent.URL.Path = trimmedContent.URL.Path[1:] } - return trimmedContent } return trimmedContent } diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index a77df1fc..3dae0656 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -33,9 +33,10 @@ import ( ) type s3Client struct { - mu *sync.Mutex - api minio.CloudStorageAPI - hostURL *client.URL + mu *sync.Mutex + api minio.CloudStorageAPI + hostURL *client.URL + virtualStyle bool } // New returns an initialized s3Client structure. if debug use a internal trace transport @@ -62,16 +63,18 @@ func New(config *client.Config) (client.Client, *probe.Error) { return minio.SignatureV4 }(), } - s3Conf.AccessKeyID = config.AccessKeyID - s3Conf.SecretAccessKey = config.SecretAccessKey - s3Conf.Transport = transport s3Conf.SetUserAgent(config.AppName, config.AppVersion, config.AppComments...) - s3Conf.Endpoint = u.Scheme + u.SchemeSeparator + u.Host api, err := minio.New(s3Conf) if err != nil { return nil, probe.NewError(err) } - return &s3Client{api: api, hostURL: u, mu: new(sync.Mutex)}, nil + s3Clnt := &s3Client{ + mu: new(sync.Mutex), + api: api, + hostURL: u, + virtualStyle: isVirtualHostStyle(u.Host), + } + return s3Clnt, nil } // GetURL get url @@ -259,6 +262,20 @@ func (c *s3Client) Stat() (*client.Content, *probe.Error) { return bucketMetadata, nil } +// Figure out if the URL is of 'virtual host' style. +// Currently only supported hosts with virtual style are Amazon S3 and Google Cloud Storage. +func isVirtualHostStyle(hostURL string) bool { + matchS3, _ := filepath.Match("*.s3*.amazonaws.com", hostURL) + if matchS3 { + return true + } + matchGoogle, _ := filepath.Match("*.storage.googleapis.com", hostURL) + if matchGoogle { + return true + } + return false +} + // url2BucketAndObject gives bucketName and objectName from URL path func (c *s3Client) url2BucketAndObject() (bucketName, objectName string) { path := c.hostURL.Path @@ -266,13 +283,7 @@ func (c *s3Client) url2BucketAndObject() (bucketName, objectName string) { // // For the time being this check is introduced for S3, // if you have custom virtual styled hosts please. list them below - matchS3, _ := filepath.Match("*.s3*.amazonaws.com", c.hostURL.Host) - if matchS3 { - hostSplits := strings.SplitN(c.hostURL.Host, ".", 2) - path = string(c.hostURL.Separator) + hostSplits[0] + c.hostURL.Path - } - matchGoogle, _ := filepath.Match("*.storage.googleapis.com", c.hostURL.Host) - if matchGoogle { + if c.virtualStyle { hostSplits := strings.SplitN(c.hostURL.Host, ".", 2) path = string(c.hostURL.Separator) + hostSplits[0] + c.hostURL.Path } @@ -353,7 +364,10 @@ func (c *s3Client) listIncompleteInRoutine(contentCh chan client.ContentOnChanne content := new(client.Content) url := *c.hostURL // join bucket with - incoming object key - url.Path = filepath.Join(b, object.Stat.Key) + url.Path = filepath.Join(string(url.Separator), b, object.Stat.Key) + if c.virtualStyle { + url.Path = filepath.Join(string(url.Separator), object.Stat.Key) + } switch { case strings.HasSuffix(object.Stat.Key, string(c.hostURL.Separator)): // We need to keep the trailing Separator, do not use filepath.Join() @@ -398,7 +412,7 @@ func (c *s3Client) listIncompleteRecursiveInRoutine(contentCh chan client.Conten } content := new(client.Content) url := *c.hostURL - url.Path = filepath.Join(bucket.Stat.Name, object.Stat.Key) + url.Path = filepath.Join(url.Path, bucket.Stat.Name, object.Stat.Key) content.URL = url content.Size = object.Stat.Size content.Time = object.Stat.Initiated @@ -420,7 +434,10 @@ func (c *s3Client) listIncompleteRecursiveInRoutine(contentCh chan client.Conten } url := *c.hostURL // join bucket and incoming object key - url.Path = filepath.Join(b, object.Stat.Key) + url.Path = filepath.Join(string(url.Separator), b, object.Stat.Key) + if c.virtualStyle { + url.Path = filepath.Join(string(url.Separator), object.Stat.Key) + } content := new(client.Content) content.URL = url content.Size = object.Stat.Size @@ -485,7 +502,10 @@ func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { content := new(client.Content) url := *c.hostURL // join bucket and incoming object key - url.Path = filepath.Join(b, object.Stat.Key) + url.Path = filepath.Join(string(url.Separator), b, object.Stat.Key) + if c.virtualStyle { + url.Path = filepath.Join(string(url.Separator), object.Stat.Key) + } switch { case strings.HasSuffix(object.Stat.Key, string(c.hostURL.Separator)): // We need to keep the trailing Separator, do not use filepath.Join() @@ -553,8 +573,12 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel } content := new(client.Content) url := *c.hostURL - // join bucket and incoming object key + // join bucket and incoming object key. url.Path = filepath.Join(string(url.Separator), b, object.Stat.Key) + // if virtualStyle replace the url.Path back. + if c.virtualStyle { + url.Path = filepath.Join(string(url.Separator), object.Stat.Key) + } content.URL = url content.Size = object.Stat.Size content.Time = object.Stat.LastModified