def get_latest_file_movement(**kwargs):
get_last_modified = lambda obj: int(obj['LastModified'].strftime('%s'))
s3 = boto3.client('s3')
objs = s3.list_objects_v2(Bucket='my-bucket',Prefix='prefix')['Contents']
last_added = [obj['Key'] for obj in sorted(objs, key=get_last_modified, reverse=True)][0]
return last_added
Above code gets me the latest file however i only want the files ending with 'csv'
You can check if they end with .csv
:
def get_latest_file_movement(**kwargs):
get_last_modified = lambda obj: int(obj['LastModified'].strftime('%s'))
s3 = boto3.client('s3')
objs = s3.list_objects_v2(Bucket='my-bucket',Prefix='prefix')['Contents']
last_added = [obj['Key'] for obj in sorted(objs, key=get_last_modified, reverse=True) if obj['Key'].endswith('.csv')][0]
return last_added