"AWS Python Atomation : list files all object in S3 with matching a pattern _latest"
- Anu Solanki
- Dec 24, 2023
- 1 min read

install and import boto3 and botocore library for connect with AWS:
!pip install boto3 botocore
import boto3
import time
from botocore.exceptions import NoCredentialsError, PartialCredentialsError, EndpointConnectionError, ClientError
Declare important variable
must be change the variable values
# AWS Credentials
aws_access_key = 'your aws_access_key'
aws_secret_key = 'your aws_secret_key'
# S3 Bucket Name
bucket_name = 'your bucket name'
# AWS Region
region_name = 'ap-south-1' # Replace with your desired AWS region, e.g., 'us-east-1'
suffix_to_find = '_latest'
AWS S3 Client Initialization with Access Keys and Region Configuration
# Create S3 client object with the specified region
s3 = boto3.client('s3', aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key, region_name=region_name)
Function to List out all the object with suffix an AWS S3 Bucket with Error Handling
def list_s3_files_with_suffix(bucket_name, suffix, aws_access_key, aws_secret_key, aws_region):
try:
# List objects in the bucket
response = s3.list_objects(Bucket=bucket_name)
# Extract 'Key' values from the response
keys = [obj['Key'] for obj in response.get('Contents', [])]
#print(keys)
# Filter objects that end with the specified suffix
#files_with_suffix = [key for key in keys if key.strip().endswith(suffix.strip())]
files_with_suffix = [key for key in keys if suffix.strip() in key.strip()]
#print(files_with_suffix)
if files_with_suffix:
print(f"Files ending with '{suffix}' in the S3 bucket '{bucket_name}':")
for file in files_with_suffix:
print(file)
else:
print(f"No files found in the bucket '{bucket_name}' with the specified suffix.")
return files_with_suffix
except Exception as e:
print(f"Error listing files: {e}")
return []
Run the list_s3_files_with_suffix() Function
list_s3_files_with_suffix(bucket_name, suffix_to_find, aws_access_key, aws_secret_key, region_name)
Comments