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"])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)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