added s3 client factory

This commit is contained in:
Matthias Bisping 2022-02-24 12:13:45 +01:00
parent b1d844a638
commit 09cdac5129
4 changed files with 25 additions and 1 deletions

View File

@ -12,3 +12,6 @@ class ProcessingFailure(Exception):
class UnknownStorageBackend(ValueError):
pass
class InvalidEndpoint(ValueError):
pass

View File

@ -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)

View File

View File

@ -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"(?P<protocol>https?)://(?P<address>.*:(?:\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)