过滤 S3 列表对象结果以找到匹配模式的键 [英] Filter S3 list-objects results to find a key matching a pattern
问题描述
我想使用 AWS CLI 查询存储桶的内容并查看特定文件是否存在,但该存储桶包含数千个文件.如何过滤结果以仅显示与模式匹配的键名?例如:
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*]"
推荐答案
--query
参数使用 JMESPath 表达式.JMESPath 有一个内部函数 contains
允许您搜索字符串模式.
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`)]"
(对于 Linux,我需要在 mySearchPattern
周围使用单引号 '
而不是反引号 `
.)
(With Linux I needed to use single quotes '
rather than back ticks `
around mySearchPattern
.)
如果您想搜索以某些字符开头的键 ,您还可以使用 --prefix
参数:
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屋!