List S3 buckets using Python, AWS CLI

In this blog, we will learn how to list down all buckets in our AWS account using Python and AWS CLI. We will learn different ways to list buckets and filter them using tags.


Listing All buckets

We can list buckets with CLI in one single command.

list buckets in AWS with CLI
Listing buckets with AWS CLI

If you have lots of buckets this output will become difficult to follow. But AWS CLI now supports query parameters. Using query parameters we can extract the required information from the output.

List S3 buckets using AWS CLI and query parameter
AWS CLI – listing buckets with query flag

We can also use jq (a lightweight command-line JSON parser) to do some funky things. The following code will print bucket names along with tags associated with them.

Using Python

Listing all buckets

We can also easily list down all buckets in the AWS account using python.

When we run the above code we will get the following output.

listing AWS buckets with python
Python – listing buckets with boto3 client

Boto3 also provides us with Bucket resources. We can use its all() function to list down all buckets in the AWS account.

Python listing aws buckets with Boto3 resource
Python listing AWS buckets with Boto3 resource

I also tried buckets filtering based on tags. You can have 100s if not thousands of buckets in the account and the best way to filter them is using tags. Boto3 does provide a filter method for bucket resources. But I did not find how we can use it. So I tried a workaround to filter buckets using tag value in python.

If you find how to use the filter method for this approach please let me know. Here is the actual function give by boto3.


We have learned how to list down buckets in the AWS account using CLI as well as Python. Next in this series, we will learn more about performing S3 operations using CLI and python. If you are interested, please subscribe to the newsletter. See you in the next blog.

Listing S3 buckets using Python & CLI

Mahesh Mogal

I am passionate about Cloud, Data Analytics, Machine Learning, and Artificial Intelligence. I like to learn and try out new things. I have started blogging about my experience while learning these exciting technologies.

Let us connect

Leave a Comment

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

Scroll to Top
Copy link
Powered by Social Snap