List S3 buckets using Python, AWS CLI

Updated On February 12, 2021 | By Mahesh Mogal

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.

Using AWS CLI

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.

Conclusion

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.

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.

Stay Updated with Latest Blogs

Get latest blogs delivered to your mail directly.

Recent Posts

Spark Join Types With Examples

In this blog, we are going to learn different spark join types. We will also write code and validate data output for each join type to better understand them.

Read More
Integrate Spark with Jupyter Notebook and Visual Studio Code

In this blog, we are going to integrate spark with jupyter notebook and visual studio code to create easy-to-use development environment.

Read More
Reading Data From SQL Tables in Spark

In this blog, we are going to learn about reading data from SQL tables in Spark. We will create Spark data frames from tables and query results as well.

Read More

Leave a Reply

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

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram