Skip to content

Commit

Permalink
Make region and bucket configuration dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSuperiorStanislav committed Aug 8, 2024
1 parent fd3107d commit 69014c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 7 additions & 1 deletion saritasa_s3_tools/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,24 @@
[],
str | None,
]
RegionGetter = collections.abc.Callable[
[],
str,
]


def get_boto3_s3_client(
access_key_getter: AccessKeyGetter,
s3_endpoint_url_getter: S3EndpointUrlGetter | None = None,
region: str = "",
region: RegionGetter | str = "",
max_pool_connections: int = 100,
) -> mypy_boto3_s3.S3Client:
"""Prepare boto3's s3 client for usage."""
endpoint_url = None
if s3_endpoint_url_getter:
endpoint_url = s3_endpoint_url_getter()
if callable(region):
region = region()
credentials = access_key_getter()
return boto3.client(
service_name="s3", # type: ignore
Expand Down
16 changes: 13 additions & 3 deletions saritasa_s3_tools/factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections.abc
import io
import random

Expand All @@ -9,6 +10,11 @@

from . import client, configs

BucketGetter = collections.abc.Callable[
[],
str,
]


class S3FileField(factory.LazyAttribute):
"""Generate file and upload to s3."""
Expand All @@ -18,8 +24,8 @@ class S3FileField(factory.LazyAttribute):
def __init__(
self,
s3_config: str,
s3_region: str,
bucket: str,
s3_region: client.RegionGetter | str,
bucket: BucketGetter | str,
access_key_getter: client.AccessKeyGetter,
s3_endpoint_url_getter: client.S3EndpointUrlGetter | None = None,
filename: str = "example.txt",
Expand Down Expand Up @@ -48,9 +54,13 @@ def _get_boto3(self) -> mypy_boto3_s3.S3Client:

def _get_s3_client(self) -> client.S3Client:
"""Set up s3 client."""
if isinstance(self.bucket, str):
bucket = self.bucket
if callable(self.bucket):
bucket = self.bucket()
return client.S3Client(
boto3_client=self._get_boto3(),
default_bucket=self.bucket,
default_bucket=bucket,
)

def _generate_file_data(self) -> bytes:
Expand Down

0 comments on commit 69014c0

Please sign in to comment.