starlette

This commit is contained in:
Tony Rewin 2023-10-05 11:51:27 +03:00
parent f660d3b97b
commit 8bfae82fa0
3 changed files with 34 additions and 16 deletions

View File

@ -1,4 +1,12 @@
#### ENV setup ## `uploader`: загружает файлы в IPFS
Использует:
- python3
- uvicorn ASGI -> WSGI
- starlette ASGI server
- boto3 s3 client
#### Переменные среды
- STORJ_ACCESS_KEY - STORJ_ACCESS_KEY
- STORJ_SECRET_KEY - STORJ_SECRET_KEY

33
main.py
View File

@ -3,7 +3,10 @@ import tempfile
import uuid import uuid
import boto3 import boto3
from botocore.exceptions import BotoCoreError, ClientError from botocore.exceptions import BotoCoreError, ClientError
from aiohttp import web from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
from starlette.requests import Request
STORJ_ACCESS_KEY = os.environ.get('STORJ_ACCESS_KEY') STORJ_ACCESS_KEY = os.environ.get('STORJ_ACCESS_KEY')
STORJ_SECRET_KEY = os.environ.get('STORJ_SECRET_KEY') STORJ_SECRET_KEY = os.environ.get('STORJ_SECRET_KEY')
@ -11,22 +14,25 @@ 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')
async def upload_handler(request: Request):
form = await request.form()
file = form.get('file')
async def upload_handler(request):
reader = await request.multipart()
file = await reader.next()
if file is None: if file is None:
return web.json_response({'error': 'No file uploaded'}, status=400) return JSONResponse({'error': 'No file uploaded'}, status_code=400)
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', s3 = boto3.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)
try: try:
with tempfile.NamedTemporaryFile() as tmp_file: with tempfile.NamedTemporaryFile() as tmp_file:
while True: while True:
chunk = await file.read_chunk() # 8192 bytes by default. chunk = await file.read(8192) # 8192 bytes by default.
if not chunk: if not chunk:
break break
tmp_file.write(chunk) tmp_file.write(chunk)
@ -42,15 +48,18 @@ async def upload_handler(request):
url = 'http://' + CDN_DOMAIN + '/' + key url = 'http://' + CDN_DOMAIN + '/' + key
return web.json_response({'url': url, 'originalFilename': file.filename}) return JSONResponse({'url': url, 'originalFilename': file.filename})
except (BotoCoreError, ClientError) as e: except (BotoCoreError, ClientError) as e:
print(e) print(e)
return web.json_response({'error': 'Failed to upload file'}, status=500) return JSONResponse({'error': 'Failed to upload file'}, status_code=500)
routes = [
Route('/upload', upload_handler, methods=['POST']),
]
app = Starlette(debug=True, routes=routes)
if __name__ == "__main__": if __name__ == "__main__":
app = web.Application() import uvicorn
app.router.add_post('/upload', upload_handler) uvicorn.run(app, host='0.0.0.0', port=8000)
web.run_app(app)

View File

@ -1,3 +1,4 @@
aiohttp~=3.8.5 uvicorn
boto3~=1.28.56 starlette
botocore~=1.31.56 boto3
botocore