Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I would like to use the AWS CLI to query the contents of a bucket and see if a particular file exists, but the bucket contains thousands of files. How can I filter the results to only show key names that match a pattern? For example:

aws s3api list-objects --bucket myBucketName --query "Contents[?Key==*mySearchPattern*]"

1 Answer

0 votes
by (44.4k points)

The --query argument uses JMESPath expressions. JMESPath has an internal function contains that allows you to search for a string pattern.

This should give the desired results:

aws s3api list-objects --bucket myBucketName --query "Contents[?contains(Key, `mySearchPattern`)]"

(With Linux I needed to use single quotes ' rather than back ticks ` around mySearchPattern.)

If you want to search for keys starting with certain characters, you can also use the --prefix argument:

aws s3api list-objects --bucket myBucketName --prefix "myPrefixToSearchFor"

Related questions

0 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

Browse Categories

...