[SPEC-7931] Filter list_buckets results based on region during import (#2950)

Filter s3 list_buckets results based on region correctly to improve resource import.

Signed-off-by: onecent1101 <liug@amazon.com>
This commit is contained in:
Vincent Liu
2021-08-09 13:06:28 -07:00
committed by GitHub
parent c7397fe4a2
commit 89602aaa63
2 changed files with 51 additions and 5 deletions
@@ -103,7 +103,17 @@ def list_s3_buckets(region: str = "") -> List[str]:
bucket_names: List[str] = []
bucket: Dict[str, any]
for bucket in response["Buckets"]:
bucket_names.append(bucket["Name"])
try:
bucket_name: str = bucket["Name"]
location_response: Dict[str, any] = s3_client.get_bucket_location(Bucket=bucket_name)
# Buckets in Region us-east-1 have a LocationConstraint of null .
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.get_bucket_location
if ((location_response["LocationConstraint"] == region) or
(not location_response["LocationConstraint"] and region == "us-east-1")):
bucket_names.append(bucket_name)
except ClientError as error:
raise RuntimeError(error_messages.AWS_SERVICE_REQUEST_CLIENT_ERROR_MESSAGE.format(
"get_bucket_location", error.response['Error']['Code'], error.response['Error']['Message']))
return bucket_names