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 Antworten
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
beantwortet vor 2 Jahren
  • 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"

beantwortet vor 2 Jahren
  • 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
beantwortet vor 8 Monaten
-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
beantwortet vor 2 Jahren
  • It has the exact same output as without --region as S3 API is global (although buckets are regional).

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen