How to use Wait functionality to check whether a key in a S3 bucket exists, using Boto3 and AWS Client?

AWS S3 waiters provide a convenient way to poll for specific conditions, such as checking if an object exists in a bucket. The object_exists waiter repeatedly checks for an object's presence until it's found or the maximum attempts are exceeded.

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.

How It Works

The S3 waiter polls the bucket every few seconds (configurable delay) for a maximum number of attempts. By default, it checks every 5 seconds for up to 20 attempts. You can customize both the polling interval and maximum attempts using WaiterConfig.

Algorithm Steps

Step 1 ? Import boto3 and botocore exceptions to handle errors.

Step 2 ? Define function parameters: bucket_name and key_name.

Step 3 ? Create an AWS session using boto3.

Step 4 ? Create an S3 client from the session.

Step 5 ? Get the waiter object for object_exists using get_waiter().

Step 6 ? Use the waiter to check if the key exists, with optional custom polling configuration.

Step 7 ? Handle exceptions for missing objects or client errors.

Example

The following code demonstrates how to use S3 waiters to check object existence ?

import boto3
from botocore.exceptions import ClientError, WaiterError

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(f'Object exists: {bucket_name}/{key_name}')
        return True
    except WaiterError as e:
        print(f'Object does not exist: {bucket_name}/{key_name}')
        return False
    except ClientError as e:
        raise Exception(f"boto3 client error: {e}")
    except Exception as e:
        raise Exception(f"Unexpected error: {e}")

# Test with existing object
print(use_waiters_check_object_exists("my-bucket", "testfolder/test.zip"))

# Test with non-existing object  
print(use_waiters_check_object_exists("my-bucket", "testfolder/nonexistent.zip"))

Output

Object exists: my-bucket/testfolder/test.zip
True
Object does not exist: my-bucket/testfolder/nonexistent.zip
False

Parameters

The WaiterConfig accepts these parameters ?

  • Delay ? Time in seconds between polling attempts (default: 5)
  • MaxAttempts ? Maximum number of polling attempts (default: 20)

Key Points

  • Waiters return None on success and raise WaiterError on timeout
  • Use shorter delays and fewer attempts for faster feedback during testing
  • Always handle WaiterError separately from other exceptions
  • Waiters are more efficient than manual polling loops for AWS resource states

Conclusion

S3 waiters provide a clean way to poll for object existence with built-in retry logic. Use WaiterConfig to customize polling behavior and handle WaiterError for non-existent objects.

Updated on: 2026-03-25T18:12:11+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements