From f2501d05568833c91e5cdba81c4e661f7ab76add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Safin?= Date: Fri, 6 Dec 2024 23:47:01 +0100 Subject: [PATCH] lazy import for upload providers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: RafaƂ Safin --- ogion/upload_providers/azure.py | 4 ++-- ogion/upload_providers/google_cloud_storage.py | 8 ++++---- ogion/upload_providers/s3.py | 7 ++++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/ogion/upload_providers/azure.py b/ogion/upload_providers/azure.py index ab303cd..3218e56 100644 --- a/ogion/upload_providers/azure.py +++ b/ogion/upload_providers/azure.py @@ -5,8 +5,6 @@ from pathlib import Path from typing import override -from azure.storage.blob import BlobServiceClient - from ogion import config, core from ogion.models.upload_provider_models import AzureProviderModel from ogion.upload_providers.base_provider import BaseUploadProvider @@ -18,6 +16,8 @@ class UploadProviderAzure(BaseUploadProvider): """Azure blob storage for storing backups""" def __init__(self, target_provider: AzureProviderModel) -> None: + from azure.storage.blob import BlobServiceClient + self.container_name = target_provider.container_name blob_service_client = BlobServiceClient.from_connection_string( diff --git a/ogion/upload_providers/google_cloud_storage.py b/ogion/upload_providers/google_cloud_storage.py index 0445276..892f856 100644 --- a/ogion/upload_providers/google_cloud_storage.py +++ b/ogion/upload_providers/google_cloud_storage.py @@ -8,10 +8,6 @@ from pathlib import Path from typing import override -import google.cloud.storage as cloud_storage -from google.auth.credentials import AnonymousCredentials -from google.oauth2 import service_account - from ogion import config, core from ogion.models.upload_provider_models import GCSProviderModel from ogion.upload_providers.base_provider import BaseUploadProvider @@ -23,6 +19,10 @@ class UploadProviderGCS(BaseUploadProvider): """GCS bucket for storing backups""" def __init__(self, target_provider: GCSProviderModel) -> None: + import google.cloud.storage as cloud_storage + from google.auth.credentials import AnonymousCredentials + from google.oauth2 import service_account + service_account_bytes = base64.b64decode( target_provider.service_account_base64.get_secret_value() ) diff --git a/ogion/upload_providers/s3.py b/ogion/upload_providers/s3.py index 84127bb..5b04a13 100644 --- a/ogion/upload_providers/s3.py +++ b/ogion/upload_providers/s3.py @@ -5,9 +5,6 @@ from pathlib import Path from typing import override -from minio import Minio -from minio.deleteobjects import DeleteObject - from ogion import config, core from ogion.models.upload_provider_models import S3ProviderModel from ogion.upload_providers.base_provider import BaseUploadProvider @@ -19,6 +16,8 @@ class UploadProviderS3(BaseUploadProvider): """S3 compatibile storage bucket for storing backups""" def __init__(self, target_provider: S3ProviderModel) -> None: + from minio import Minio + self.bucket_upload_path = target_provider.bucket_upload_path self.max_bandwidth = target_provider.max_bandwidth @@ -82,6 +81,8 @@ def download_backup(self, path: str) -> Path: def clean( self, backup_file: Path, max_backups: int, min_retention_days: int ) -> None: + from minio.deleteobjects import DeleteObject + for backup_path in backup_file.parent.iterdir(): core.remove_path(backup_path) log.info("removed %s from local disk", backup_path)