Fix recursive listing and deletion in S3 gateway (#333)

* Fix recursive listing and deletion in S3 gateway

* Prepend the prefix only on recursive listing

* Fix tests
This commit is contained in:
Kaloyan Raev 2018-09-10 00:51:33 +03:00 committed by JT Olio
parent 555e7f1897
commit 20863e6bdf
2 changed files with 32 additions and 13 deletions

View File

@ -206,26 +206,32 @@ func (s *storjObjects) ListObjects(ctx context.Context, bucket, prefix, marker,
} }
startAfter := paths.New(marker) startAfter := paths.New(marker)
recursive := delimiter == ""
var objects []minio.ObjectInfo var objects []minio.ObjectInfo
var prefixes []string var prefixes []string
o, err := s.storj.bs.GetObjectStore(ctx, bucket) o, err := s.storj.bs.GetObjectStore(ctx, bucket)
if err != nil { if err != nil {
return minio.ListObjectsInfo{}, err return minio.ListObjectsInfo{}, err
} }
items, more, err := o.List(ctx, paths.New(prefix), startAfter, nil, delimiter == "", maxKeys, meta.All) items, more, err := o.List(ctx, paths.New(prefix), startAfter, nil, recursive, maxKeys, meta.All)
if err != nil { if err != nil {
return result, err return result, err
} }
if len(items) > 0 { if len(items) > 0 {
for _, item := range items { for _, item := range items {
path := item.Path
if recursive {
path = path.Prepend(prefix)
}
if item.IsPrefix { if item.IsPrefix {
prefixes = append(prefixes, item.Path.String()+"/") prefixes = append(prefixes, path.String()+"/")
continue continue
} }
objects = append(objects, minio.ObjectInfo{ objects = append(objects, minio.ObjectInfo{
Bucket: bucket, Bucket: bucket,
IsDir: false, IsDir: false,
Name: item.Path.String(), Name: path.String(),
ModTime: item.Meta.Modified, ModTime: item.Meta.Modified,
Size: item.Meta.Size, Size: item.Meta.Size,
ContentType: item.Meta.ContentType, ContentType: item.Meta.ContentType,

View File

@ -356,24 +356,37 @@ func TestListObjects(t *testing.T) {
{Path: paths.New("test-file-2.txt")}, {Path: paths.New("test-file-2.txt")},
} }
objInfos := []minio.ObjectInfo{
{Bucket: bucket, Name: path.Join("test-file-1.txt")},
{Bucket: bucket, Name: path.Join("test-file-2.txt")},
}
for i, example := range []struct { for i, example := range []struct {
more bool more bool
startAfter string startAfter string
nextMarker string nextMarker string
delimiter string delimiter string
recursive bool recursive bool
objInfos []minio.ObjectInfo
err error err error
errString string errString string
}{ }{
{false, "", "", "", true, nil, ""}, {
{true, "test-start-after", "test-file-2.txt", "/", false, nil, ""}, more: false, startAfter: "", nextMarker: "", delimiter: "", recursive: true,
// mock returning non-nil error objInfos: []minio.ObjectInfo{
{false, "", "", "", true, Error.New("error"), "Storj Gateway error: error"}, {Bucket: bucket, Name: path.Join("test-prefix/test-file-1.txt")},
{Bucket: bucket, Name: path.Join("test-prefix/test-file-2.txt")},
}, err: nil, errString: "",
},
{
more: true, startAfter: "test-start-after", nextMarker: "test-file-2.txt", delimiter: "/", recursive: false,
objInfos: []minio.ObjectInfo{
{Bucket: bucket, Name: path.Join("test-file-1.txt")},
{Bucket: bucket, Name: path.Join("test-file-2.txt")},
}, err: nil, errString: "",
},
{
more: false, startAfter: "", nextMarker: "", delimiter: "", recursive: true,
objInfos: []minio.ObjectInfo{
{Bucket: bucket, Name: path.Join("test-prefix/test-file-1.txt")},
{Bucket: bucket, Name: path.Join("test-prefix/test-file-2.txt")},
}, err: Error.New("error"), errString: "Storj Gateway error: error",
},
} { } {
errTag := fmt.Sprintf("Test case #%d", i) errTag := fmt.Sprintf("Test case #%d", i)
@ -393,7 +406,7 @@ func TestListObjects(t *testing.T) {
assert.NotNil(t, listInfo, errTag) assert.NotNil(t, listInfo, errTag)
assert.Equal(t, example.more, listInfo.IsTruncated, errTag) assert.Equal(t, example.more, listInfo.IsTruncated, errTag)
assert.Equal(t, example.nextMarker, listInfo.NextMarker, errTag) assert.Equal(t, example.nextMarker, listInfo.NextMarker, errTag)
assert.Equal(t, objInfos, listInfo.Objects, errTag) assert.Equal(t, example.objInfos, listInfo.Objects, errTag)
assert.Nil(t, listInfo.Prefixes, errTag) assert.Nil(t, listInfo.Prefixes, errTag)
} }
} }