Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
Boto3 obtiene solo cubos S3 de una región específica

Lamentablemente, el siguiente código enumera todos los cubos de todas las regiones y no solo de "eu-west-1" como se especifica. ¿Cómo puedo cambiar eso?

 import boto3 s3 = boto3.client("s3", region_name="eu-west-1") for bucket in s3.list_buckets()["Buckets"]: bucket_name = bucket["Name"] print(bucket["Name"])
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

s3 = boto3.client("s3", region_name="eu-west-1")

se conecta al extremo de la API de S3 en eu-west-1 . No limita la lista a eu-west-1 . Una solución es consultar la ubicación del depósito y filtrar.

 s3 = boto3.client("s3") for bucket in s3.list_buckets()["Buckets"]: if s3.get_bucket_location(Bucket=bucket['Name'])['LocationConstraint'] == 'eu-west-1': print(bucket["Name"])

Si necesita una sola línea usando la comprensión de la lista de Python:

 region_buckets = [bucket["Name"] for bucket in s3.list_buckets()["Buckets"] if s3.get_bucket_location(Bucket=bucket['Name'])['LocationConstraint'] == 'eu-west-1'] print(region_buckets)
over 4 years ago · Santiago Trujillo Report

0

La solución anterior no siempre funciona para depósitos en algunas regiones de EE. UU. porque "LocationConstraint" puede ser nulo. Aquí hay otra solución:

 s3 = boto3.client("s3") for bucket in s3.list_buckets()["Buckets"]: if s3.head_bucket(Bucket=bucket['Name'])['ResponseMetadata']['HTTPHeaders']['x-amz-bucket-region'] == 'us-east-1': print(bucket["Name"])

El método SDK:

s3.head_bucket(Bucket=[INSERT_BUCKET_NAME_HERE])['ResponseMetadata']['HTTPHeaders']['x-amz-bucket-region']

... siempre debería darte la región del cubo. Gracias a sd65 por el consejo: https://github.com/boto/boto3/issues/292

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!