Ad

Why Does Listing Objects In An S3 Bucket's Folder Using AWS SDK For JavaScript Returns Empty Contents Array But KeyCount Greater Than One?

- 1 answer

I have a bucket in S3 called myBucketName. It has one folder called myFolderName and is containing one file myFile.

I try to list the objects in the folder like so:

s3.listObjectsV2({       
  Bucket: 'myBucketName',
  Delimiter: "/",
  Prefix: 'myFolderName',
})

With the response

{
  IsTruncated: false,
  Contents: [],    
  Name: 'myBucketName',
  Prefix: 'myFolderName',
  Delimiter: '/',
  MaxKeys: 1000,
  CommonPrefixes: [ { Prefix: 'myFolderName/' } ],
  KeyCount: 1 
}

How come the Contents array is empty even though KeyCount is 1? I would have expected to contain one item with Key == 'myFolderName/myFile'

What should I do to get the desired result, i.e. a list of the objects in the folder?

Ad

Answer

Use Prefix: 'myFolderName/' (with the forward slash /) when listing the objects. It's telling you there is only one "object" (a folder) in the folder you're asking about.

Ad
source: stackoverflow.com
Ad