feat: aioboto3 for instead of boto3
All checks were successful
Deploy on Push / deploy (push) Successful in 1m33s
All checks were successful
Deploy on Push / deploy (push) Successful in 1m33s
This commit is contained in:
parent
72fc9bd667
commit
ef2c902b32
27
main.py
27
main.py
|
@ -1,8 +1,7 @@
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import uuid
|
import uuid
|
||||||
import boto3
|
import aioboto3 # Ensure this is only imported once
|
||||||
from botocore.exceptions import BotoCoreError, ClientError
|
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.responses import JSONResponse
|
from starlette.responses import JSONResponse
|
||||||
from starlette.routing import Route
|
from starlette.routing import Route
|
||||||
|
@ -16,7 +15,6 @@ STORJ_END_POINT = os.environ.get('STORJ_END_POINT')
|
||||||
STORJ_BUCKET_NAME = os.environ.get('STORJ_BUCKET_NAME')
|
STORJ_BUCKET_NAME = os.environ.get('STORJ_BUCKET_NAME')
|
||||||
CDN_DOMAIN = os.environ.get('CDN_DOMAIN')
|
CDN_DOMAIN = os.environ.get('CDN_DOMAIN')
|
||||||
|
|
||||||
|
|
||||||
@check_auth
|
@check_auth
|
||||||
async def upload_handler(request: Request):
|
async def upload_handler(request: Request):
|
||||||
form = await request.form()
|
form = await request.form()
|
||||||
|
@ -28,36 +26,28 @@ async def upload_handler(request: Request):
|
||||||
file_name, file_extension = os.path.splitext(file.filename)
|
file_name, file_extension = os.path.splitext(file.filename)
|
||||||
key = str(uuid.uuid4()) + file_extension
|
key = str(uuid.uuid4()) + file_extension
|
||||||
|
|
||||||
s3 = boto3.client('s3',
|
async with aioboto3.client('s3',
|
||||||
aws_access_key_id=STORJ_ACCESS_KEY,
|
aws_access_key_id=STORJ_ACCESS_KEY,
|
||||||
aws_secret_access_key=STORJ_SECRET_KEY,
|
aws_secret_access_key=STORJ_SECRET_KEY,
|
||||||
endpoint_url=STORJ_END_POINT)
|
endpoint_url=STORJ_END_POINT) as s3:
|
||||||
|
|
||||||
try:
|
|
||||||
with tempfile.NamedTemporaryFile() as tmp_file:
|
with tempfile.NamedTemporaryFile() as tmp_file:
|
||||||
while True:
|
while True:
|
||||||
chunk = await file.read(8192) # 8192 bytes by default.
|
chunk = await file.read(8192)
|
||||||
if not chunk:
|
if not chunk:
|
||||||
break
|
break
|
||||||
tmp_file.write(chunk)
|
tmp_file.write(chunk)
|
||||||
|
tmp_file.flush()
|
||||||
|
|
||||||
s3.upload_file(
|
await s3.upload_file(
|
||||||
Filename=tmp_file.name,
|
Filename=tmp_file.name,
|
||||||
Bucket=STORJ_BUCKET_NAME,
|
Bucket=STORJ_BUCKET_NAME,
|
||||||
Key=key,
|
Key=key,
|
||||||
ExtraArgs={
|
ExtraArgs={"ContentType": file.content_type}
|
||||||
"ContentType": file.content_type
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
url = 'http://' + CDN_DOMAIN + '/' + key
|
url = f'http://{CDN_DOMAIN}/{key}'
|
||||||
|
|
||||||
return JSONResponse({'url': url, 'originalFilename': file.filename})
|
return JSONResponse({'url': url, 'originalFilename': file.filename})
|
||||||
|
|
||||||
except (BotoCoreError, ClientError) as e:
|
|
||||||
print(e)
|
|
||||||
return JSONResponse({'error': 'Failed to upload file'}, status_code=500)
|
|
||||||
|
|
||||||
async def home(request: Request):
|
async def home(request: Request):
|
||||||
return JSONResponse({'message': 'Hello World!'})
|
return JSONResponse({'message': 'Hello World!'})
|
||||||
|
|
||||||
|
@ -71,4 +61,3 @@ app = Starlette(debug=True, routes=routes)
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import uvicorn
|
import uvicorn
|
||||||
uvicorn.run(app, host='0.0.0.0', port=8080)
|
uvicorn.run(app, host='0.0.0.0', port=8080)
|
||||||
|
|
|
@ -11,8 +11,7 @@ python = "^3.12"
|
||||||
aiohttp = "^3.9.1"
|
aiohttp = "^3.9.1"
|
||||||
uvicorn = "^0.24.0.post1"
|
uvicorn = "^0.24.0.post1"
|
||||||
starlette = "^0.33.0"
|
starlette = "^0.33.0"
|
||||||
boto3 = "^1.33.6"
|
aioboto3 = "^9.0.0"
|
||||||
botocore = "^1.33.6"
|
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
black = "^23.10.1"
|
black = "^23.10.1"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user