Created
February 27, 2025 19:52
-
-
Save danilogco/0cd33237ab2df713d33462907ec9a373 to your computer and use it in GitHub Desktop.
s3 check directory exists script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
aws_access_key_id = "XXXXXXXXXXX" | |
aws_secret_access_key = "XXXXXXXXXXX" | |
aws_session_token = None | |
region_name = "us-east-1" | |
s3_client = boto3.client( | |
"s3", | |
aws_access_key_id=aws_access_key_id, | |
aws_secret_access_key=aws_secret_access_key, | |
aws_session_token=aws_session_token, | |
region_name=region_name, | |
) | |
def check_directory_exists(bucket, prefix): | |
response = s3_client.list_objects_v2(Bucket=bucket, Prefix=prefix, MaxKeys=1) | |
return "Contents" in response | |
bucket_name = "bucket-name" | |
dirs = [ | |
"folder1/folder2/", | |
"folder1/folder2/folder3/", | |
"folder1/folder2/folder3/folder4/", | |
] | |
for directory_prefix in dirs: | |
if check_directory_exists(bucket_name, directory_prefix): | |
print(f"{directory_prefix}: Diretório existe!") | |
else: | |
print(f"{directory_prefix}: Diretório não existe!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment