s3_provider.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # (c) Nelen & Schuurmans
  3. import logging
  4. from typing import Optional
  5. from typing import TYPE_CHECKING
  6. import aioboto3
  7. from botocore.client import Config
  8. from clean_python import ValueObject
  9. if TYPE_CHECKING:
  10. from types_aiobotocore_s3.client import S3Client
  11. __all__ = ["S3BucketOptions", "S3BucketProvider"]
  12. logger = logging.getLogger(__name__)
  13. class S3BucketOptions(ValueObject):
  14. url: str
  15. access_key: str
  16. secret_key: str
  17. bucket: str
  18. region: Optional[str] = None
  19. class S3BucketProvider:
  20. def __init__(self, options: S3BucketOptions):
  21. self.options = options
  22. @property
  23. def bucket(self) -> str:
  24. return self.options.bucket
  25. @property
  26. def client(self) -> "S3Client":
  27. session = aioboto3.Session()
  28. return session.client(
  29. "s3",
  30. endpoint_url=self.options.url,
  31. aws_access_key_id=self.options.access_key,
  32. aws_secret_access_key=self.options.secret_key,
  33. region_name=self.options.region,
  34. config=Config(
  35. s3={"addressing_style": "virtual"}, # "path" will become deprecated
  36. signature_version="s3v4", # for minio
  37. retries={
  38. "max_attempts": 4, # 1 try and up to 3 retries
  39. "mode": "adaptive",
  40. },
  41. ),
  42. use_ssl=self.options.url.startswith("https"),
  43. )