Lenny D.
Lenny D.

Reputation: 298

Can positively check S3 bucket existence but won't be able to list its objects

I'm building my S3 buckets like nested directories in order to store files by some criteria, and the first thing that I noticed is that, for example, take into account that sample only stores those test=xxx keys which are the ones that in final instance store files.

s3Client.doesBucketExistV2("one/two/sample") // true
s3Client.doesBucketExistV2("one/two/sample/test=123") // false

Both exist and test=xxx contains files but they weren't created manually by me in AWS, but by a program.

Why test=xxx doesn't returntrueinstead offalse?

And my second doubt is when trying to list objects for a given bucket...

s3Client.listObjects(new ListObjectsRequest()
   .withBucketName("one/")
   .withPrefix("two/")
   .withPrefix("sample/")) // The specified key does not exist. [404]

Why can't I list the objects of a given bucket that exists?

Upvotes: 1

Views: 862

Answers (2)

nologin
nologin

Reputation: 1452

the second .withPrefix("sample/") overrides .withPrefix("two/"). It does not concatenate the strings.

The bucketname, prefix and key are separate things. So doesBucketExistV2() proofs in the last case on a key.

Your bucketname is: one
Your prefix is: /two/
or Another prefix is: /two/sample/
with the key: "test=xxx"


s3Client.listObjects(new ListObjectsRequest()
   .withBucketName("one")
   .withPrefix("/two/sample/");

Upvotes: 1

Zusman
Zusman

Reputation: 666

Maybe try this:

s3 = boto3.client("s3")
list_of_files = s3.list_objects_v2(Bucket=your-bucket)['Contents']

Upvotes: 0

Related Questions