[英]Boto3 Amazon S3 (Python package) - getting only files that fill a condition (a filtered bucket.list())
我有一个S3服务器,每个存储桶下都有数百万个文件。 我想从存储桶中下载文件,但只下载满足特定条件的文件。 有没有比遍历所有存储区然后在遍历文件时检查特定条件更好的方法? 如此处所示:
import os
# Import the SDK
import boto
from boto.s3.connection import OrdinaryCallingFormat
LOCAL_PATH = 'W:/RD/Fancy/s3_opportunities/'
bucket_name = '/recording'#/sampledResponseLogger'
# connect to the bucket
print 'Connecting...'
conn = boto.connect_s3(calling_format=OrdinaryCallingFormat()) #conn = boto.connect_s3()
print 'Getting bucket...'
bucket = conn.get_bucket(bucket_name)
print 'Going through the list of files...'
bucket_list = bucket.list()
for l in bucket_list:
keyString = str(l.key)
# SOME CONDITION
if('2015-08' in keyString):
# check if file exists locally, if not: download it
filename=LOCAL_PATH+keyString[56:]
if not os.path.exists(filename):
print 'Downloading file: ' + keyString + '...'
# Download the object that the key represents
l.get_contents_to_filename(filename)
可用于过滤服务器端ListBucket
操作的唯一机制是prefix
。 因此,如果S3中的对象具有某种隐式目录结构(例如foo/bar/fie/baz/object1
),则可以使用前缀仅列出以foo/bar/fie
开头的对象。 如果您的对象名称未显示此分层命名,则除了列出所有对象并使用自己的机制进行过滤之外,您实际上无能为力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.