最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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

Post a comment

comment list (0)

  1. No comments so far