Skip to content

Commit 1301de0

Browse files
[DPE-5908] Support old bucket endpoints format (#570)
1 parent e1be970 commit 1301de0

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

Diff for: lib/charms/mysql/v0/s3_helpers.py

+26-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing import Dict, List, Tuple
2222

2323
import boto3
24+
import botocore
2425

2526
logger = logging.getLogger(__name__)
2627

@@ -32,13 +33,36 @@
3233

3334
# Increment this PATCH version before using `charmcraft publish-lib` or reset
3435
# to 0 if you are raising the major API version
35-
LIBPATCH = 9
36+
LIBPATCH = 10
3637

3738
# botocore/urllib3 clutter the logs when on debug
3839
logging.getLogger("botocore").setLevel(logging.WARNING)
3940
logging.getLogger("urllib3").setLevel(logging.WARNING)
4041

4142

43+
def _construct_endpoint(s3_parameters: dict) -> str:
44+
"""Construct the S3 service endpoint using the region.
45+
46+
This is needed when the provided endpoint is from AWS, and it doesn't contain the region.
47+
"""
48+
# Use the provided endpoint if a region is not needed.
49+
endpoint = s3_parameters["endpoint"]
50+
51+
# Load endpoints data.
52+
loader = botocore.loaders.create_loader()
53+
data = loader.load_data("endpoints")
54+
55+
# Construct the endpoint using the region.
56+
resolver = botocore.regions.EndpointResolver(data)
57+
endpoint_data = resolver.construct_endpoint("s3", s3_parameters["region"])
58+
59+
# Use the built endpoint if it is an AWS endpoint.
60+
if endpoint_data and endpoint.endswith(endpoint_data["dnsSuffix"]):
61+
endpoint = f"{endpoint.split('://')[0]}://{endpoint_data['hostname']}"
62+
63+
return endpoint
64+
65+
4266
def upload_content_to_s3(content: str, content_path: str, s3_parameters: Dict) -> bool:
4367
"""Uploads the provided contents to the provided S3 bucket.
4468
@@ -67,11 +91,7 @@ def upload_content_to_s3(content: str, content_path: str, s3_parameters: Dict) -
6791
ca_file.flush()
6892
verif = ca_file.name
6993

70-
s3 = session.resource(
71-
"s3",
72-
endpoint_url=s3_parameters["endpoint"],
73-
verify=verif,
74-
)
94+
s3 = session.resource("s3", endpoint_url=_construct_endpoint(s3_parameters), verify=verif)
7595

7696
bucket = s3.Bucket(s3_parameters["bucket"])
7797

0 commit comments

Comments
 (0)