|
12 | 12 | from datetime import datetime, timezone
|
13 | 13 | from io import BytesIO
|
14 | 14 |
|
15 |
| -import boto3 as boto3 |
| 15 | +import boto3 |
16 | 16 | import botocore
|
17 | 17 | from botocore.exceptions import ClientError
|
18 | 18 | from charms.data_platform_libs.v0.s3 import CredentialsChangedEvent, S3Requirer
|
@@ -88,6 +88,23 @@ def _tls_ca_chain_filename(self) -> str:
|
88 | 88 | return f"{self.charm._storage_path}/pgbackrest-tls-ca-chain.crt"
|
89 | 89 | return ""
|
90 | 90 |
|
| 91 | + def _get_s3_session_resource(self, s3_parameters: dict): |
| 92 | + session = boto3.session.Session( |
| 93 | + aws_access_key_id=s3_parameters["access-key"], |
| 94 | + aws_secret_access_key=s3_parameters["secret-key"], |
| 95 | + region_name=s3_parameters["region"], |
| 96 | + ) |
| 97 | + return session.resource( |
| 98 | + "s3", |
| 99 | + endpoint_url=self._construct_endpoint(s3_parameters), |
| 100 | + verify=(self._tls_ca_chain_filename or None), |
| 101 | + config=botocore.client.Config( |
| 102 | + # https://github.com/boto/boto3/issues/4400#issuecomment-2600742103 |
| 103 | + request_checksum_calculation="when_required", |
| 104 | + response_checksum_validation="when_required", |
| 105 | + ), |
| 106 | + ) |
| 107 | + |
91 | 108 | def _are_backup_settings_ok(self) -> tuple[bool, str | None]:
|
92 | 109 | """Validates whether backup settings are OK."""
|
93 | 110 | if self.model.get_relation(self.relation_name) is None:
|
@@ -227,18 +244,9 @@ def _create_bucket_if_not_exists(self) -> None:
|
227 | 244 |
|
228 | 245 | bucket_name = s3_parameters["bucket"]
|
229 | 246 | region = s3_parameters.get("region")
|
230 |
| - session = boto3.session.Session( |
231 |
| - aws_access_key_id=s3_parameters["access-key"], |
232 |
| - aws_secret_access_key=s3_parameters["secret-key"], |
233 |
| - region_name=s3_parameters["region"], |
234 |
| - ) |
235 | 247 |
|
236 | 248 | try:
|
237 |
| - s3 = session.resource( |
238 |
| - "s3", |
239 |
| - endpoint_url=self._construct_endpoint(s3_parameters), |
240 |
| - verify=(self._tls_ca_chain_filename or None), |
241 |
| - ) |
| 249 | + s3 = self._get_s3_session_resource(s3_parameters) |
242 | 250 | except ValueError as e:
|
243 | 251 | logger.exception("Failed to create a session '%s' in region=%s.", bucket_name, region)
|
244 | 252 | raise e
|
@@ -1316,17 +1324,8 @@ def _upload_content_to_s3(
|
1316 | 1324 | processed_s3_path = os.path.join(s3_parameters["path"], s3_path).lstrip("/")
|
1317 | 1325 | try:
|
1318 | 1326 | logger.info(f"Uploading content to bucket={bucket_name}, path={processed_s3_path}")
|
1319 |
| - session = boto3.session.Session( |
1320 |
| - aws_access_key_id=s3_parameters["access-key"], |
1321 |
| - aws_secret_access_key=s3_parameters["secret-key"], |
1322 |
| - region_name=s3_parameters["region"], |
1323 |
| - ) |
1324 | 1327 |
|
1325 |
| - s3 = session.resource( |
1326 |
| - "s3", |
1327 |
| - endpoint_url=self._construct_endpoint(s3_parameters), |
1328 |
| - verify=(self._tls_ca_chain_filename or None), |
1329 |
| - ) |
| 1328 | + s3 = self._get_s3_session_resource(s3_parameters) |
1330 | 1329 | bucket = s3.Bucket(bucket_name)
|
1331 | 1330 |
|
1332 | 1331 | with tempfile.NamedTemporaryFile() as temp_file:
|
@@ -1359,16 +1358,7 @@ def _read_content_from_s3(self, s3_path: str, s3_parameters: dict) -> str | None
|
1359 | 1358 | processed_s3_path = os.path.join(s3_parameters["path"], s3_path).lstrip("/")
|
1360 | 1359 | try:
|
1361 | 1360 | logger.info(f"Reading content from bucket={bucket_name}, path={processed_s3_path}")
|
1362 |
| - session = boto3.session.Session( |
1363 |
| - aws_access_key_id=s3_parameters["access-key"], |
1364 |
| - aws_secret_access_key=s3_parameters["secret-key"], |
1365 |
| - region_name=s3_parameters["region"], |
1366 |
| - ) |
1367 |
| - s3 = session.resource( |
1368 |
| - "s3", |
1369 |
| - endpoint_url=self._construct_endpoint(s3_parameters), |
1370 |
| - verify=(self._tls_ca_chain_filename or None), |
1371 |
| - ) |
| 1361 | + s3 = self._get_s3_session_resource(s3_parameters) |
1372 | 1362 | bucket = s3.Bucket(bucket_name)
|
1373 | 1363 | with BytesIO() as buf:
|
1374 | 1364 | bucket.download_fileobj(processed_s3_path, buf)
|
|
0 commit comments