Reputation: 31
I have a requirement where I need to list all objects in a s3 bucket and their storage classes.
How can I achieve this with Python
and Boto3
? I use Python 3.7
.
Upvotes: 0
Views: 5917
Reputation: 269330
This code will list all objects in a given bucket, displaying the object name (Key) and Storage Class. This code uses the resource method of accessing Amazon S3.
import boto3
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket('my-bucket')
for object in bucket.objects.all():
print(object.key, object.storage_class)
Upvotes: 4
Reputation: 20042
Do yourself a favor and use S3 Inventory Report.
Amazon S3 inventory is one of the tools Amazon S3 provides to help manage your storage. You can also simplify and speed up business workflows and big data jobs using Amazon S3 inventory, which provides a scheduled alternative to the Amazon S3 synchronous List API operation.
One of the items that's listed in the report is:
Here's the full list of what's in the report.
Finally, you can use S3 Inventory REST API.
Upvotes: 3