是否有办法列出指定日期之间的所有s3文件。起始日期可以作为前缀传递。我对如何结束日期感到困惑。请帮忙。
import boto3
def get_matching_s3_objects(bucket, prefix=''):
"""
Generate objects in an S3 bucket.
:param bucket: Name of the S3 bucket.
:param prefix: Only fetch objects whose key starts with
this prefix
"""
s3 = boto3.client('s3')
kwargs = {'Bucket': bucket}
if isinstance(prefix, str):
kwargs['Prefix'] = prefix
while True:
# The S3 API response is a large blob of metadata.
# 'Contents' contains information about the listed objects.
resp = s3.list_objects_v2(**kwargs)
try:
contents = resp['Contents']
except KeyError:
return
for obj in contents:
key = obj['Key']
if key.startswith(prefix) and key.endswith(suffix):
yield obj
# The S3 API is paginated, returning up to 1000 keys at a time.
# Pass the continuation token into the next response, until we
# reach the final page (when this field is missing).
try:
kwargs['ContinuationToken'] = resp['NextContinuationToken']
except KeyError:
break
def get_matching_s3_keys(bucket, prefix=''):
"""
Generate the keys in an S3 bucket.
:param bucket: Name of the S3 bucket.
:param prefix: Only fetch keys that start with this prefix (optional).
:param suffix: Only fetch keys that end with this suffix (optional).
"""
for obj in get_matching_s3_objects(bucket, prefix, suffix):
yield obj['Key']
答案 0 :(得分:1)
AFAIK没有使用boto3按日期过滤的直接方法,唯一的filter available是Bucket
,Delimiter
,EncodingType
,Marker
,{{ 1}},MaxKeys
和Prefix
。
因此,您需要循环遍历键/对象,以将开始/结束日期与对象RequestPayer
的datetime值进行比较,以便在一周前(包括)到今天之间的特定存储桶中获取所有对象(排除),我将执行
last_modified