---
title: "List S3 Buckets"
lang: "en"
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/post/python-list-s3-buckets
---

![Blog post image for List S3 Buckets - Automate AWS S3 interactions. This Python snippet uses Boto3 to easily list all S3 buckets in your account.](/_astro/hero.BsiJ6hry_ZCl3Cs.webp)

[Home](/)›[Codesnippets](/codesnippets)›[All Categories](/codesnippets/categories)›[Aws](/codesnippets/categories/aws)

Codesnippets

[Prev in AwsAWS DynamoDB CRUD Operations in Node.js with the AWS SDK v3](/codesnippets/post/nodejs-dynamodb-crud-aws-sdk-v3)

[Aws](/codesnippets/categories/aws)[Python](/codesnippets/categories/python)[Devops](/codesnippets/categories/devops)[Python](/codesnippets/python)

# List S3 Buckets

[Mohammad Abu Mattar](/authors/mohammad-abu-mattar)Published: 25 Jun 202502 Mins read03 Mins listen

[Markdown for AI(opens in a new tab)](/post/python-list-s3-buckets/index.md "Open the plain-Markdown version of this page, for pasting into an AI tool")

TL;DR

Automate AWS S3 interactions. This Python snippet uses Boto3 to easily list all S3 buckets in your account.

Series

[AWS Automation](/series/aws-automation)3/4

[PreviousAWS Secrets Manager](/codesnippets/post/nodejs-aws-secrets-manager)[NextAWS EC2 Instance Management with Boto3: Start, Stop, and Query Instances](/codesnippets/post/aws-ec2-instance-management-boto3-python)

All posts in this series (4)

Code Snippets4

1.  [Check S3 Bucket Existence](/codesnippets/post/bash-s3-bucket-exists)
2.  [AWS Secrets Manager](/codesnippets/post/nodejs-aws-secrets-manager)
3.  [List S3 BucketsYou are here](/codesnippets/post/python-list-s3-buckets)
4.  [AWS EC2 Instance Management with Boto3: Start, Stop, and Query Instances](/codesnippets/post/aws-ec2-instance-management-boto3-python)

### List S3 Buckets

Contents

