forked from mtusi2026/mtusi2026lab4
Добавить приватное хранилище вложений и отдачу файла через API.
Байты лежат в SeaweedFS, метаданные в Postgres. Другая реплика скачивает тот же объект, потому что локальный диск API не является источником файла.
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
"""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
|
||||
|
||||
|
||||
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 _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
|
||||
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user