amazon web services - refactor to limit to use lastModified<=15 - Stack Overflow

Yes, we can use the aws s3api... to filter the keys that is older than 15 days by means of running belo

Yes, we can use the aws s3api... to filter the keys that is older than 15 days by means of running below command (below returns expected result):

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

However, we want to use in python boto3, using this sample piece of the script -

Which is, we tried to replace this below lines

  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

With :

    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

The above changes did not give expected result, means, returned "whole" keys (whole timetamp).

Please advise what is wrong.

Yes, we can use the aws s3api... to filter the keys that is older than 15 days by means of running below command (below returns expected result):

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

However, we want to use in python boto3, using this sample piece of the script - https://stackoverflow/a/53877685/27921578

Which is, we tried to replace this below lines

  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

With :

    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

The above changes did not give expected result, means, returned "whole" keys (whole timetamp).

Please advise what is wrong.

Share Improve this question edited Nov 18, 2024 at 17:48 Hari Addepalli asked Nov 18, 2024 at 17:47 Hari AddepalliHari Addepalli 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

never mind - debugged/researched, we can achieve by means of this

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745603532a4635541.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信