- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use Boto3 library in Python to get the list of buckets present in AWS S3?
Problem Statement − Use boto3 library in Python to get the list of all buckets present in AWS.
Example − Get the name of buckets like – BUCKET_1, BUCKET2, BUCKET_3
Approach/Algorithm to solve this problem
Step 1 − Import boto3 and botocore exceptions to handle exceptions.
Step 2 − Create an AWS session using Boto3 library.
Step 3 − Create an AWS resource for S3
Step 4 − Use the function buckets.all() to list out the bucket names.
Step 5 − Handle any unwanted exception, if it occurs
Step 6 − Return the list of buckets_namev
Example
The following code gets the list of buckets present in S3 −
import boto3 from botocore.exceptions import ClientError # To get list of buckets present in AWS using S3 resource def get_buckets_resource(): session = boto3.session.Session() # User can pass customized access key, secret_key and token as well s3_resource = session.resource('s3') try: buckets = list(s3_resource.buckets.all()) print("Got buckets using resource:", buckets) except ClientError: print("Couldn't get buckets.") raise else: return buckets get_buckets_resource()
Output
Got buckets using resource:[s3.Bucket(name='BUCKET_1'), s3.Bucket(name='BUCKET_2'), s3.Bucket(name='BUCKET_3)………… ]
- Related Articles
- How to use Boto3 to get a list of buckets present in S3 using AWS Client?
- How to use Boto3 to get the list of schemas present in AWS account
- How to use Boto3 library in Python to upload an object in S3 using AWS Resource?
- How to use Boto3 library in Python to get a list of files from S3 based on the last modified date using AWS Resource?
- How to use Boto3 library in Python to delete an object from S3 using AWS Resource?
- How to use Boto3 to get the list of triggers present in an AWS account
- How to use Boto3 to get the list of workflows present an in AWS account
- How to use Boto3 to paginate through object versions of a S3 bucket present in AWS Glue
- How to use Boto3 to paginate through all objects of a S3 bucket present in AWS Glue
- How to get the list of all crawlers present in an AWS account using Boto3
- How to get the list of all registries present in an AWS account using Boto3
- How to use Boto3 to paginate through multi-part upload objects of a S3 bucket present in AWS Glue
- How to use Boto3 to get the details of allsecurity configuration present in AWS Glue Security?
- How to get the list of all versions of the object from S3 present in AWS Resource
- How to use Boto3 library in Python to get the details of a crawler?

Advertisements