forked from mtusi2026/mtusi2026lab4
solution
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
"""S3 client: one bounded PUT, no implicit write retries, streaming GET."""
|
||||
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from minio import Minio
|
||||
from minio.error import MinioException
|
||||
import urllib3
|
||||
|
||||
from .errors import ApiError
|
||||
|
||||
STORAGE_ERRORS = (MinioException, urllib3.exceptions.HTTPError, OSError)
|
||||
MAX_BYTES = 10 * 1024 * 1024
|
||||
|
||||
|
||||
class StorageUnavailable(Exception):
|
||||
"""Storage error without sensitive endpoint/credentials in its message."""
|
||||
|
||||
|
||||
class MinioStorage:
|
||||
def __init__(self, settings):
|
||||
endpoint = urlsplit(settings.s3_endpoint_url)
|
||||
if endpoint.scheme not in ("http", "https") or not endpoint.netloc:
|
||||
raise ValueError("S3_ENDPOINT_URL must be an http(s) endpoint")
|
||||
if endpoint.path not in ("", "/") or endpoint.username or endpoint.password:
|
||||
raise ValueError("S3 endpoint must not contain a path or credentials")
|
||||
if not settings.s3_access_key or not settings.s3_secret_key:
|
||||
raise ValueError("S3 credentials are required")
|
||||
self.bucket = settings.s3_bucket
|
||||
self.http = urllib3.PoolManager(
|
||||
num_pools=2, maxsize=8, block=False,
|
||||
timeout=urllib3.Timeout(connect=3, read=10), retries=False,
|
||||
)
|
||||
# Readiness has its own short deadline, so S3 outage cannot accumulate probes.
|
||||
self.probe_http = urllib3.PoolManager(
|
||||
num_pools=1, maxsize=2, timeout=urllib3.Timeout(connect=0.5, read=0.5), retries=False,
|
||||
)
|
||||
options = dict(
|
||||
endpoint=endpoint.netloc, access_key=settings.s3_access_key,
|
||||
secret_key=settings.s3_secret_key, secure=endpoint.scheme == "https",
|
||||
region=settings.s3_region,
|
||||
)
|
||||
self.client = Minio(**options, http_client=self.http)
|
||||
self.probe = Minio(**options, http_client=self.probe_http)
|
||||
|
||||
def put(self, key, stream, size, content_type):
|
||||
try:
|
||||
stream.seek(0)
|
||||
# All accepted files fit ONE part: no abandoned multipart upload state.
|
||||
self.client.put_object(self.bucket, key, stream, size, content_type,
|
||||
part_size=MAX_BYTES, num_parallel_uploads=1)
|
||||
except STORAGE_ERRORS:
|
||||
raise StorageUnavailable() from None
|
||||
|
||||
def get(self, key):
|
||||
try:
|
||||
return self.client.get_object(self.bucket, key)
|
||||
except STORAGE_ERRORS:
|
||||
raise StorageUnavailable() from None
|
||||
|
||||
def stat(self, key):
|
||||
try:
|
||||
return self.client.stat_object(self.bucket, key)
|
||||
except STORAGE_ERRORS:
|
||||
raise StorageUnavailable() from None
|
||||
|
||||
def delete(self, key):
|
||||
try:
|
||||
self.client.remove_object(self.bucket, key)
|
||||
except STORAGE_ERRORS:
|
||||
raise StorageUnavailable() from None
|
||||
|
||||
def ready(self):
|
||||
try:
|
||||
if not self.probe.bucket_exists(self.bucket):
|
||||
raise StorageUnavailable()
|
||||
except STORAGE_ERRORS:
|
||||
raise StorageUnavailable() from None
|
||||
|
||||
def close(self):
|
||||
self.http.clear()
|
||||
self.probe_http.clear()
|
||||
|
||||
|
||||
def unavailable():
|
||||
return ApiError(503, "unavailable", "Хранилище временно недоступно")
|
||||
Reference in New Issue
Block a user