- 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 and AWS Client to determine whether a root bucket exists in S3?
Problem Statement − Use Boto3 library in Python to determine whether a root bucket exists in S3 or not.
Example − Bucket_1 exists or not in S3.
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 client for S3.
Step 4 − Use the function head_bucket(). It returns 200 OK if the bucket exists and the user has permission to access it. Otherwise, the response would be 403 Forbidden or 404 Not Found.
Step 5 − Handle the exception based on the response code.
Step 6 − Return True/False based on whether the bucket exists or not.
Example
The following code checks whether the root bucket exists in S3 −
import boto3 from botocore.exceptions import ClientError # To check whether root 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 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
- Related Articles
- How to use Boto3 and AWS Resource to determine whether a root bucket exists in S3?
- How to use Waitersto check whether an S3 bucket exists,using Boto3 and AWS Client?
- How to use Wait functionality to check whether a key in a S3 bucket exists, using Boto3 and AWS Client?
- How to get the bucket location of a S3 bucket using Boto3 and AWS Client?
- How to get the bucket logging details of a S3 bucket using Boto3 and AWS Client?
- How to get the lifecycle of a S3 bucket using Boto3 and AWS Client?
- How to get the notification configuration details of a S3 bucket using Boto3 and AWS Client?
- How to get the ownership control details of an S3 bucket using Boto3 and AWS Client?
- How to use Boto3 to get a list of buckets present in S3 using AWS Client?
- 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 use Boto3 to paginate through multi-part upload objects of a S3 bucket present in AWS Glue
- How to use waiter functionality for bucket_not_exists using Boto3 and AWS Client?
- How to use Boto3 to download an object from S3 using AWS Resource?
- How to use Wait functionality to validatewhether a key does not exist in a S3 bucket in Boto3?

Advertisements