From 09cdac51295a3294b18d18b92cf45d82f2abf0a4 Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Thu, 24 Feb 2022 12:13:45 +0100 Subject: [PATCH] added s3 client factory --- pyinfra/exceptions.py | 3 +++ pyinfra/storage/adapters/azure.py | 1 - pyinfra/storage/adapters/s3.py | 0 pyinfra/storage/clients/s3.py | 22 ++++++++++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 pyinfra/storage/adapters/s3.py create mode 100644 pyinfra/storage/clients/s3.py diff --git a/pyinfra/exceptions.py b/pyinfra/exceptions.py index b97154f..ab3a89b 100644 --- a/pyinfra/exceptions.py +++ b/pyinfra/exceptions.py @@ -12,3 +12,6 @@ class ProcessingFailure(Exception): class UnknownStorageBackend(ValueError): pass + +class InvalidEndpoint(ValueError): + pass diff --git a/pyinfra/storage/adapters/azure.py b/pyinfra/storage/adapters/azure.py index 7f9e015..ed54f9a 100644 --- a/pyinfra/storage/adapters/azure.py +++ b/pyinfra/storage/adapters/azure.py @@ -59,7 +59,6 @@ class AzureStorageAdapter(StorageAdapter): data = blob_data.readall() yield data - @_retry(ResourceExistsError) def purge(self, bucket_name): logger.debug(f"Purging Azure container '{bucket_name}'...") container_client = self.__client.get_container_client(bucket_name) diff --git a/pyinfra/storage/adapters/s3.py b/pyinfra/storage/adapters/s3.py new file mode 100644 index 0000000..e69de29 diff --git a/pyinfra/storage/clients/s3.py b/pyinfra/storage/clients/s3.py new file mode 100644 index 0000000..05f86ad --- /dev/null +++ b/pyinfra/storage/clients/s3.py @@ -0,0 +1,22 @@ +import re + +from minio import Minio + +from pyinfra.config import CONFIG +from pyinfra.exceptions import InvalidEndpoint + + +def parse_endpoint(endpoint): + endpoint_pattern = r"(?Phttps?)://(?P
.*:(?:\d{1,3}\.){3}\d{1,3})" + + match = re.match(endpoint_pattern, endpoint) + + if not match: + raise InvalidEndpoint(f"Endpoint {endpoint} is invalid; expected {endpoint_pattern}") + + return {"secure": match.group("protocol") == "https", "endpoint": match.group("address")} + + +def get_s3_client(endpoint=None) -> Minio: + endpoint = CONFIG.s3.endpoint if endpoint is None else endpoint + return Minio(**parse_endpoint(endpoint), access_key=CONFIG.s3.secret_key, secret_key=CONFIG.s3.access_key)