How to list all S3 Buckets using Python boto3

To list the S3 Buckets inside an AWS Account, you will need to use the list_buckets() method of boto3.

Below are two example codes that you can use to retrieve all S3 buckets inside a Amazon Web Services account.

Both example scripts will do that same thing. It will query AWS for all the S3 Buckets inside the account and return the buckets names in a Python list.

Since both will do the same thing, you can use whichever method you prefer.

Example 1: Listing S3 Buckets using boto3 resource

import boto3

# Function that will get the list of S3 Buckets in an AWS Account
def get_s3_bucket_list():

    # Initialize boto3 S3 resource
    s3_resource = boto3.resource('s3')

    # Get the iterator from the S3 buckets collection
    s3_bucket_iterator = s3_resource.buckets.all()

    # Initialize the list of S3 Buckets to an empty list
    s3_bucket_list = []

    # Go over each of the S3 Buckets
    for s3_bucket in s3_bucket_iterator:

        # Get the name of the S3 Bucket
        s3_bucket_name = s3_bucket.name

        # Add the S3 Bucket name to the list
        s3_bucket_list.append(s3_bucket_name)

    # Return the list of S3 Buckets
    return s3_bucket_list


if __name__ == "__main__":

    # Call the method to retrieve the list of S3 Bucket names
    s3_bucket_list = get_s3_bucket_list()

    # Print each s3 bucket
    for s3_bucket_name in s3_bucket_list:
        print(s3_bucket_name)

Example 2: Listing S3 Buckets using boto3 client

import boto3

# Function that will get the list of S3 Buckets in an AWS Account
def get_s3_bucket_list():

    # Initialize boto3 to use s3 client
    s3_client = boto3.client('s3')

    # Initialize the list of S3 Buckets to an empty list
    s3_bucket_list = []
    
    # Get the list of S3 Buckets from AWS
    s3_response = s3_client.list_buckets()

    # Go over each of the S3 Buckets
    for s3_bucket in s3_response['Buckets']:

        # Get the name of the S3 Bucket
        s3_bucket_name = s3_bucket['Name']

        # Add the S3 Bucket name to the list
        s3_bucket_list.append(s3_bucket_name)

    # Return the list of S3 Buckets
    return s3_bucket_list


if __name__ == "__main__":

    # Call the method to retrieve the list of S3 Bucket names
    s3_bucket_list = get_s3_bucket_list()

    # Print each s3 bucket
    for s3_bucket_name in s3_bucket_list:
        print(s3_bucket_name)

Why does boto3.client(‘s3’) does not have paginators for listing S3 Buckets?

By default, an AWS Account can create a limit of 100 S3 Buckets. This limit can be increased up to a hard maximum limit of 1,000 S3 Buckets.

Since the hard limit is 1,000 S3 Buckets per AWS account, there is no need to paginate or do a NextContinuationToken for listing S3 Buckets. This is unlike listing the objects inside an S3 Bucket which will paginate if the objects that you are querying exceeds more than 1000 object keys.


We hope this helps you list your S3 Buckets in your AWS account using Python and boto3.

Let us know your experience in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.