Repo passa a viver dentro de infra/, com três stacks isoladas que
compartilham uma network Docker externa ('infra-net'):
main/ infra base: postgres + redis + minio + minio-init + pgadmin
+ caddy. Postgres roda em modo multi-banco; o init script
cria os DBs 'wedding' e 'gitea' com roles dedicadas. MinIO
tem um bucket inicial criado pelo minio-init com anonymous
download + CORS. Caddy é o único container expondo 80/443
e roteia por hostname: gitea.{DOMAIN_BASE} / wedding.* /
pgadmin.* / minio.* / media.* (rewrite de bucket).
gitea/ gitea + act_runner. Gitea liga no postgres compartilhado e
usa redis pra cache+sessões. O runner ganha um Dockerfile
pequeno que adiciona docker CLI por cima do
gitea/act_runner pra workflows poderem chamar 'docker
build'. Bootstrap do token de runner documentado no
.env.example.
wedding_photo/ Só a aplicação: 'wedding_app' (FastAPI + SPA) +
postgres-backup + media-backup. Os bancos e o MinIO vêm
da stack main/. A app usa extra_hosts: host-gateway pra
alcançar media.{DOMAIN_BASE} via Caddy mesmo em dev local
— assim a assinatura S3 fecha com o host que o browser
usa pra fazer PUT.
Orquestração:
- Makefile no root: 'make up' sobe tudo na ordem (main -> gitea ->
wedding_photo). 'make up-{main,gitea,wedding}' pra controle
granular. 'make logs-*', 'make down', 'make status', 'make pull-*'.
- network.sh cria a 'infra-net' antes de qualquer up; idempotente.
- Cada stack tem seu próprio .env.example. As creds compartilhadas
(DOMAIN_BASE, MINIO_ROOT_*, WEDDING_DB_*) precisam casar entre
main/.env e o consumidor (gitea/.env ou wedding_photo/.env).
- .gitignore ignora todas as pastas data/ dos volumes.
177 lines
5.5 KiB
Python
177 lines
5.5 KiB
Python
import asyncio
|
|
from typing import Any
|
|
|
|
import boto3
|
|
from botocore.client import Config
|
|
|
|
from ..config import settings
|
|
|
|
DEFAULT_PART_SIZE = 10 * 1024 * 1024
|
|
|
|
|
|
class S3Storage:
|
|
"""Thin wrapper around boto3 that fits the same shape used by routes.
|
|
|
|
All network methods are async-wrapped via asyncio.to_thread so they
|
|
can be awaited from FastAPI handlers without blocking the event loop.
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
self._client = boto3.client(
|
|
"s3",
|
|
endpoint_url=settings.S3_ENDPOINT,
|
|
region_name=settings.S3_REGION,
|
|
aws_access_key_id=settings.S3_ACCESS_KEY_ID,
|
|
aws_secret_access_key=settings.S3_SECRET_ACCESS_KEY,
|
|
config=Config(
|
|
signature_version="s3v4",
|
|
s3={
|
|
"addressing_style": "path"
|
|
if settings.S3_FORCE_PATH_STYLE
|
|
else "virtual",
|
|
},
|
|
retries={"max_attempts": 3, "mode": "standard"},
|
|
),
|
|
)
|
|
self._bucket = settings.S3_BUCKET
|
|
self._public_base = settings.S3_PUBLIC_BASE_URL.rstrip("/")
|
|
|
|
def presign_put(
|
|
self, key: str, content_type: str, expires_in: int = 600
|
|
) -> dict[str, Any]:
|
|
url = self._client.generate_presigned_url(
|
|
"put_object",
|
|
Params={
|
|
"Bucket": self._bucket,
|
|
"Key": key,
|
|
"ContentType": content_type,
|
|
},
|
|
ExpiresIn=expires_in,
|
|
)
|
|
return {
|
|
"url": url,
|
|
"method": "PUT",
|
|
"headers": {"content-type": content_type},
|
|
}
|
|
|
|
async def init_multipart(
|
|
self,
|
|
key: str,
|
|
content_type: str,
|
|
part_count: int,
|
|
expires_in: int = 3600,
|
|
) -> dict[str, Any]:
|
|
def _init() -> dict[str, Any]:
|
|
res = self._client.create_multipart_upload(
|
|
Bucket=self._bucket, Key=key, ContentType=content_type
|
|
)
|
|
return res
|
|
|
|
res = await asyncio.to_thread(_init)
|
|
upload_id: str = res["UploadId"]
|
|
|
|
def _sign_parts() -> list[str]:
|
|
urls: list[str] = []
|
|
for i in range(1, part_count + 1):
|
|
u = self._client.generate_presigned_url(
|
|
"upload_part",
|
|
Params={
|
|
"Bucket": self._bucket,
|
|
"Key": key,
|
|
"PartNumber": i,
|
|
"UploadId": upload_id,
|
|
},
|
|
ExpiresIn=expires_in,
|
|
)
|
|
urls.append(u)
|
|
return urls
|
|
|
|
part_urls = await asyncio.to_thread(_sign_parts)
|
|
return {
|
|
"upload_id": upload_id,
|
|
"part_size": DEFAULT_PART_SIZE,
|
|
"part_urls": part_urls,
|
|
}
|
|
|
|
async def complete_multipart(
|
|
self,
|
|
key: str,
|
|
upload_id: str,
|
|
parts: list[dict[str, Any]],
|
|
) -> None:
|
|
sorted_parts = sorted(parts, key=lambda p: p["partNumber"])
|
|
boto_parts = [
|
|
{"PartNumber": p["partNumber"], "ETag": p["etag"]} for p in sorted_parts
|
|
]
|
|
|
|
def _complete() -> None:
|
|
self._client.complete_multipart_upload(
|
|
Bucket=self._bucket,
|
|
Key=key,
|
|
UploadId=upload_id,
|
|
MultipartUpload={"Parts": boto_parts},
|
|
)
|
|
|
|
await asyncio.to_thread(_complete)
|
|
|
|
async def abort_multipart(self, key: str, upload_id: str) -> None:
|
|
def _abort() -> None:
|
|
try:
|
|
self._client.abort_multipart_upload(
|
|
Bucket=self._bucket, Key=key, UploadId=upload_id
|
|
)
|
|
except Exception: # noqa: BLE001
|
|
pass
|
|
|
|
await asyncio.to_thread(_abort)
|
|
|
|
def public_url(self, key: str) -> str:
|
|
return f"{self._public_base}/{key}"
|
|
|
|
async def head(self, key: str) -> dict[str, Any] | None:
|
|
def _head() -> dict[str, Any] | None:
|
|
try:
|
|
res = self._client.head_object(Bucket=self._bucket, Key=key)
|
|
return {
|
|
"contentType": res.get("ContentType", "application/octet-stream"),
|
|
"contentLength": int(res.get("ContentLength", 0)),
|
|
"etag": (res.get("ETag", "") or "").strip('"'),
|
|
}
|
|
except self._client.exceptions.ClientError as e:
|
|
code = e.response.get("Error", {}).get("Code", "")
|
|
if code in {"404", "NoSuchKey", "NotFound"}:
|
|
return None
|
|
raise
|
|
|
|
return await asyncio.to_thread(_head)
|
|
|
|
async def delete(self, key: str) -> None:
|
|
def _delete() -> None:
|
|
try:
|
|
self._client.delete_object(Bucket=self._bucket, Key=key)
|
|
except Exception: # noqa: BLE001
|
|
pass
|
|
|
|
await asyncio.to_thread(_delete)
|
|
|
|
async def get_bytes(self, key: str) -> bytes:
|
|
def _get() -> bytes:
|
|
res = self._client.get_object(Bucket=self._bucket, Key=key)
|
|
return res["Body"].read()
|
|
|
|
return await asyncio.to_thread(_get)
|
|
|
|
async def put_bytes(self, key: str, data: bytes, content_type: str) -> None:
|
|
def _put() -> None:
|
|
self._client.put_object(
|
|
Bucket=self._bucket,
|
|
Key=key,
|
|
Body=data,
|
|
ContentType=content_type,
|
|
)
|
|
|
|
await asyncio.to_thread(_put)
|
|
|
|
|
|
storage = S3Storage()
|