From 1d8590a4417ba81434ddf70093e99335ce0daaab Mon Sep 17 00:00:00 2001 From: Corentin Forler Date: Thu, 19 Sep 2024 12:32:16 +0200 Subject: [PATCH] refactor(s3): Handle region and endpoints in delete_s3_files --- press/press/doctype/remote_file/remote_file.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/press/press/doctype/remote_file/remote_file.py b/press/press/doctype/remote_file/remote_file.py index f23c9a9e4ae..5066548a317 100644 --- a/press/press/doctype/remote_file/remote_file.py +++ b/press/press/doctype/remote_file/remote_file.py @@ -271,17 +271,26 @@ def delete_s3_files(buckets): from press.utils import chunk - press_settings = frappe.get_single("Press Settings") + press_settings: "PressSettings" = frappe.get_single("Press Settings") # type: ignore for bucket_name in buckets.keys(): + if bucket_name == press_settings.aws_s3_bucket: + endpoint_url = press_settings.offsite_backups_endpoint + region_name = press_settings.backup_region + elif bucket_name == press_settings.remote_uploads_bucket: + endpoint_url = press_settings.remote_uploads_endpoint + region_name = press_settings.remote_uploads_region + else: + endpoint_url = frappe.db.get_value("Backup Bucket", bucket_name, "endpoint_url") + region_name = frappe.db.get_value("Backup Bucket", bucket_name, "region") + s3 = resource( "s3", aws_access_key_id=press_settings.offsite_backups_access_key_id, aws_secret_access_key=press_settings.get_password( "offsite_backups_secret_access_key", raise_exception=False ), - region_name=frappe.db.get_value("Backup Bucket", bucket_name, "region") or None, - endpoint_url=frappe.db.get_value("Backup Bucket", bucket_name, "endpoint_url") - or "https://s3.amazonaws.com", + region_name=region_name or "ap-south-1", + endpoint_url=endpoint_url or "https://s3.amazonaws.com", ) bucket = s3.Bucket(bucket_name) for objects in chunk([{"Key": x} for x in buckets[bucket_name]], 1000):