89 lines
2.3 KiB
Python
89 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import io
|
|
import logging
|
|
import os
|
|
import uuid
|
|
from typing import Optional
|
|
|
|
import boto3
|
|
from botocore.client import Config
|
|
from botocore.exceptions import ClientError
|
|
|
|
log = logging.getLogger("messenger.storage")
|
|
|
|
_client = None
|
|
|
|
|
|
def _env(name: str, default: Optional[str] = None) -> str:
|
|
value = os.environ.get(name, default)
|
|
if value is None:
|
|
raise RuntimeError(f"Environment variable {name} is required")
|
|
return value
|
|
|
|
|
|
def init_storage() -> None:
|
|
global _client
|
|
if _client is not None:
|
|
return
|
|
_client = boto3.client(
|
|
"s3",
|
|
endpoint_url=_env("S3_ENDPOINT"),
|
|
region_name=_env("S3_REGION", "us-east-1"),
|
|
aws_access_key_id=_env("S3_ACCESS_KEY_ID"),
|
|
aws_secret_access_key=_env("S3_SECRET_ACCESS_KEY"),
|
|
config=Config(signature_version="s3v4", s3={"addressing_style": "path"}),
|
|
)
|
|
log.info("storage client initialized")
|
|
|
|
|
|
def _bucket() -> str:
|
|
return _env("S3_BUCKET", "messenger-attachments")
|
|
|
|
|
|
def new_object_key(content_type: str) -> str:
|
|
ext = "png" if content_type == "image/png" else "jpg"
|
|
return f"attachments/{uuid.uuid4().hex}.{ext}"
|
|
|
|
|
|
def put_object(key: str, body: bytes, content_type: str) -> str:
|
|
if _client is None:
|
|
raise RuntimeError("storage not initialized")
|
|
sha256 = hashlib.sha256(body).hexdigest()
|
|
_client.put_object(
|
|
Bucket=_bucket(),
|
|
Key=key,
|
|
Body=body,
|
|
ContentType=content_type,
|
|
Metadata={"sha256": sha256},
|
|
)
|
|
return sha256
|
|
|
|
|
|
def get_object(key: str) -> bytes:
|
|
if _client is None:
|
|
raise RuntimeError("storage not initialized")
|
|
response = _client.get_object(Bucket=_bucket(), Key=key)
|
|
return response["Body"].read()
|
|
|
|
|
|
def delete_object(key: str) -> None:
|
|
if _client is None:
|
|
return
|
|
try:
|
|
_client.delete_object(Bucket=_bucket(), Key=key)
|
|
except ClientError as exc:
|
|
log.warning("failed to delete object %s: %s", key, exc)
|
|
|
|
|
|
def object_exists(key: str) -> bool:
|
|
if _client is None:
|
|
return False
|
|
try:
|
|
_client.head_object(Bucket=_bucket(), Key=key)
|
|
return True
|
|
except ClientError as exc:
|
|
if exc.response["Error"]["Code"] in ("404", "NoSuchKey"):
|
|
return False
|
|
raisev |