more before level3 changes
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
"""Private S3 bucket. Callers pass a server-generated key; the display filename never becomes one."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from concurrent.futures import TimeoutError as FutureTimeout
|
||||
|
||||
import boto3
|
||||
from botocore.config import Config
|
||||
from botocore.exceptions import BotoCoreError, ClientError
|
||||
|
||||
from .config import ObjectSettings
|
||||
|
||||
# Docker DNS can spend several seconds on a stopped MinIO name. The HTTP handler
|
||||
# must still answer inside HAProxy's one-second health check.
|
||||
_POOL = ThreadPoolExecutor(max_workers=16, thread_name_prefix='s3')
|
||||
|
||||
|
||||
class MissingObject(Exception):
|
||||
"""The key is not in the bucket."""
|
||||
|
||||
|
||||
class StorageError(Exception):
|
||||
"""The object store could not complete the call. The message stays internal."""
|
||||
|
||||
|
||||
class ObjectStore:
|
||||
def __init__(self, settings: ObjectSettings) -> None:
|
||||
self._bucket = settings.bucket
|
||||
self._client = boto3.client(
|
||||
's3',
|
||||
endpoint_url=settings.endpoint,
|
||||
region_name=settings.region,
|
||||
aws_access_key_id=settings.access_key,
|
||||
aws_secret_access_key=settings.secret_key,
|
||||
config=Config(
|
||||
signature_version='s3v4',
|
||||
s3={'addressing_style': 'path'},
|
||||
connect_timeout=1,
|
||||
read_timeout=2,
|
||||
retries={'max_attempts': 1},
|
||||
),
|
||||
)
|
||||
|
||||
def ready(self) -> None:
|
||||
self._bounded(self._head, 0.7)
|
||||
|
||||
def put(self, key: str, body: bytes, content_type: str) -> None:
|
||||
self._bounded(lambda: self._put(key, body, content_type), 8)
|
||||
|
||||
def get(self, key: str) -> bytes:
|
||||
return self._bounded(lambda: self._get(key), 8)
|
||||
|
||||
def delete(self, key: str) -> None:
|
||||
self._bounded(lambda: self._delete(key), 8)
|
||||
|
||||
def _bounded(self, fn, timeout: float):
|
||||
future = _POOL.submit(fn)
|
||||
try:
|
||||
return future.result(timeout=timeout)
|
||||
except FutureTimeout:
|
||||
raise StorageError() from None
|
||||
|
||||
def _head(self) -> None:
|
||||
try:
|
||||
self._client.head_bucket(Bucket=self._bucket)
|
||||
except (BotoCoreError, ClientError):
|
||||
raise StorageError() from None
|
||||
|
||||
def _put(self, key: str, body: bytes, content_type: str) -> None:
|
||||
try:
|
||||
self._client.put_object(
|
||||
Bucket=self._bucket,
|
||||
Key=key,
|
||||
Body=body,
|
||||
ContentType=content_type,
|
||||
ContentLength=len(body),
|
||||
)
|
||||
except (BotoCoreError, ClientError):
|
||||
raise StorageError() from None
|
||||
|
||||
def _get(self, key: str) -> bytes:
|
||||
try:
|
||||
response = self._client.get_object(Bucket=self._bucket, Key=key)
|
||||
except ClientError as exc:
|
||||
code = exc.response.get('Error', {}).get('Code', '')
|
||||
if code in ('NoSuchKey', '404', 'NotFound'):
|
||||
raise MissingObject() from None
|
||||
raise StorageError() from None
|
||||
except BotoCoreError:
|
||||
raise StorageError() from None
|
||||
payload = response['Body']
|
||||
try:
|
||||
return payload.read()
|
||||
finally:
|
||||
payload.close()
|
||||
|
||||
def _delete(self, key: str) -> None:
|
||||
try:
|
||||
self._client.delete_object(Bucket=self._bucket, Key=key)
|
||||
except (BotoCoreError, ClientError):
|
||||
raise StorageError() from None
|
||||
Reference in New Issue
Block a user