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*]"
The
--queryargument uses JMESPath expressions. JMESPath has an internal functioncontainsthat allows you to search for a string pattern.This should give the desired results:
(With Linux I needed to use single quotes
'rather than back ticks`aroundmySearchPattern.)If you want to search for keys starting with certain characters, you can also use the
--prefixargument: