Introduce typed config objects, optional adapter injection, and .env loading to simplify testing while preserving env-based defaults for production usage. Co-authored-by: Cursor <[email protected]>
49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
"""MinIO connection configuration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
|
|
from python_utils import check_env
|
|
|
|
from python_repositories.config.dotenv_loader import load_dotenv
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class MinioConfig:
|
|
"""Configuration for connecting to MinIO."""
|
|
|
|
endpoint: str
|
|
access_key: str
|
|
secret_key: str
|
|
bucket: str
|
|
secure: bool = False
|
|
|
|
@classmethod
|
|
def from_env(
|
|
cls,
|
|
endpoint_env_var_name: str = "MINIO_ENDPOINT",
|
|
access_key_env_var_name: str = "MINIO_ACCESS_KEY",
|
|
secret_key_env_var_name: str = "MINIO_SECRET_KEY",
|
|
bucket_env_var_name: str = "MINIO_BUCKET",
|
|
*,
|
|
use_dotenv: bool = True,
|
|
) -> MinioConfig:
|
|
"""Load configuration from environment variables."""
|
|
if use_dotenv:
|
|
load_dotenv()
|
|
env_var_names = {
|
|
endpoint_env_var_name,
|
|
access_key_env_var_name,
|
|
secret_key_env_var_name,
|
|
bucket_env_var_name,
|
|
}
|
|
check_env(env_var_names)
|
|
return cls(
|
|
endpoint=str(os.getenv(endpoint_env_var_name)),
|
|
access_key=str(os.getenv(access_key_env_var_name)),
|
|
secret_key=str(os.getenv(secret_key_env_var_name)),
|
|
bucket=str(os.getenv(bucket_env_var_name)),
|
|
)
|