Initial commit: UMB Telegram Bot
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import os
|
||||
import logging
|
||||
|
||||
import boto3
|
||||
from botocore.config import Config
|
||||
|
||||
from config import ACCESS_KEY, SECRET_KEY, BUCKET_NAME
|
||||
|
||||
logger = logging.getLogger("s3_client")
|
||||
|
||||
ENDPOINT_URL = "https://storage.yandexcloud.net"
|
||||
|
||||
def _build_s3_client():
|
||||
return boto3.client(
|
||||
"s3",
|
||||
endpoint_url=ENDPOINT_URL,
|
||||
aws_access_key_id=ACCESS_KEY,
|
||||
aws_secret_access_key=SECRET_KEY,
|
||||
config=Config(
|
||||
connect_timeout=30,
|
||||
read_timeout=300,
|
||||
retries={"max_attempts": 3},
|
||||
),
|
||||
)
|
||||
|
||||
def upload_file(file_path: str) -> str | None:
|
||||
key_name = os.path.basename(file_path)
|
||||
try:
|
||||
client = _build_s3_client()
|
||||
client.upload_file(file_path, BUCKET_NAME, key_name)
|
||||
file_url = f"{ENDPOINT_URL}/{BUCKET_NAME}/{key_name}"
|
||||
logger.info("Файл загружен в S3: %s", file_url)
|
||||
return file_url
|
||||
except Exception as exc:
|
||||
logger.exception("Ошибка загрузки в S3: %s", exc)
|
||||
return None
|
||||
Reference in New Issue
Block a user