Home > Software engineering >  boto3 list_objects_v2 returns all recursive folders and throws no error
boto3 list_objects_v2 returns all recursive folders and throws no error

Time:10-26

I am new to aws and currently reading objects from S3 bucket.

import boto3

s3 = boto3.client('s3')

bucket = 'my_bucket'
key = 'folder/10'
contents = s3.list_objects_v2(Bucket=f"{bucket}", Prefix=key)
contents.get('Contents')

The bucket does not contain any folder as folder/10 instead it contains folder/100. list_objects_v2 and list_objects recursively patches the path and returns Key as folder/100 in the Contents dict.

How do I make sure it gives me an exception error if the path my_bucket/folder/10 does not exist in S3. ?

Thanks for the help.

CodePudding user response:

If you are saying that 10 is the name of a 'folder', and you only wish to list the contents of that folder, then use: Prefix='folder/10/

Please note that folders do not actually exist in Amazon S3. For example, you could upload a file to invoices/january.txt even if the invoices folder does not exist. That folder will then 'magically' appear! Then, if the object is deleted, the folder will magically 'disappear' because it never actually existed.

The Key (filename) of an Amazon S3 object includes the full path to the object, so that object is called invoices/january.txt, not just january.txt inside a folder.

If no objects are found in a given prefix, then an empty result set will be returned. It will not trigger an exception, although you could use the raise command in your code to generate an exception in such a situation.

  • Related