Files
mtusi2026lab4/app/storage.py
T
Artu-RitoandClaude Sonnet 5 38268f63f8 Лаба 4: SeaweedFS S3-хранилище и вложения (оценка 4)
Переиспользован стек lab3 (LB/2 реплики/Postgres/Redis/идемпотентность).
Добавлены: S3-хранилище байтов вложений (SeaweedFS вместо MinIO — публичные
MinIO-образы недоступны без авторизации), метаданные attachments в Postgres,
проверка изображений по содержимому с защитой от decompression bomb,
ограниченные S3-credentials, скрипт сверки orphan-объектов. make test/
test-persistence/test-failover/validate зелёные для обеих реплик; собраны
доказательства по превышению размера, MIME-спуфингу, чужим attachment_id,
приватности bucket, конкурентным загрузкам и orphan-очистке — см. REPORT.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 00:59:13 +03:00

64 lines
2.0 KiB
Python

"""Private S3-compatible object storage for attachment bytes (MinIO behind the scenes).
boto3 is synchronous; calls are pushed to a thread so the event loop is not
blocked by S3 I/O. Object keys are server-generated UUIDs, never the
user-supplied filename (filename is display-only, see contracts/README.md).
"""
import asyncio
import io
import uuid
import boto3
from botocore.client import Config
from botocore.exceptions import ClientError
_client = None
_bucket: str | None = None
def init(endpoint: str, region: str, access_key: str, secret_key: str, bucket: str) -> None:
global _client, _bucket
_bucket = bucket
_client = boto3.client(
"s3",
endpoint_url=endpoint,
region_name=region,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
config=Config(signature_version="s3v4", retries={"max_attempts": 2}),
)
async def ensure_bucket() -> None:
"""The API identity only has Read/Write/List on this one bucket, not
bucket-admin rights, so CreateBucket is not an option. SeaweedFS (like S3)
auto-creates the bucket on the first successful PutObject, so writing an
empty marker object both creates the bucket and proves write access."""
await asyncio.to_thread(_client.put_object, Bucket=_bucket, Key=".bootstrap", Body=b"")
async def ping() -> bool:
try:
await asyncio.to_thread(_client.head_object, Bucket=_bucket, Key=".bootstrap")
return True
except Exception: # noqa: BLE001 - readiness probe
return False
def new_object_key() -> str:
return f"attachments/{uuid.uuid4().hex}"
async def put_object(key: str, data: bytes, content_type: str) -> None:
await asyncio.to_thread(
_client.put_object, Bucket=_bucket, Key=key, Body=data, ContentType=content_type
)
async def get_object(key: str) -> bytes:
def _get():
resp = _client.get_object(Bucket=_bucket, Key=key)
return resp["Body"].read()
return await asyncio.to_thread(_get)