python s3 在不知道确切文件名的情况下获取文件

python s3 get file without knowing exactly the name file

我正在使用 python 和 boto 从 amazon S3 获取文件。我以这种格式上传:year/month/FileNameYYYYMMdd.hhmmss.zip

我的问题是我不知道文件的时间 (hhmmss)。有办法得到吗?例如:

bucket = awsCnx.get_bucket(bucketName)
directory = 'year/month/FileNameYYYYMMdd.*.zip'
bucket.list(prefix=directory)

来自博托 source-code:

def list(self, prefix='', delimiter='', marker='', headers=None,
             encoding_type=None):
"""
...
:type prefix: string
        :param prefix: allows you to limit the listing to a particular
            prefix.  For example, if you call the method with
            prefix='/foo/' then the iterator will only cycle through
            the keys that begin with the string '/foo/'.
...

您可以在 docs

中阅读更多相关信息

所以我看不出有什么理由不完全按照您编写的方式实现它,只需对声明进行微小更改:

directory = 'year/month/FileNameYYYYMMdd'

(当然用正确的日期替换日期模式)。

bucket 确实支持前缀选项!下面是它的用法示例:

bucket = awsCnx.get_bucket(bucketName)
directory = 'year/month/FileName'
keys = bucket.get_all_keys(prefix=directory)

keys 将是包含任何以 year/month/FileName* 开头的文件的 s3 密钥列表,您可以从中获取它们的名称 (keys[0].name) 或它们的内容 (keys[0].get_contents_as_string)

bucket.list 也可以使用前缀,但是 return 一个 BucketListResultSet 而不是