Non-recursive listing in Minio Gateway (#327)

* Non-recursive listing in Minio Gateway

* Cleaner code

* Fix tests
This commit is contained in:
Kaloyan Raev 2018-09-08 20:10:58 +03:00 committed by JT Olio
parent c68cd1232d
commit 2a1a52acc2
2 changed files with 31 additions and 22 deletions

View File

@ -200,38 +200,46 @@ func (s *storjObjects) ListBuckets(ctx context.Context) (
func (s *storjObjects) ListObjects(ctx context.Context, bucket, prefix, marker,
delimiter string, maxKeys int) (result minio.ListObjectsInfo, err error) {
defer mon.Task()(&ctx)(&err)
if delimiter != "" && delimiter != "/" {
return minio.ListObjectsInfo{}, Error.New("delimiter %s not supported", delimiter)
}
startAfter := paths.New(marker)
var fl []minio.ObjectInfo
var objects []minio.ObjectInfo
var prefixes []string
o, err := s.storj.bs.GetObjectStore(ctx, bucket)
if err != nil {
return minio.ListObjectsInfo{}, err
}
items, more, err := o.List(ctx, paths.New(prefix), startAfter, nil, true, maxKeys, meta.All)
items, more, err := o.List(ctx, paths.New(prefix), startAfter, nil, delimiter == "", maxKeys, meta.All)
if err != nil {
return result, err
}
if len(items) > 0 {
//Populate the objectlist (aka filelist)
f := make([]minio.ObjectInfo, len(items))
for i, fi := range items {
f[i] = minio.ObjectInfo{
Bucket: bucket,
IsDir: fi.IsPrefix,
Name: fi.Path.String(),
ModTime: fi.Meta.Modified,
Size: fi.Meta.Size,
ContentType: fi.Meta.ContentType,
UserDefined: fi.Meta.UserDefined,
ETag: fi.Meta.Checksum,
for _, item := range items {
if item.IsPrefix {
prefixes = append(prefixes, item.Path.String()+"/")
continue
}
objects = append(objects, minio.ObjectInfo{
Bucket: bucket,
IsDir: false,
Name: item.Path.String(),
ModTime: item.Meta.Modified,
Size: item.Meta.Size,
ContentType: item.Meta.ContentType,
UserDefined: item.Meta.UserDefined,
ETag: item.Meta.Checksum,
})
}
startAfter = items[len(items)-1].Path[len(paths.New(prefix)):]
fl = f
}
result = minio.ListObjectsInfo{
IsTruncated: more,
Objects: fl,
Objects: objects,
Prefixes: prefixes,
}
if more {
result.NextMarker = startAfter.String()

View File

@ -349,7 +349,6 @@ func TestListObjects(t *testing.T) {
bucket := "test-bucket"
prefix := "test-prefix"
delimiter := "test-delimiter"
maxKeys := 123
items := []objects.ListItem{
@ -376,21 +375,23 @@ func TestListObjects(t *testing.T) {
more bool
startAfter string
nextMarker string
delimiter string
recursive bool
err error
errString string
}{
{false, "", "", nil, ""},
{true, "test-start-after", "test-file-2.txt", nil, ""},
{false, "", "", "", true, nil, ""},
{true, "test-start-after", "test-file-2.txt", "/", false, nil, ""},
// mock returning non-nil error
{false, "", "", Error.New("error"), "Storj Gateway error: error"},
{false, "", "", "", true, Error.New("error"), "Storj Gateway error: error"},
} {
errTag := fmt.Sprintf("Test case #%d", i)
mockBS.EXPECT().GetObjectStore(gomock.Any(), bucket).Return(mockOS, nil)
mockOS.EXPECT().List(gomock.Any(), paths.New(prefix), paths.New(example.startAfter),
nil, true, maxKeys, meta.All).Return(items, example.more, example.err)
nil, example.recursive, maxKeys, meta.All).Return(items, example.more, example.err)
listInfo, err := storjObj.ListObjects(ctx, bucket, prefix, example.startAfter, delimiter, maxKeys)
listInfo, err := storjObj.ListObjects(ctx, bucket, prefix, example.startAfter, example.delimiter, maxKeys)
if err != nil {
assert.EqualError(t, err, example.errString, errTag)