Israel Zinc
Israel Zinc

Reputation: 2769

Access a amazon s3 bucket subfolder using python

I am trying to access a bucket subfolder using python's boto3.

The problem is that I cannot find anywhere how to input the subfolder information inside the boto code.

All I find is how to put the bucket name, but I do not have access to the whole bucket, just to a specific subfolder. Can anyone give me a light?

What I did so far:

 BUCKET = "folder/subfolder"
 conn = S3Connection(AWS_KEY, AWS_SECRET)
 bucket = conn.get_bucket(BUCKET)
 for key in bucket.list():
     print key.name.encode('utf-8')

The error messages:

botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied

I do not need to use boto for the operation, I just need to list/get the files inside this subfolder.

P.S.: I can access the files using cyberduck by putting the path folder/subfolder, which means I have access to the date.

Sincerely,

Israel

Upvotes: 1

Views: 4159

Answers (2)

Israel Zinc
Israel Zinc

Reputation: 2769

I fixed the problem using something similar vtl suggested:

I had to put the prefix in my bucket and a delimiter. The final code was something like this:

objects = s3.list_objects(Bucket=bucketName, Prefix=bucketPath+'/', Delimiter='/')

As he said, there's not folder structure, then you have to state a delimiter and also put it after the Prefix like I did.

Thanks for the reply.

Upvotes: 2

vluzko
vluzko

Reputation: 335

Try:

for obj in bucket.objects.filter(Prefix="your_subfolder"):
    do_something()

AWS doesn't actually have a directory structure - it just fakes one by putting "/"s in names. The Prefix option restricts the search to all objects whose name starts with the given prefix, which should be your "subfolder".

Upvotes: 1

Related Questions