Rework s3 upload system
This commit is contained in:
+14
-7
@@ -2,6 +2,7 @@ import os
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
from anyio import Condition
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from aiobotocore.session import get_session
|
from aiobotocore.session import get_session
|
||||||
|
|
||||||
@@ -18,6 +19,7 @@ class S3Worker:
|
|||||||
self._s3_client = None
|
self._s3_client = None
|
||||||
|
|
||||||
self.bucket = os.getenv('BUCKET_NAME')
|
self.bucket = os.getenv('BUCKET_NAME')
|
||||||
|
self.max_file_size = os.getenv('MAX_FILES_SIZE')
|
||||||
|
|
||||||
async def __aenter__(self):
|
async def __aenter__(self):
|
||||||
self._client = await self._s3_session.create_client(
|
self._client = await self._s3_session.create_client(
|
||||||
@@ -40,13 +42,18 @@ class S3Worker:
|
|||||||
Body=data,
|
Body=data,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def generate_upload_url(self, key: str, content_type: str, expires_in: int = 300) -> str:
|
async def generate_upload_post(self, key: str, content_type: str, expires_in: int = 300) -> dict:
|
||||||
return await self._client.generate_presigned_url("put_object",
|
return await self._client.generate_presigned_post(
|
||||||
Params={
|
Bucket=self.bucket,
|
||||||
"Bucket": self.bucket,
|
Key=key,
|
||||||
"Key": key,
|
Fields={
|
||||||
"ContentType": content_type,
|
"Content-Type": content_type,
|
||||||
|
"acl": "private",
|
||||||
},
|
},
|
||||||
|
Conditions=[
|
||||||
|
["content-length-range", 0, self.max_file_size],
|
||||||
|
{"acl": "private"},
|
||||||
|
],
|
||||||
ExpiresIn=expires_in,
|
ExpiresIn=expires_in,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -78,7 +85,7 @@ async def test_run():
|
|||||||
|
|
||||||
url = await worker.generate_download_url("test.txt", "hello.txt")
|
url = await worker.generate_download_url("test.txt", "hello.txt")
|
||||||
print(url)
|
print(url)
|
||||||
url = await worker.generate_upload_url("some.jpg", content_type="image/jpeg")
|
url = await worker.generate_upload_post("some.jpg", content_type="image/jpeg")
|
||||||
print(url)
|
print(url)
|
||||||
url = await worker.generate_download_url("some.jpg", "image.jpg")
|
url = await worker.generate_download_url("some.jpg", "image.jpg")
|
||||||
print(url)
|
print(url)
|
||||||
|
|||||||
Reference in New Issue
Block a user