- 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 Wait functionality to check whether a key in a S3 bucket exists, using Boto3 and AWS Client?
When a user wants to use wait functionality to validate whether a key in a bucket exists or not in programming code.
Problem Statement − Use boto3 library in Python to check whether a key exists in a bucket, using waiters functionality. For example, use waiters to check whether a key test.zip exists in Bucket_1.
Approach/Algorithm to solve this problem
Step 1 − Import boto3 and botocore exceptions to handle exceptions.
Step 2 − bucket_name and key are two parameters in function.
Step 3 − Create an AWS session using boto3 library.
Step 4 − Create an AWS client for S3.
Step 5 − Now create the wait object for object_exists using get_waiter function
Step 6 − Now, use the wait object to validate whether key exists or not in a given bucket. By default, it checks in every 5 seconds until a successful state is reached. An error is returned after 20 failed checks. However, the user can define polling time and max attempts.
Step 7 − It returns None.
Step 8 − Handle the generic exception if something went wrong while checking the bucket.
Example
Use the following code to use waiter to check whether a key exists in a bucket or not −
import boto3 from botocore.exceptions import ClientError def use_waiters_check_object_exists(bucket_name, key_name): session = boto3.session.Session() s3_client = session.client('s3') try: waiter = s3_client.get_waiter('object_exists') waiter.wait(Bucket=bucket_name, Key = key_name, WaiterConfig={ 'Delay': 2, 'MaxAttempts': 5}) print('Object exists: ' + bucket_name +'/'+key_name) except ClientError as e: raise Exception( "boto3 client error in use_waiters_check_object_exists: " + e.__str__()) except Exception as e: raise Exception( "Unexpected error in use_waiters_check_object_exists: " + e.__str__()) print(use_waiters_check_object_exists("Bucket_1","testfolder/test.zip")) print(use_waiters_check_object_exists("Bucket_1","testfolder/test1.zip") )
Output
Object exists: Bucket_1/testfolder/test.zip None botocore.exceptions.WaiterError: Waiter ObjectExists failed: Max attempts exceeded "Unexpected error in use_waiters_check_object_exists: " + e.__str__()) Exception: Unexpected error in use_waiters_check_object_exists: Waiter ObjectExists failed: Max attempts exceed
For Bucket_1/testfolder/test.zip, the output is print statement and None. Since the response doesn’t return anything, it prints None.
For Bucket_1/testfolder/test1.zip, the output is an exception, as this object doesn’t exist.
In exception, it can be read that Max attempts exceeded.
- Related Articles
- How to use Waitersto check whether an S3 bucket exists,using Boto3 and AWS Client?
- How to use Boto3 and AWS Client to determine whether a root bucket exists in S3?
- How to use Wait functionality to validatewhether a key does not exist in a S3 bucket in Boto3?
- How to use Boto3 and AWS Resource to determine whether a root bucket exists in S3?
- 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 use waiter functionality for bucket_not_exists 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 Boto3 to check whether a Glue Job exists or not?
