The other questions I could find were refering to an older version of Boto. I would like to download the latest file of an S3 bucket. In the documentation I found that there
I also wanted to download latest file from s3 bucket but located in a specific folder. Use following function to get latest filename using bucket name and prefix (which is folder name).
import boto3
def get_latest_file_name(bucket_name,prefix):
"""
Return the latest file name in an S3 bucket folder.
:param bucket: Name of the S3 bucket.
:param prefix: Only fetch keys that start with this prefix (folder name).
"""
s3_client = boto3.client('s3')
objs = s3_client.list_objects_v2(Bucket=bucket_name)['Contents']
shortlisted_files = dict()
for obj in objs:
key = obj['Key']
timestamp = obj['LastModified']
# if key starts with folder name retrieve that key
if key.startswith(prefix):
# Adding a new key value pair
shortlisted_files.update( {key : timestamp} )
latest_filename = max(shortlisted_files, key=shortlisted_files.get)
return latest_filename
latest_filename = get_latest_file_name(bucket_name='use_your_bucket_name',prefix = 'folder_name/')