Code: Select all
import boto3
import pathlib
BUCKET_NAME = 'testing'
s3_client = boto3.client('s3')
response = s3_client.list_objects_v2(Bucket = BUCKET_NAME, Prefix = KEY)
if 'Contents' in response:
for obj in response['Contents']:
file_key = obj['Key']
file_name = os.path.basename(file_key) # Get the file name from the key
local_file_path = os.path.join(f'test_dir', file_name)
#Download the file
s3_client.download_file(BUCKET_NAME, file_key, local_file_path)
print(f"Downloaded {file_name}")