How to download the latest file of an S3 bucket using Boto3?

后端 未结 7 1014
借酒劲吻你
借酒劲吻你 2021-01-11 23:22

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

7条回答
  •  时光取名叫无心
    2021-01-12 00:06

    If you have a lot of files then you'll need to use pagination as mentioned by helloV. This is how I did it.

    get_last_modified = lambda obj: int(obj['LastModified'].strftime('%s'))
    s3 = boto3.client('s3')
    paginator = s3.get_paginator( "list_objects" )
    page_iterator = paginator.paginate( Bucket = "BucketName", Prefix = "Prefix")
    for page in page_iterator:
        if "Contents" in page:
            last_added = [obj['Key'] for obj in sorted( page["Contents"], key=get_last_modified)][-1]
    

提交回复
热议问题