Skip to content

Commit

Permalink
add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tihuan committed Dec 23, 2024
1 parent 4d8b744 commit 0ca2e83
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions server/common/utils/data_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,29 @@ def discover_s3_region_name(uri): # type: ignore
"""If this is an s3 protocol, discover and return the (aws) region name.
If a return name could not be discovered, or if the uri is not an s3 protocol, return None."""

protocol, _ = DataLocator._get_protocol_and_path(uri) # type: ignore
if protocol == "s3":
bucket = urlparse(uri).netloc
client = boto3.client("s3")
try:
res = client.head_bucket(Bucket=bucket)
except botocore.exceptions.ClientError:
return None

region = res.get("ResponseMetadata", {}).get("HTTPHeaders", {}).get("x-amz-bucket-region")
if region:
return region
try:
protocol, _ = DataLocator._get_protocol_and_path(uri) # type: ignore
if protocol == "s3":
bucket = urlparse(uri).netloc
print(f"Attempting to discover region for bucket: {bucket}")
client = boto3.client("s3")
try:
res = client.head_bucket(Bucket=bucket)
print(f"Received response for head_bucket: {res}")
except botocore.exceptions.ClientError as e:
print(f"ClientError occurred while accessing bucket {bucket}: {e}")
return None

region = res.get("ResponseMetadata", {}).get("HTTPHeaders", {}).get("x-amz-bucket-region")
if region:
print(f"Region for bucket {bucket} discovered: {region}")
return region
else:
print(f"Region not found in response for bucket {bucket}")
return None
else:
print(f"URI does not use the S3 protocol: {uri}")
return None
return None
except Exception as e:
print(f"An unexpected error occurred while processing URI: {uri}. Error: {e}")
return None

0 comments on commit 0ca2e83

Please sign in to comment.