Using boto3, how can I retrieve all files in my S3 bucket without retrieving the folders?
Consider the following file structure:
file_1.txt
folder_1/
As stated in the other answers, s3 does not actually have directories trees. But there is a convenient workaround taking advantage of the fact that the s3 "folders" have zero size by using paginators. This code-snippet will print out the desired output if all your files in the bucket have size > 0 (of course you need to adapt your region) :
bucket_name = "bucketname"
s3 = boto3.client('s3', region_name='eu-central-1')
paginator = s3.get_paginator('list_objects')
[print(page['Key']) for page in paginator.paginate(Bucket=bucket_name).search("Contents[?Size > `0`][]")]
The filtering is done using JMESPath.
Note: Of course this would also exclude files with size 0, but usually you don't need storage for empty files.