I am writing a Python script to download the latest file from inside an S3 Bucket's folder. I understand how to download the latest file object from my S3 Bucket, however the files I want to download are in a folder inside the bucket. I am at a complete loss on how to do it and where it may be added within my code. I tried putting the path at the end of my bucket link but that did not seem to work.
# AWS Credentials
client = boto3.client('athena',aws_access_key_id=aws_server_access_key, aws_secret_access_key=aws_server_secret_key,region_name='us-east-1')
ba = boto3.client('s3',aws_access_key_id=aws_server_access_key, aws_secret_access_key=aws_server_secret_key,region_name='us-east-1')
# Get latest modified file
get_last_modified = lambda obj: int(obj['LastModified'].strftime('%s'))
objs = ba.list_objects_v2(Bucket=BUCKET_NAME)['Contents']
last_added = [obj['Key'] for obj in sorted(objs, key=get_last_modified)][0]
s3 = boto3.resource('s3', aws_access_key_id= aws_server_access_key,aws_secret_access_key= aws_server_secret_key)
try:
s3.Bucket(BUCKET_NAME).download_file(last_added, last_added)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The object does not exist.")
else:
raise