Filter S3 list-objects results to find a key matching a pattern

后端 未结 2 1829
无人及你
无人及你 2020-12-30 01:22

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 resu

相关标签:
2条回答
  • 2020-12-30 01:56

    I tried on Ubuntu 14, awscli 1.2

    --query "Contents[?contains(Key,'stati')].Key" 
    --query "Contents[?contains(Key,\'stati\')].Key" 
    --query "Contents[?contains(Key,`stati`)].Key"
    

    Illegal token value '?contains(Key,'stati')].Key'

    After upgraded the aws version to 1.16 , worked with

    --query "Contents[?contains(Key,'stati')].Key" 
    
    0 讨论(0)
  • 2020-12-30 02:01

    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"
    
    0 讨论(0)
提交回复
热议问题