重构以限制使用lastModified<=15

问题描述 投票:0回答:1

是的,我们可以使用

aws s3api...
通过运行以下命令来过滤超过15天的密钥(下面返回预期结果):

lastModifiedMax=`date -d "15 days ago" '+%Y-%m-%d'`
aws s3api list-objects --bucket $bucket  --query "Contents[?LastModified<='${lastModifiedMax}'][].{object: Key}"

但是,我们想在 python boto3 中使用,使用脚本的示例片段 - https://stackoverflow.com/a/53877685/27921578

我们尝试替换下面几行

  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)
                for content in resp.get('Contents', []):
                    last_modified_date = content['LastModified']
                    if (
                        content['Key'].endswith(suffixes) and
                        last_modified_rule(last_modified_min, last_modified_date, last_modified_max)
                    ):
                        yield content

与:

    end_date = date.today() - timedelta(days=15)

    for prefix in prefixes:
        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)
            for content in resp.get('Contents', ['?LastModified<='+str(end_date)]):
                    yield content

上述更改没有给出预期结果,意味着返回“整个”键(整个时间戳)。

有什么问题请指教。

amazon-web-services boto3
1个回答
0
投票

没关系-调试/研究,我们可以通过这个来实现

           for content in resp.get('Contents',  []):
                if (
                        content['Key'] and content['LastModified'] <= datetime.now().astimezone() - timedelta(days=15)
                    ):
                    yield content

© www.soinside.com 2019 - 2024. All rights reserved.