Search code examples
pythonboto3

Listing objects in S3 with suffix using boto3


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'


Solution

  • 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