|
21 | 21 | from typing import Dict, List, Tuple
|
22 | 22 |
|
23 | 23 | import boto3
|
| 24 | +import botocore |
24 | 25 |
|
25 | 26 | logger = logging.getLogger(__name__)
|
26 | 27 |
|
|
32 | 33 |
|
33 | 34 | # Increment this PATCH version before using `charmcraft publish-lib` or reset
|
34 | 35 | # to 0 if you are raising the major API version
|
35 |
| -LIBPATCH = 9 |
| 36 | +LIBPATCH = 10 |
36 | 37 |
|
37 | 38 | # botocore/urllib3 clutter the logs when on debug
|
38 | 39 | logging.getLogger("botocore").setLevel(logging.WARNING)
|
39 | 40 | logging.getLogger("urllib3").setLevel(logging.WARNING)
|
40 | 41 |
|
41 | 42 |
|
| 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 | + |
42 | 66 | def upload_content_to_s3(content: str, content_path: str, s3_parameters: Dict) -> bool:
|
43 | 67 | """Uploads the provided contents to the provided S3 bucket.
|
44 | 68 |
|
@@ -67,11 +91,7 @@ def upload_content_to_s3(content: str, content_path: str, s3_parameters: Dict) -
|
67 | 91 | ca_file.flush()
|
68 | 92 | verif = ca_file.name
|
69 | 93 |
|
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) |
75 | 95 |
|
76 | 96 | bucket = s3.Bucket(s3_parameters["bucket"])
|
77 | 97 |
|
|
0 commit comments