Boto3 S3, sort bucket by last modified

前端 未结 7 1569
眼角桃花
眼角桃花 2020-12-09 09:29

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

7条回答
  •  渐次进展
    2020-12-09 09:52

    A simpler approach, using the python3 sorted() function:

    import boto3
    s3 = boto3.resource('s3')
    
    myBucket = s3.Bucket('name')
    
    def obj_last_modified(myobj):
        return myobj.last_modified
    
    sortedObjects = sorted(myBucket.objects.all(), key=obj_last_modified, reverse=True)
    

    you now have a reverse sorted list, sorted by the 'last_modified' attribute of each Object.

提交回复
热议问题