Files
mtusi2026lab4/app/storage.py
T

124 lines
2.8 KiB
Python

from __future__ import annotations
import asyncio
import os
from pathlib import Path
import boto3
from botocore.config import Config
from botocore.exceptions import (
BotoCoreError,
ClientError,
)
from app.errors import ApiError
_client = None
def _unavailable() -> ApiError:
return ApiError(
503,
"unavailable",
"Сервис временно недоступен",
)
def _credential(file_variable: str) -> str:
file_path = os.environ[file_variable]
value = Path(file_path).read_text().rstrip("\r\n")
if not value:
raise ValueError("Empty S3 credential file")
return value
def bucket_name() -> str:
return os.environ["S3_BUCKET"]
def get_client():
global _client
if _client is None:
_client = boto3.client(
"s3",
endpoint_url=os.environ["S3_ENDPOINT"],
aws_access_key_id=_credential("S3_ACCESS_KEY_ID_FILE"),
aws_secret_access_key=_credential("S3_SECRET_ACCESS_KEY_FILE"),
region_name=os.getenv("S3_REGION", "us-east-1"),
config=Config(
signature_version="s3v4",
s3={"addressing_style": "path"},
connect_timeout=3,
read_timeout=10,
retries={
"mode": "standard",
"total_max_attempts": 2,
},
),
)
return _client
def _put(key: str, data: bytes, content_type: str) -> None:
get_client().put_object(
Bucket=bucket_name(),
Key=key,
Body=data,
ContentType=content_type,
)
def _get(key: str) -> bytes:
maximum = 10 * 1024 * 1024
response = get_client().get_object(
Bucket=bucket_name(),
Key=key,
)
body = response["Body"]
try:
expected_size = response["ContentLength"]
if not 1 <= expected_size <= maximum:
raise _unavailable()
data = body.read(maximum + 1)
if len(data) != expected_size or len(data) > maximum:
raise _unavailable()
return data
finally:
body.close()
async def put_object(
key: str,
data: bytes,
content_type: str,
) -> None:
try:
await asyncio.to_thread(_put, key, data, content_type)
except (BotoCoreError, ClientError, OSError, KeyError, ValueError):
raise _unavailable() from None
async def get_object(key: str) -> bytes:
try:
return await asyncio.to_thread(_get, key)
except (BotoCoreError, ClientError, OSError, KeyError, ValueError):
raise _unavailable() from None
def _check() -> None:
get_client().head_bucket(Bucket=bucket_name())
async def check_storage() -> None:
try:
await asyncio.to_thread(_check)
except (BotoCoreError, ClientError, OSError, KeyError, ValueError):
raise _unavailable() from None