How to use Boto3 library in Python to get the list of buckets present in AWS S3?

The Boto3 library is the AWS SDK for Python that allows you to interact with AWS services programmatically. You can use Boto3 to retrieve a list of all S3 buckets in your AWS account using either the resource or client interface.

Prerequisites

Before running the code, ensure you have:

  • Boto3 installed: pip install boto3
  • AWS credentials configured (via AWS CLI, environment variables, or IAM roles)
  • Appropriate S3 permissions to list buckets

Using S3 Resource Interface

The resource interface provides a higher-level, object-oriented approach to interact with S3 ?

import boto3
from botocore.exceptions import ClientError

def get_buckets_resource():
    """Get list of S3 buckets using resource interface"""
    session = boto3.session.Session()
    # User can pass customized access_key_id, secret_access_key and session_token
    s3_resource = session.resource('s3')
    
    try:
        buckets = list(s3_resource.buckets.all())
        print("Got buckets using resource:", [bucket.name for bucket in buckets])
        return buckets
    except ClientError as e:
        print(f"Couldn't get buckets: {e}")
        raise

# Call the function
bucket_list = get_buckets_resource()

Using S3 Client Interface

The client interface provides low-level access to AWS services and returns response data as dictionaries ?

import boto3
from botocore.exceptions import ClientError

def get_buckets_client():
    """Get list of S3 buckets using client interface"""
    s3_client = boto3.client('s3')
    
    try:
        response = s3_client.list_buckets()
        buckets = [bucket['Name'] for bucket in response['Buckets']]
        print("Got buckets using client:", buckets)
        return buckets
    except ClientError as e:
        print(f"Couldn't get buckets: {e}")
        raise

# Call the function
bucket_names = get_buckets_client()

Complete Example with Both Methods

import boto3
from botocore.exceptions import ClientError

def list_s3_buckets():
    """Compare resource and client methods for listing S3 buckets"""
    
    # Method 1: Using Resource
    print("=== Using S3 Resource ===")
    try:
        s3_resource = boto3.resource('s3')
        resource_buckets = [bucket.name for bucket in s3_resource.buckets.all()]
        print(f"Found {len(resource_buckets)} buckets:", resource_buckets)
    except ClientError as e:
        print(f"Resource method failed: {e}")
    
    # Method 2: Using Client
    print("\n=== Using S3 Client ===")
    try:
        s3_client = boto3.client('s3')
        response = s3_client.list_buckets()
        client_buckets = [bucket['Name'] for bucket in response['Buckets']]
        print(f"Found {len(client_buckets)} buckets:", client_buckets)
    except ClientError as e:
        print(f"Client method failed: {e}")

# Run the comparison
list_s3_buckets()

Sample Output

=== Using S3 Resource ===
Found 3 buckets: ['BUCKET_1', 'BUCKET_2', 'BUCKET_3']

=== Using S3 Client ===
Found 3 buckets: ['BUCKET_1', 'BUCKET_2', 'BUCKET_3']

Comparison

Method Interface Level Return Type Best For
Resource High-level Bucket objects Object-oriented operations
Client Low-level Dictionary response Raw API access

Conclusion

Both Boto3 resource and client interfaces can retrieve S3 bucket lists effectively. Use the resource interface for object-oriented operations or the client interface when you need direct API access and response metadata.

---
Updated on: 2026-03-25T18:08:27+05:30

490 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements