Files
mtusi2026lab6/app/s3.py
T
dawuduk 14ab803529 Перенести рабочую реализацию lab5 в skeleton лабораторной 6.
API, worker, брокер и стенд остаются базой, контракт lab6 не менялся.
2026-11-17 15:15:42 +03:00

164 lines
4.7 KiB
Python

"""Private bucket access. The API never redirects a client to this endpoint."""
from __future__ import annotations
import asyncio
import logging
import boto3
from botocore.client import Config
from botocore.exceptions import BotoCoreError, ClientError
from app import config
from app.errors import AppError
logger = logging.getLogger("messenger.s3")
_client = None
_signer = None
def _client_or_create():
global _client
if _client is None:
if not config.S3_ACCESS_KEY_ID or not config.S3_SECRET_ACCESS_KEY:
raise AppError(503, "service unavailable")
_client = boto3.client(
"s3",
endpoint_url=config.S3_ENDPOINT,
aws_access_key_id=config.S3_ACCESS_KEY_ID,
aws_secret_access_key=config.S3_SECRET_ACCESS_KEY,
region_name=config.S3_REGION,
config=Config(
signature_version="s3v4",
s3={"addressing_style": "path"},
connect_timeout=2,
read_timeout=20,
retries={"max_attempts": 2},
),
)
return _client
def _signer_or_create():
"""Signs URLs for the host the browser/client can reach. It does not send traffic."""
global _signer
if _signer is None:
if not config.S3_ACCESS_KEY_ID or not config.S3_SECRET_ACCESS_KEY:
raise AppError(503, "service unavailable")
_signer = boto3.client(
"s3",
endpoint_url=config.S3_PUBLIC_ENDPOINT,
aws_access_key_id=config.S3_ACCESS_KEY_ID,
aws_secret_access_key=config.S3_SECRET_ACCESS_KEY,
region_name=config.S3_REGION,
config=Config(
signature_version="s3v4",
s3={"addressing_style": "path"},
),
)
return _signer
def _unavailable(exc: Exception) -> AppError:
code = ""
if isinstance(exc, ClientError):
code = exc.response.get("Error", {}).get("Code", "")
logger.warning("object storage call failed code=%s", code or type(exc).__name__)
return AppError(503, "service unavailable")
async def head_bucket() -> None:
def _head() -> None:
_client_or_create().head_bucket(Bucket=config.S3_BUCKET)
try:
await asyncio.to_thread(_head)
except AppError:
raise
except (BotoCoreError, ClientError) as exc:
raise _unavailable(exc) from None
async def put_bytes(key: str, data: bytes, content_type: str) -> None:
def _put() -> None:
_client_or_create().put_object(
Bucket=config.S3_BUCKET,
Key=key,
Body=data,
ContentType=content_type,
ContentLength=len(data),
)
try:
await asyncio.to_thread(_put)
except AppError:
raise
except (BotoCoreError, ClientError) as exc:
raise _unavailable(exc) from None
async def get_bytes(key: str) -> bytes:
def _get() -> bytes:
response = _client_or_create().get_object(Bucket=config.S3_BUCKET, Key=key)
body = response["Body"].read()
response["Body"].close()
return body
try:
return await asyncio.to_thread(_get)
except AppError:
raise
except (BotoCoreError, ClientError) as exc:
raise _unavailable(exc) from None
def presign_put(key: str, content_type: str, size: int) -> str:
return _signer_or_create().generate_presigned_url(
"put_object",
Params={
"Bucket": config.S3_BUCKET,
"Key": key,
"ContentType": content_type,
"ContentLength": size,
},
ExpiresIn=config.PRESIGN_TTL_SECONDS,
HttpMethod="PUT",
)
async def head_size(key: str) -> int | None:
def _head() -> int | None:
try:
response = _client_or_create().head_object(Bucket=config.S3_BUCKET, Key=key)
except ClientError as exc:
code = exc.response.get("Error", {}).get("Code", "")
if code in {"404", "NoSuchKey", "NotFound"}:
return None
raise
return int(response["ContentLength"])
try:
return await asyncio.to_thread(_head)
except AppError:
raise
except (BotoCoreError, ClientError) as exc:
raise _unavailable(exc) from None
async def delete_key(key: str) -> None:
def _delete() -> None:
try:
_client_or_create().delete_object(Bucket=config.S3_BUCKET, Key=key)
except ClientError as exc:
code = exc.response.get("Error", {}).get("Code", "")
if code in {"NoSuchKey", "404", "NoSuchBucket"}:
return
raise
try:
await asyncio.to_thread(_delete)
except AppError:
raise
except (BotoCoreError, ClientError) as exc:
raise _unavailable(exc) from None