Реализовать асинхронную обработку вложений
This commit is contained in:
@@ -5,6 +5,7 @@ import certifi
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import BinaryIO, Iterator
|
||||
from io import BytesIO
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from minio import Minio
|
||||
@@ -57,6 +58,7 @@ class S3Storage:
|
||||
secure=parsed.scheme == 'https',
|
||||
region=region,
|
||||
http_client=PoolManager(
|
||||
maxsize=8,
|
||||
timeout=Timeout(connect=2, read=5),
|
||||
retries=Retry(total=1, connect=1, read=1, backoff_factor=0.1),
|
||||
cert_reqs='CERT_REQUIRED',
|
||||
@@ -102,6 +104,18 @@ class S3Storage:
|
||||
self._translate(error)
|
||||
return ObjectStream(response)
|
||||
|
||||
async def read(self, key: str, max_bytes: int) -> bytes:
|
||||
stream = await self.get(key)
|
||||
result = bytearray()
|
||||
for chunk in stream.chunks():
|
||||
result.extend(chunk)
|
||||
if len(result) > max_bytes:
|
||||
raise ValueError('stored object exceeds configured byte limit')
|
||||
return bytes(result)
|
||||
|
||||
async def put_bytes(self, key: str, content: bytes, content_type: str) -> None:
|
||||
await self.put(key, BytesIO(content), len(content), content_type)
|
||||
|
||||
async def exists(self, key: str) -> bool:
|
||||
try:
|
||||
await asyncio.to_thread(self._client.stat_object, self.bucket, key)
|
||||
|
||||
Reference in New Issue
Block a user