One way to see the contents would be:
for my_bucket_object in my_bucket.objects.all():
print(my_bucket_object)
Answer from garnaat on Stack OverflowOne way to see the contents would be:
for my_bucket_object in my_bucket.objects.all():
print(my_bucket_object)
This is similar to an 'ls' but it does not take into account the prefix folder convention and will list the objects in the bucket. It's left up to the reader to filter out prefixes which are part of the Key name.
In Python 2:
from boto.s3.connection import S3Connection
conn = S3Connection() # assumes boto.cfg setup
bucket = conn.get_bucket('bucket_name')
for obj in bucket.get_all_keys():
print(obj.key)
In Python 3:
from boto3 import client
conn = client('s3') # again assumes boto.cfg setup, assume AWS S3
for key in conn.list_objects(Bucket='bucket_name')['Contents']:
print(key['Key'])
listing the top level contents of a s3 bucket with Prefix and Delimiter
Most efficient (cost/speed) to check if multiple files are in a S3 "folder" with Python/Boto3
Using boto3 to list only all key names of files in a bucket - best methods when the bucket is ridiculously big?
python - Retrieving subfolders names in S3 bucket from Boto3 - Stack Overflow
Till now, to check the names or last_modified details of all objects in a bucket, what I did was the following:
import boto3
s3r = boto3.resource('s3', aws_access_key_id=access_key_id, aws_secret_access_key=access_key_secret)
bucket_items = list(s3r.Bucket('BucketName').objects.all())
However, this seems to be a terrible idea for handling it when the bucket gets too big. Running this on a machine with 8GB RAM threw a memory error!
I assume I have to be doing something wrong if only fetching the file keys is this difficult. Is the objects.all() method actually saving details of the file's contents into memory when I call it?
Below piece of code returns ONLY the 'subfolders' in a 'folder' from s3 bucket.
import boto3
bucket = 'my-bucket'
#Make sure you provide / in the end
prefix = 'prefix-name-with-slash/'
client = boto3.client('s3')
result = client.list_objects(Bucket=bucket, Prefix=prefix, Delimiter='/')
for o in result.get('CommonPrefixes'):
print 'sub folder : ', o.get('Prefix')
For more details, you can refer to https://github.com/boto/boto3/issues/134
S3 is an object storage, it doesn't have real directory structure. The "/" is rather cosmetic. One reason that people want to have a directory structure, because they can maintain/prune/add a tree to the application. For S3, you treat such structure as sort of index or search tag.
To manipulate object in S3, you need boto3.client or boto3.resource, e.g. To list all object
import boto3
s3 = boto3.client("s3")
all_objects = s3.list_objects(Bucket = 'bucket-name')
http://boto3.readthedocs.org/en/latest/reference/services/s3.html#S3.Client.list_objects
In fact, if the s3 object name is stored using '/' separator. The more recent version of list_objects (list_objects_v2) allows you to limit the response to keys that begin with the specified prefix.
To limit the items to items under certain sub-folders:
import boto3
s3 = boto3.client("s3")
response = s3.list_objects_v2(
Bucket=BUCKET,
Prefix ='DIR1/DIR2',
MaxKeys=100 )
Documentation
Another option is using python os.path function to extract the folder prefix. Problem is that this will require listing objects from undesired directories.
import os
s3_key = 'first-level/1456753904534/part-00014'
filename = os.path.basename(s3_key)
foldername = os.path.dirname(s3_key)
# if you are not using conventional delimiter like '#'
s3_key = 'first-level#1456753904534#part-00014'
filename = s3_key.split("#")[-1]
A reminder about boto3 : boto3.resource is a nice high level API. There are pros and cons using boto3.client vs boto3.resource. If you develop internal shared library, using boto3.resource will give you a blackbox layer over the resources used.
Lets say that My_bucket are the bucket that are public and there are /Cupcake/Vanilla/ subfolders. how do you get the most recent created subfolder on boto3?