2016-03-24 30 views
4

我可以抓住並通過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 

閱讀我的AWS S3存儲桶中的所有對象,然後

obj.key 

會給我內的路徑桶。

是否有一種方法可以預先過濾那些關於某個起始路徑(存儲桶中的目錄)的文件,以避免循環遍歷所有對象並稍後過濾?

回答

7

使用filter[1],[2]集合的方法,如桶。

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