By using AWS re:Post, you agree to the Terms of Use
/How to list buckets only from a certain region using the CLI?/

How to list buckets only from a certain region using the CLI?

0

I 'm able to list all the buckets using either:

  • aws s3 ls
  • aws s3api list-buckets

However, I can't find an efficient way to filter by region, something like aws s3api list-buckets --filter region=us-east-2. I'd like to achieve this in 1 API call/CLI command only, i.e., ideally not multiple round-trips to the AWS API. Use of jq would be acceptable.

3 Answers
1

My previous response was a fast question, the negative vote was unexpected. Based on what you mentioned before to another answer I was able to create a Powershell script that I hope could be useful in your case. Assuming you define a variable name region

Set-Variable -name region -Value "us-west-2"

Running the following script should give you the bucket names

 aws s3api list-buckets --output json |  jq .Buckets[].Name | 
 foreach-object { 
    Set-Variable -name bname -value $_
    aws s3api get-bucket-location --bucket $_  
    } | 
 foreach-object{ if($region -eq $_) { Write-Output $bName } } 

Take care and stay safe, Best regards,

Igvir

answered 3 months ago
  • This seems to be the only solution AFAIK. It's very unfortunate that we need to call get-bucket-location for each bucket. If only AWS could include the region in the list-buckets response this could be avoided.

  • I agree AWS could include the region in the list-buckets response.

0

Not possible with aws "aws s3 ls" or "aws s3api list-buckets"

answered 3 months ago
  • I'm open to any alternatives, as long as I don't have to query bucket-by-bucket for the region of each one.

-1

Hello According to the AWS Command Line Interface reference, you have the option to use global parameters.

Did you try global parameter --region?

aws s3api list-buckets --region "us-east-2"

answered 3 months ago
  • It has the exact same output as without --region as S3 API is global (although buckets are regional).

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions