How to use Boto3 and AWS Client to determine whether a root bucket exists in S3?

When working with AWS S3 using Python, you often need to verify if a bucket exists before performing operations. The Boto3 library provides the head_bucket() method to check bucket existence and access permissions.

Approach

The solution involves these key steps:

  • Step 1 − Import boto3 and botocore exceptions to handle errors
  • Step 2 − Create an AWS session using boto3 library
  • Step 3 − Create an AWS client for S3
  • Step 4 − Use head_bucket() which returns 200 OK if the bucket exists and you have access, or 403/404 for permission/existence issues
  • Step 5 − Handle exceptions based on response codes
  • Step 6 − Return True/False based on bucket existence

Example

The following code demonstrates how to check if a bucket exists in S3 ?

import boto3
from botocore.exceptions import ClientError

# Function to check whether bucket exists or not
def bucket_exists(bucket_name):
    try:
        session = boto3.session.Session()
        # User can pass customized access key, secret_key and token as well
        s3_client = session.client('s3')
        s3_client.head_bucket(Bucket=bucket_name)
        print("Bucket exists.", bucket_name)
        exists = True
    except ClientError as error:
        error_code = int(error.response['Error']['Code'])
        if error_code == 403:
            print("Private Bucket. Forbidden Access!", bucket_name)
        elif error_code == 404:
            print("Bucket Does Not Exist!", bucket_name)
        exists = False
    return exists

# Test the function
print(bucket_exists('bucket_1'))
print(bucket_exists('AWS_bucket_1'))

Output

Bucket exists. bucket_1
True
Bucket Does Not Exist! AWS_bucket_1
False

How It Works

The head_bucket() method performs a HEAD operation on the bucket. It returns different HTTP status codes:

  • 200 OK − Bucket exists and you have access
  • 403 Forbidden − Bucket exists but access is denied
  • 404 Not Found − Bucket does not exist

Alternative Approach

You can also use the list_buckets() method to check if a bucket exists ?

import boto3

def bucket_exists_alternative(bucket_name):
    s3_client = boto3.client('s3')
    buckets = s3_client.list_buckets()
    
    for bucket in buckets['Buckets']:
        if bucket['Name'] == bucket_name:
            return True
    return False

# Test alternative approach
print(bucket_exists_alternative('my-test-bucket'))

Conclusion

Use head_bucket() for efficient bucket existence checks as it only retrieves metadata. The list_buckets() approach works but is less efficient for large numbers of buckets.

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

609 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements