I need to fetch a list of items from S3 using Boto3, but instead of returning default sort order (descending) I want it to return it via reverse order.
I know you ca
s3 = boto3.client('s3')
get_last_modified = lambda obj: int(obj['LastModified'].strftime('%Y%m%d%H%M%S'))
def sortFindLatest(bucket_name):
resp = s3.list_objects(Bucket=bucket_name)
if 'Contents' in resp:
objs = resp['Contents']
files = sorted(objs, key=get_last_modified)
for key in files:
file = key['Key']
cx = s3.get_object(Bucket=bucket_name, Key=file)
This works for me to sort by date and time. I am using Python3 AWS lambda. Your mileage may vary. It can be optimized, I purposely made it discrete. As mentioned in an earlier post, 'reverse=True' can be added to change the sort order.