VIPIN KUMAR
VIPIN KUMAR

Reputation: 3137

How to pass variable in s3api to filter the object using lastmodified?

I was trying to filter the S3 objects based on lastmodified using below command -

Upon providing the date manually like this LastModified>=2018-12-20 in --query it worked fine.

aws s3api list-objects --bucket $bucketNameToUse --query 'Contents[?LastModified>=`2018-12-20`].{Key: Key, LastModified: LastModified}' --output text

But when I tried to use Date variable in place of 2018-12-20 like below -

echo $DATEVARIABLE
2018-12-20

aws s3api list-objects --bucket $bucketNameToUse --query 'Contents[?LastModified > `$(DATEVARIABLE)`].{Key: Key, LastModified: LastModified}' --output text

It print every object in the bucket, I tried all possible way to use bash variable but didn't work. Not sure what I am missing here. Any Suggestion please.

Upvotes: 0

Views: 609

Answers (2)

VIPIN KUMAR
VIPIN KUMAR

Reputation: 3137

I got the solution with backtick (`) -

aws s3api list-objects --bucket $bucketNameToUse --query 'Contents[?LastModified > `'$DATEVARIABLE'`].{Key: Key, LastModified: LastModified}' --output text

Upvotes: 0

Prabhakar Reddy
Prabhakar Reddy

Reputation: 5124

AWS CLI uses JMESPath for --query option. Following worked for me after passing query in double quotes as a string.

--query "Contents[?LastModified>='${DATEVARIABLE}'].[Key, LastModified]"

Upvotes: 1

Related Questions