Boto3:仅从S3资源中获取选定的对象 [英] Boto3: grabbing only selected objects from the S3 resource

查看:129
本文介绍了Boto3:仅从S3资源中获取选定的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过

s3 = boto3.resource('s3')
    bucket = s3.Bucket('my-bucket')
    all_objs = bucket.objects.all()
    for obj in all_objs:
        pass
        #filter only the objects I need

然后

obj.key

会给我铲斗中的路径.

是否有一种方法可以只对那些遵循特定起始路径(存储桶中的目录)的文件进行预先过滤,以便避免循环遍历所有对象并在以后进行过滤?

Is there a way to filter beforehand for only those files respecting a certain starting path (a directory in the bucket) so that I'd avoid looping over all the objects and filtering later?

推荐答案

使用filter [2] 像bucket之类的集合方法.

Use the filter[1], [2] method of collections like bucket.

s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
objs = bucket.objects.filter(Prefix='myprefix')
for obj in objs:
    pass

这篇关于Boto3:仅从S3资源中获取选定的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