bhargav
bhargav

Reputation: 31

List all objects in AWS S3 bucket with their storage class using Boto3 Python

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

Answers (2)

John Rotenstein
John Rotenstein

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

baduker
baduker

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:

  • Storage class – Storage class used for storing the object

Here's the full list of what's in the report.

Finally, you can use S3 Inventory REST API.

Upvotes: 3

Related Questions