"""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)