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.

4 Answers
2

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

Igvir
answered 2 years 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 2 years 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.

0

Here is a bash answer to this question. you need to use aws s3api get-bucket-location --bucket xxxxx to get the region as documented here.

for bucket in $(aws s3api list-buckets | jq '.Buckets | .[].Name' | sed 's/"//g' ) ; do
  location=$(aws s3api get-bucket-location --bucket $bucket | jq '.LocationConstraint' | sed 's/null/us-east-1/g' | sed 's/"//g')
  echo $bucket $location
done
AWS
answered 8 months ago
-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"

Igvir
answered 2 years 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