skip to Main Content

I am using this command in PowerShell to list all buckets in my account:

aws s3 ls --profile xxx-dev

I need to list only the buckets that starts with some string. I tried this:

aws s3 ls --query "Buckets[?starts_with(Name, 'aveva-erm-') == 'true'].[Name]" --profile xxx-dev

I hae two problems:

1 – The results in both commands are exactly the same, so the --query options seems to be ignored, even though it is in the docs.

2 – The output is one line for each bucket like this: 2023-04-06 09:14:38 my-bucket-name. It is including a timestamp in each line. I would like to have only the bucket names.

— UPDATE —

I had better results with aws s3api list-buckets --query "Buckets[?starts_with(Name, 'prefix-')].Name" --profile xxx-dev

Using s3api list-buckets instead of s3 ls seems to take into account the --query parameter. But this outputs an serialized array in Powershell, like [ "bucket1", "bucket2", "bucket..." ]. Since I am using this output to chain another PowerShell command, I need the command to return only the bucket names. Is it possible?

2

Answers


  1. aws s3api list-buckets –query "Buckets[*].Name"

    aws s3api list-buckets –query "Buckets[].Name"

    You can try above one

    Login or Signup to reply.
  2. While the documentation page for aws s3 ls does mention support for the --query option, this seems to be ignored altogether. Same goes for the --output option…

    So instead, you can use:

    $ aws s3api list-buckets --query "Buckets[?starts_with(Name, 'prefix-')].Name" --profile xxx-dev --output text
    bucket1
    bucket2
    bucket...
    

    --output text will turn the output into multiple lines.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search