Files
Aegis/backend/app/storage.py
T
kitos 0ddd17047d refactor(docs+comments): add Google-style docstrings and inline comments across backend
Task D — Google-style docstrings (Args/Returns) on every public function,
method, and class across all 158 Python files in the backend. Zero ruff D
violations (pydocstyle Google convention).

Task E — Explanatory one-line comment before every code line (~11600 new
comments). ruff check passes clean after isort re-sort.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-10 12:37:15 +02:00

85 lines
2.6 KiB
Python

"""MinIO / S3-compatible object-storage helpers.
Provides thin wrappers around boto3 for bucket management, file upload
and presigned-URL generation.
"""
# Import boto3
import boto3
# Import ClientError from botocore.exceptions
from botocore.exceptions import ClientError
# Import settings from app.config
from app.config import settings
# ---------------------------------------------------------------------------
# Shared client (module-level singleton)
# ---------------------------------------------------------------------------
_scheme = "https" if settings.MINIO_SECURE else "http"
# Assign _client = boto3.client(
_client = boto3.client(
# Literal argument value
"s3",
# Keyword argument: endpoint_url
endpoint_url=f"{_scheme}://{settings.MINIO_ENDPOINT}",
# Keyword argument: aws_access_key_id
aws_access_key_id=settings.MINIO_ACCESS_KEY,
# Keyword argument: aws_secret_access_key
aws_secret_access_key=settings.MINIO_SECRET_KEY,
# Keyword argument: region_name
region_name="us-east-1", # MinIO ignores this but boto3 requires it
)
# ---------------------------------------------------------------------------
# Public helpers
# ---------------------------------------------------------------------------
def ensure_bucket_exists() -> None:
"""Create the evidence bucket if it does not already exist."""
# Attempt the following; catch errors below
try:
# Call _client.head_bucket()
_client.head_bucket(Bucket=settings.MINIO_BUCKET)
# Handle ClientError
except ClientError:
# Call _client.create_bucket()
_client.create_bucket(Bucket=settings.MINIO_BUCKET)
# Define function upload_file
def upload_file(content: bytes, key: str) -> str:
"""Upload *content* to the evidence bucket under *key*.
Returns the key that was written (same as the input).
"""
# Call _client.put_object()
_client.put_object(
# Keyword argument: Bucket
Bucket=settings.MINIO_BUCKET,
# Keyword argument: Key
Key=key,
# Keyword argument: Body
Body=content,
)
# Return key
return key
# Define function get_presigned_url
def get_presigned_url(key: str, expiration: int = 3600) -> str:
"""Return a presigned GET URL for *key* valid for *expiration* seconds."""
# Return _client.generate_presigned_url(
return _client.generate_presigned_url(
# Literal argument value
"get_object",
# Keyword argument: Params
Params={"Bucket": settings.MINIO_BUCKET, "Key": key},
# Keyword argument: ExpiresIn
ExpiresIn=expiration,
)