Python boto3按大小排序s3对象

时间:2018-05-15 17:20:23

标签: python amazon-s3 boto3

是否可以在python(Boto3)中或使用任何其他python库根据s3桶的大小列出s3桶中的对象。

我有1000个文件,我想先用最小的文件处理

我尝试过使用,但我觉得它没有这种方式

from operator import itemgetter
sortedobjects = sorted(source_bucket.objects.all(),key=itemgetter('size'))

2 个答案:

答案 0 :(得分:1)

你很亲密!您只需使用operator.itemgetter,而不是import boto3 from operator import attrgetter s3 = boto3.resource('s3') bucket = s3.Bucket('your-bucket-here') sortedobjects = sorted(bucket.objects.all(), key=attrgetter('size')) ,如下所示:

router.post('/liveAuction', (req, res, next) => {
const id = req.body.id;

Auction.getAuctionById(id, (err, liveAuction) => {
    if (err){
        res.json({
            success: false,
            message: "Something went wrong!"
        });
        console.log(err);
    }
    else {
        res.json({
            success: true,
            message: "Auction retrieved!",
            liveAuction
        });
    }
});
});

答案 1 :(得分:0)

import boto3
client = boto3.client('s3') 
response = client.list_objects(   
    Bucket='BucketName'
)
for object_size in response['Contents']:
    print((object_size['Key']), (object_size['Size']))