[Overview](#overview)[Multi-profile S3 management](#multi-profile-s3-management)[Use case](#use-case)[The Problem](#the-problem)[Multi-profile complexity](#multi-profile-complexity)[Manual process inefficiencies](#manual-process-inefficiencies)[The Solution](#the-solution)[Automated profile discovery](#automated-profile-discovery)[Detailed error handling](#detailed-error-handling)[Features](#features)[Key capabilities](#key-capabilities)[Output format](#output-format)[Code implementation](#code-implementation)[Dependencies and setup](#dependencies-and-setup)[Profile discovery function](#profile-discovery-function)[Bucket listing function](#bucket-listing-function)[Main display function](#main-display-function)[Benefits and usage](#benefits-and-usage)[Operational advantages](#operational-advantages)[Use cases](#use-cases)[Community Discussion](#community-discussion)[Your S3 management approaches](#your-s3-management-approaches)[Alternative tools](#alternative-tools)

## [Overview](#overview)

### [Multi-profile S3 management](#multi-profile-s3-management)

Ever juggled multiple AWS accounts and needed a quick S3 bucket inventory across all of them? This Python script handles it.

### [Use case](#use-case)

Perfect for organizations managing multiple AWS accounts or developers working with different IAM roles and profiles.

## [The Problem](#the-problem)

### [Multi-profile complexity](#multi-profile-complexity)

When you’re working with multiple AWS accounts or IAM roles through different profiles, getting a consolidated view of your S3 buckets can be a hassle. Switching contexts or running commands repeatedly for each profile is slow and easy to get wrong. A full inventory or audit turns into a chore.

### [Manual process inefficiencies](#manual-process-inefficiencies)

Traditional approaches require manual switching between profiles and running separate commands for each account.

## [The Solution](#the-solution)

### [Automated profile discovery](#automated-profile-discovery)

This Python script uses Boto3 to discover all your configured AWS CLI profiles. It then iterates through each profile, attempting to list its S3 buckets. It prints a profile-by-profile breakdown and handles errors like missing credentials or access denied on a single profile, so the run finishes even in complex setups.

### [Detailed error handling](#detailed-error-handling)

Gracefully handles various error conditions including missing credentials, access denied, and network issues.

## [Features](#features)

### [Key capabilities](#key-capabilities)

**TL;DR**

-   Lists S3 buckets across all configured AWS CLI profiles in one go.
-   Automatically discovers available AWS profiles.
-   Prints a per-profile listing of S3 buckets.
-   Handles errors for individual profiles (e.g., credential issues, access denied).

### [Output format](#output-format)

Organized display showing buckets grouped by AWS profile with clear visual separation.

## [Code implementation](#code-implementation)

### [Dependencies and setup](#dependencies-and-setup)

list\_s3\_buckets.py

```
1import boto32from botocore.exceptions import NoCredentialsError, ClientError
```

### [Profile discovery function](#profile-discovery-function)

```
1def get_aws_profiles():2    """Get all configured AWS profiles from the AWS CLI configuration."""3    try:4        session = boto3.Session()5        return session.available_profiles6    except Exception as e:7        print(f"Error getting AWS profiles: {str(e)}")8        return []
```

### [Bucket listing function](#bucket-listing-function)

```
1def list_s3_buckets_for_profile(profile_name):2    """3    Lists all S3 buckets for a specific AWS profile.4    Returns a list of bucket names or an empty list if an error occurs.5    """6    buckets = []7    try:8        session = boto3.Session(profile_name=profile_name)9        s3_client = session.client('s3')10        response = s3_client.list_buckets()11        if response['Buckets']:12            for bucket in response['Buckets']:13                buckets.append(bucket['Name'])14    except NoCredentialsError:15        print(f"  Warning: No credentials found for profile '{profile_name}'. Skipping.")16    except ClientError as e:17        error_code = e.response.get("Error", {}).get("Code")18        error_message = e.response.get("Error", {}).get("Message")19        print(f"  Warning: AWS Client Error for profile '{profile_name}' ({error_code}): {error_message}. Skipping.")20    except Exception as e:21        print(f"  Warning: An unexpected error occurred for profile '{profile_name}': {e}. Skipping.")22    return buckets
```

### [Main display function](#main-display-function)

```
1def display_all_s3_buckets_by_profile():2    """3    Fetches and displays S3 buckets for all configured AWS profiles.4    """5    profiles = get_aws_profiles()6
7    if not profiles:8        print("No AWS profiles found. Please configure your AWS CLI.")9        return10
11    print(f"\nChecking S3 Buckets across {len(profiles)} AWS Profiles:")12    print("-" * 40)13
14    for profile in sorted(profiles):15        print(f"\nProfile: {profile}")16        print("  S3 Buckets:")17        buckets = list_s3_buckets_for_profile(profile)18        if buckets:19            for bucket_name in buckets:20                print(f"    - {bucket_name}")21        else:22            print("    No buckets or inaccessible for this profile.")23        print("-" * 40)24
25    if not any(list_s3_buckets_for_profile(p) for p in profiles):26        print("\nNo S3 buckets found across any configured profiles or all were inaccessible.")27
28
29if __name__ == "__main__":30    display_all_s3_buckets_by_profile()
```

## [Benefits and usage](#benefits-and-usage)

### [Operational advantages](#operational-advantages)

**Why This Helps**

This script saves time for anyone managing multiple AWS environments. It covers S3 bucket auditing, inventory tasks, and compliance checks across your whole AWS footprint in a single run. You get one view without switching profiles by hand.

### [Use cases](#use-cases)

Ideal for inventory management, compliance auditing, and multi-account AWS operations.

## [Community Discussion](#community-discussion)

### [Your S3 management approaches](#your-s3-management-approaches)

**Your Turn**

How do you manage S3 bucket information in your Python projects? Any favorite Boto3 tricks for S3 you’d like to share?

### [Alternative tools](#alternative-tools)

Share your preferred methods for managing S3 resources across multiple AWS accounts.

Was this useful?

## Tags

[#Python](/codesnippets/tags/python)[#Boto3](/codesnippets/tags/boto3)[#AWS](/codesnippets/tags/aws)[#S3](/codesnippets/tags/s3)[#DevOps](/codesnippets/tags/devops)[#Cloud](/codesnippets/tags/cloud)[#Automation](/codesnippets/tags/automation)[#Scripting](/codesnippets/tags/scripting)

## Share

[Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fpython-list-s3-buckets "Share on Facebook")[Twitter](https://twitter.com/intent/tweet/?text=List%20S3%20Buckets&url=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fpython-list-s3-buckets "Share on Twitter")[LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fpython-list-s3-buckets&title=List%20S3%20Buckets&summary=Automate%20AWS%20S3%20interactions.%20This%20Python%20snippet%20uses%20Boto3%20to%20easily%20list%20all%20S3%20buckets%20in%20your%20account.&source=https://mkabumattar.com "Share on LinkedIn")[WhatsApp](https://wa.me/?text=List%20S3%20Buckets%20https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fpython-list-s3-buckets "Share on WhatsApp")[Telegram](https://t.me/share/url?url=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fpython-list-s3-buckets&text=List%20S3%20Buckets "Share on Telegram")[Reddit](https://www.reddit.com/submit?url=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fpython-list-s3-buckets&title=List%20S3%20Buckets "Share on Reddit")[Hacker News](http://news.ycombinator.com/submitlink?u=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fpython-list-s3-buckets&t=List%20S3%20Buckets "Share on Hacker News")[Pinterest](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fpython-list-s3-buckets&media=&description=Automate%20AWS%20S3%20interactions.%20This%20Python%20snippet%20uses%20Boto3%20to%20easily%20list%20all%20S3%20buckets%20in%20your%20account. "Share on Pinterest")[Email](<mailto:?subject=List%20S3%20Buckets&body=Check out this article: https%3A%2F%2Fmkabumattar.com%2Fcodesnippets%2Fpost%2Fpython-list-s3-buckets>)

## Comments

## You might also enjoy

More posts on similar topics

[![AWS EC2 Instance Management with Boto3: Start, Stop, and Query Instances](/_astro/hero.PnHlvJay_ZkFqWG.webp)](/codesnippets/post/aws-ec2-instance-management-boto3-python)

## [AWS EC2 Instance Management with Boto3: Start, Stop, and Query Instances](/codesnippets/post/aws-ec2-instance-management-boto3-python)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Cloud](/codesnippets/categories/cloud)
-   [Aws](/codesnippets/categories/aws)
-   [Devops](/codesnippets/categories/devops)
-   [Automation](/codesnippets/categories/automation)

If you've ever spent 20 minutes clicking through the AWS Console just to stop a handful of dev instances, you already know the pain. It's tedious, it doesn't scale, and one wrong click can ruin your a

[#AWS](/codesnippets/tags/aws)[#EC2](/codesnippets/tags/ec2)[#Boto3](/codesnippets/tags/boto3)+6 tags

[read more](/codesnippets/post/aws-ec2-instance-management-boto3-python)

[![Check S3 Bucket Existence](/_astro/hero.CWiO4GWV_1YUYTb.webp)](/codesnippets/post/bash-s3-bucket-exists)

## [Check S3 Bucket Existence](/codesnippets/post/bash-s3-bucket-exists)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Aws](/codesnippets/categories/aws)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Devops](/codesnippets/categories/devops)

Quick Tip Don't let your deployment blow up because of a missing S3 bucket. This Bash script lets you check if a bucket exists before anything fails. The Problem Missing bucket failure

[#Bash](/codesnippets/tags/bash)[#AWS](/codesnippets/tags/aws)[#DevOps](/codesnippets/tags/devops)+3 tags

[read more](/codesnippets/post/bash-s3-bucket-exists)

[![AWS Secrets Manager](/_astro/hero.BIDl4oI2_Z1yJEXf.webp)](/codesnippets/post/nodejs-aws-secrets-manager)

## [AWS Secrets Manager](/codesnippets/post/nodejs-aws-secrets-manager)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Aws](/codesnippets/categories/aws)
-   [Devops](/codesnippets/categories/devops)

Loading secrets in a Node.js app without exposing them If you're still storing API keys or database credentials in .env files or hardcoding them into your codebase, it's time for a better appro

[#NodeJS](/codesnippets/tags/nodejs)[#TypeScript](/codesnippets/tags/typescript)[#AWS](/codesnippets/tags/aws)+3 tags

[read more](/codesnippets/post/nodejs-aws-secrets-manager)

[![Essential Bash Variables for Every Script](/_astro/hero.B4bDowyY_YQpD7.webp)](/codesnippets/post/essential-bash-variables)

## [Essential Bash Variables for Every Script](/codesnippets/post/essential-bash-variables)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Devops](/codesnippets/categories/devops)

Overview Quick Tip You know what's worse than writing scripts? Writing scripts that break every time you move them to a different machine. Built-in Bash variables fix that. The problem wi

[#Bash](/codesnippets/tags/bash)[#Shell Scripting](/codesnippets/tags/shell-scripting)[#Linux](/codesnippets/tags/linux)+4 tags

[read more](/codesnippets/post/essential-bash-variables)

[![Optimizing your python code with \_\_slots\_\_?](/_astro/hero.DP_vYsHU_gYDXn.webp)](/codesnippets/post/python-slots-optimization)

## [Optimizing your python code with \_\_slots\_\_?](/codesnippets/post/python-slots-optimization)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Python](/codesnippets/categories/python)
-   [Productivity](/codesnippets/categories/productivity)

Memory optimization with slots Understanding the problem Optimizing data models in big data workflows with slots In big data and MLOps workflows, you often work with massive

[#Python](/codesnippets/tags/python)[#MemoryOptimization](/codesnippets/tags/memoryoptimization)[#DataScience](/codesnippets/tags/datascience)+4 tags

[read more](/codesnippets/post/python-slots-optimization)

[![Bash Retry Function: Automatically Retry Failing Commands with Exponential Backoff](/_astro/hero.Dap62rVN_Z1q0nar.webp)](/codesnippets/post/bash-retry-function-exponential-backoff)

## [Bash Retry Function: Automatically Retry Failing Commands with Exponential Backoff](/codesnippets/post/bash-retry-function-exponential-backoff)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Devops](/codesnippets/categories/devops)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Automation](/codesnippets/categories/automation)

Quick Tip Wrap any flaky command in one reusable retry function and stop re-running red pipelines by hand. The Problem The Problem Some commands fail for reasons that have nothing to

[#Bash](/codesnippets/tags/bash)[#Retry](/codesnippets/tags/retry)[#Exponential Backoff](/codesnippets/tags/exponential-backoff)+3 tags

[read more](/codesnippets/post/bash-retry-function-exponential-backoff)

6 related posts
