From 99cd1ccfe5e4f264974a609dbde434f638a7f06c Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Mon, 23 Nov 2020 15:36:42 +0100 Subject: [PATCH] S3: fix empty directory detection when listing empty directory MinIO returns no contents while S3 returns 1 object with the key equal to the prefix. Make detection work in both cases Fixes #227 --- vfs/s3fs.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/vfs/s3fs.go b/vfs/s3fs.go index 8bdd309c..f77a7677 100644 --- a/vfs/s3fs.go +++ b/vfs/s3fs.go @@ -652,7 +652,7 @@ func (fs *S3Fs) hasContents(name string) (bool, error) { prefix += "/" } } - maxResults := int64(1) + maxResults := int64(2) ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(fs.ctxTimeout)) defer cancelFn() results, err := fs.svc.ListObjectsV2WithContext(ctx, &s3.ListObjectsV2Input{ @@ -664,7 +664,16 @@ func (fs *S3Fs) hasContents(name string) (bool, error) { if err != nil { return false, err } - return len(results.Contents) > 0, nil + // MinIO returns no contents while S3 returns 1 object + // with the key equal to the prefix for empty directories + for _, obj := range results.Contents { + name, _ := fs.resolve(obj.Key, prefix) + if name == "" || name == "/" { + continue + } + return true, nil + } + return false, nil } func (fs *S3Fs) headObject(name string) (*s3.HeadObjectOutput, error) {