筛选S3列表对象结果以找到与模式匹配的键 [英] Filter S3 list-objects results to find a key matching a pattern
问题描述
我想使用AWS CLI查询存储桶的内容,查看是否存在特定文件,但是存储桶包含数千个文件.如何过滤结果以仅显示与模式匹配的键名?例如:
aws s3api list-objects --bucket myBucketName --query "Contents[?Key==*mySearchPattern*]"
--query
参数使用JMESPath表达式. JMESPath具有内部功能contains
,该功能可让您搜索字符串模式.>
这应该得到预期的结果:
aws s3api list-objects --bucket myBucketName --query "Contents[?contains(Key, `mySearchPattern`)]"
(在Linux中,我需要使用单引号'
,而不是在mySearchPattern
周围使用反引号`
.)
如果要搜索带有某些字符的键开头,还可以使用--prefix
参数:
aws s3api list-objects --bucket myBucketName --prefix "myPrefixToSearchFor"
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 --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"
这篇关于筛选S3列表对象结果以找到与模式匹配的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!