From 58269e73fc6bbf70ce82bdfae88ffdd58d6e5ff7 Mon Sep 17 00:00:00 2001 From: Andrey Kostov Date: Thu, 19 Feb 2015 16:31:34 -0800 Subject: [PATCH] Fix S3 driver's list when the root directory is either "" or "/" --- docs/storage/driver/s3/s3.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/storage/driver/s3/s3.go b/docs/storage/driver/s3/s3.go index eb9f08f49..d240c9018 100644 --- a/docs/storage/driver/s3/s3.go +++ b/docs/storage/driver/s3/s3.go @@ -587,6 +587,15 @@ func (d *driver) List(path string) ([]string, error) { if path != "/" && path[len(path)-1] != '/' { path = path + "/" } + + // This is to cover for the cases when the rootDirectory of the driver is either "" or "/". + // In those cases, there is no root prefix to replace and we must actually add a "/" to all + // results in order to keep them as valid paths as recognized by storagedriver.PathRegexp + prefix := "" + if d.s3Path("") == "" { + prefix = "/" + } + listResponse, err := d.Bucket.List(d.s3Path(path), "/", "", listMax) if err != nil { return nil, err @@ -597,11 +606,11 @@ func (d *driver) List(path string) ([]string, error) { for { for _, key := range listResponse.Contents { - files = append(files, strings.Replace(key.Key, d.s3Path(""), "", 1)) + files = append(files, strings.Replace(key.Key, d.s3Path(""), prefix, 1)) } for _, commonPrefix := range listResponse.CommonPrefixes { - directories = append(directories, strings.Replace(commonPrefix[0:len(commonPrefix)-1], d.s3Path(""), "", 1)) + directories = append(directories, strings.Replace(commonPrefix[0:len(commonPrefix)-1], d.s3Path(""), prefix, 1)) } if listResponse.IsTruncated {