Implement upload_file endpoint (#547)

This commit is contained in:
Kerem Yilmaz
2024-07-03 17:54:31 -07:00
committed by GitHub
parent 03a1b6d92c
commit 21b9eea446
3 changed files with 71 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
from enum import StrEnum
from typing import Any, Callable
from typing import IO, Any, Callable
from urllib.parse import urlparse
import aioboto3
@@ -55,6 +55,17 @@ class AsyncAWSClient:
LOG.exception("S3 upload failed.", uri=uri)
return None
@execute_with_async_client(client_type=AWSClientType.S3)
async def upload_file_stream(self, uri: str, file_obj: IO[bytes], client: AioBaseClient = None) -> str | None:
try:
parsed_uri = S3Uri(uri)
await client.upload_fileobj(file_obj, parsed_uri.bucket, parsed_uri.key)
LOG.debug("Upload file stream success", uri=uri)
return uri
except Exception:
LOG.exception("S3 upload stream failed.", uri=uri)
return None
@execute_with_async_client(client_type=AWSClientType.S3)
async def upload_file_from_path(self, uri: str, file_path: str, client: AioBaseClient = None) -> None:
try:
@@ -137,3 +148,6 @@ class S3Uri(object):
@property
def uri(self) -> str:
return self._parsed.geturl()
aws_client = AsyncAWSClient()