3/01/2023

Folder existing check in s3 bucket, python example code

Check folder exist in s3 bucket

refer to code:

.

import boto3
import botocore.exceptions

s3 = boto3.client('s3')
bucket_name = 'your-bucket-name'
folder_path = 'f1/f2/f3/f4/'

try:
response = s3.list_objects_v2(Bucket=bucket_name, Prefix=folder_path)
if 'Contents' in response or 'CommonPrefixes' in response:
print(f"Folder {folder_path} exists in bucket {bucket_name}")
else:
print(f"Folder {folder_path} does not exist in bucket {bucket_name}")
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "NoSuchBucket":
print(f"Bucket {bucket_name} does not exist")
else:
raise

..


thank you

πŸ™‡πŸ»‍♂️

www.marearts.com

No comments:

Post a Comment