AWS S3: How to list bucket contents by last modified date using CLI?
Introduction
AWS’ s3api
CLI can be used to filter objects in a S3 bucket based on the Last Modified Date
using the --query
filter.
In this article, we will look at how you can leverage this functionality to only return the objects that you need.
Table of contents
How to use list-objects-v2
This command is used to return some or all (up to 1000) objects in a bucket. To use this operation, you must have READ
access to the bucket.
You can run this command by using the following example:
aws s3api list-objects-v2 --bucket my-bucket
By default, the output returns a LastModified
field for every object in the response. We will use this field to filter the results that we care about.
{
"Contents": [
{
"LastModified": "2019-11-05T23:11:50.000Z",
"ETag": "\"621503c373607d548b37cff8778d992c\"",
"StorageClass": "STANDARD",
"Key": "doc1.rtf",
"Size": 391
},
...
How filter based on Last Modified Date?
The list-objects-v2
command takes an optional field called --query
as input that can be used to filter the response data. The query language is based on JMESPath
Examples:
Filter objects after certain date
We can filter out objects that were modified after a certain date by using the command below:
aws s3api list-objects-v2 --bucket my-bucket --query 'Contents[?LastModified>`2022-01-01`].Key'
Filter objects on certain date
We can filter objects that were modified on a particular date using the command below:
aws s3api list-objects-v2 --bucket my-bucket --query 'Contents[?contains(LastModified, `2022-01-01`)].Key'