Files
mtusi2026lab4/app/s3client.py
T

153 lines
5.4 KiB
Python

"""S3 client for the private attachment bucket. Object keys are chosen by the server."""
import socket
from urllib.parse import urlparse
import boto3
from botocore.config import Config
from botocore.exceptions import BotoCoreError, ClientError, EndpointConnectionError
from app.config import Settings
from app.errors import ApiError
_client = None
def client(settings: Settings):
global _client
if _client is None:
_client = boto3.client(
"s3",
endpoint_url=settings.s3_endpoint,
aws_access_key_id=settings.s3_access_key,
aws_secret_access_key=settings.s3_secret_key,
region_name=settings.s3_region,
config=Config(
signature_version="s3v4",
s3={"addressing_style": "path"},
connect_timeout=1,
read_timeout=20,
retries={"max_attempts": 1, "mode": "standard"},
),
)
return _client
def _unavailable(exc: Exception) -> ApiError:
return ApiError(503, "unavailable", "Хранилище недоступно")
def _ensure_reachable(settings: Settings) -> None:
"""Fail in about a second when the endpoint black-holes a stopped container."""
parsed = urlparse(settings.s3_endpoint)
port = parsed.port or (443 if parsed.scheme == "https" else 80)
try:
with socket.create_connection((parsed.hostname, port), timeout=1):
return
except OSError as exc:
raise _unavailable(exc) from exc
def head_bucket(settings: Settings) -> None:
_ensure_reachable(settings)
try:
client(settings).head_bucket(Bucket=settings.s3_bucket)
except (ClientError, EndpointConnectionError, BotoCoreError, OSError) as exc:
raise _unavailable(exc) from exc
def put_file(settings: Settings, key: str, path: str, content_type: str, size: int) -> None:
_ensure_reachable(settings)
try:
with open(path, "rb") as handle:
client(settings).put_object(
Bucket=settings.s3_bucket,
Key=key,
Body=handle,
ContentLength=size,
ContentType=content_type,
)
except (ClientError, EndpointConnectionError, BotoCoreError, OSError) as exc:
raise _unavailable(exc) from exc
def put_bytes(settings: Settings, key: str, payload: bytes, content_type: str = "application/octet-stream") -> None:
_ensure_reachable(settings)
try:
client(settings).put_object(
Bucket=settings.s3_bucket,
Key=key,
Body=payload,
ContentLength=len(payload),
ContentType=content_type,
)
except (ClientError, EndpointConnectionError, BotoCoreError, OSError) as exc:
raise _unavailable(exc) from exc
def get_bytes(settings: Settings, key: str, limit: int) -> bytes:
_ensure_reachable(settings)
try:
obj = client(settings).get_object(Bucket=settings.s3_bucket, Key=key)
body = obj["Body"]
try:
data = body.read(limit + 1)
finally:
body.close()
except ClientError as exc:
code = exc.response.get("Error", {}).get("Code", "")
status = exc.response.get("ResponseMetadata", {}).get("HTTPStatusCode")
if code in {"NoSuchKey", "404", "NotFound", "NoSuchBucket"} or status == 404:
raise ApiError(404, "not_found", "Объект не найден") from exc
raise _unavailable(exc) from exc
except (EndpointConnectionError, BotoCoreError, OSError) as exc:
raise _unavailable(exc) from exc
if len(data) > limit:
raise ApiError(500, "internal_error", "Внутренняя ошибка")
return data
def delete_object(settings: Settings, key: str) -> None:
_ensure_reachable(settings)
try:
client(settings).delete_object(Bucket=settings.s3_bucket, Key=key)
except (ClientError, EndpointConnectionError, BotoCoreError, OSError) as exc:
raise _unavailable(exc) from exc
def object_exists(settings: Settings, key: str) -> bool:
_ensure_reachable(settings)
try:
client(settings).head_object(Bucket=settings.s3_bucket, Key=key)
return True
except ClientError as exc:
code = exc.response.get("Error", {}).get("Code", "")
status = exc.response.get("ResponseMetadata", {}).get("HTTPStatusCode")
if code in {"NoSuchKey", "404", "NotFound", "NoSuchBucket"} or status in {404, 400}:
return False
raise _unavailable(exc) from exc
except (EndpointConnectionError, BotoCoreError, OSError) as exc:
raise _unavailable(exc) from exc
def list_objects(settings: Settings, prefix: str = "o/") -> tuple[list[dict], int]:
_ensure_reachable(settings)
found = []
pages = 0
token = None
while True:
kwargs = {"Bucket": settings.s3_bucket, "Prefix": prefix, "MaxKeys": 1000}
if token:
kwargs["ContinuationToken"] = token
try:
response = client(settings).list_objects_v2(**kwargs)
except (ClientError, EndpointConnectionError, BotoCoreError, OSError) as exc:
raise _unavailable(exc) from exc
pages += 1
found.extend(response.get("Contents") or [])
if not response.get("IsTruncated"):
return found, pages
token = response.get("NextContinuationToken")
if not token:
raise ApiError(503, "unavailable", "Хранилище вернуло неполный список")