uploader/main.py

74 lines
2.4 KiB
Python
Raw Normal View History

2024-05-06 08:03:24 +00:00
import logging
2023-09-27 23:05:14 +00:00
import os
import tempfile
import uuid
2024-05-06 08:03:24 +00:00
import aioboto3
2023-10-05 08:51:27 +00:00
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
from starlette.requests import Request
2023-12-02 05:44:06 +00:00
from auth import check_auth
2024-05-06 08:03:24 +00:00
# Logging configuration
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
2023-09-27 23:05:14 +00:00
STORJ_ACCESS_KEY = os.environ.get('STORJ_ACCESS_KEY')
STORJ_SECRET_KEY = os.environ.get('STORJ_SECRET_KEY')
STORJ_END_POINT = os.environ.get('STORJ_END_POINT')
STORJ_BUCKET_NAME = os.environ.get('STORJ_BUCKET_NAME')
CDN_DOMAIN = os.environ.get('CDN_DOMAIN')
2024-05-06 08:09:56 +00:00
#@check_auth
2023-10-05 08:51:27 +00:00
async def upload_handler(request: Request):
2024-05-06 08:03:24 +00:00
logging.debug("Received upload request")
2023-10-05 08:51:27 +00:00
form = await request.form()
file = form.get('file')
2023-09-27 23:05:14 +00:00
if file is None:
2024-05-06 08:03:24 +00:00
logging.error("No file uploaded")
2023-10-05 08:51:27 +00:00
return JSONResponse({'error': 'No file uploaded'}, status_code=400)
2023-09-27 23:05:14 +00:00
file_name, file_extension = os.path.splitext(file.filename)
key = str(uuid.uuid4()) + file_extension
2024-05-06 08:03:24 +00:00
logging.debug(f"Generated file key: {key}")
2023-10-05 08:51:27 +00:00
2024-05-06 07:50:19 +00:00
async with aioboto3.client('s3',
aws_access_key_id=STORJ_ACCESS_KEY,
aws_secret_access_key=STORJ_SECRET_KEY,
endpoint_url=STORJ_END_POINT) as s3:
2023-09-27 23:05:14 +00:00
with tempfile.NamedTemporaryFile() as tmp_file:
while True:
2024-05-06 07:50:19 +00:00
chunk = await file.read(8192)
2023-09-27 23:05:14 +00:00
if not chunk:
break
tmp_file.write(chunk)
2024-05-06 07:50:19 +00:00
tmp_file.flush()
2023-09-27 23:05:14 +00:00
2024-05-06 08:03:24 +00:00
logging.debug("Starting file upload to S3")
2024-05-06 07:50:19 +00:00
await s3.upload_file(
2023-09-27 23:05:14 +00:00
Filename=tmp_file.name,
Bucket=STORJ_BUCKET_NAME,
Key=key,
2024-05-06 07:50:19 +00:00
ExtraArgs={"ContentType": file.content_type}
2023-09-27 23:05:14 +00:00
)
2024-05-06 08:03:24 +00:00
logging.debug("File upload completed")
2023-09-27 23:05:14 +00:00
2024-05-06 07:50:19 +00:00
url = f'http://{CDN_DOMAIN}/{key}'
2024-05-06 08:03:24 +00:00
logging.info(f"File uploaded successfully: {url}")
2023-10-05 08:51:27 +00:00
return JSONResponse({'url': url, 'originalFilename': file.filename})
2023-09-27 23:05:14 +00:00
async def home(request: Request):
2024-05-06 08:03:24 +00:00
logging.debug("Home route called")
return JSONResponse({'message': 'Hello World!'})
2023-10-05 08:51:27 +00:00
routes = [
2024-05-06 07:14:43 +00:00
Route('/test', home, methods=['GET']),
Route('/', upload_handler, methods=['POST'])
2023-10-05 08:51:27 +00:00
]
2023-09-27 23:05:14 +00:00
2023-10-05 08:51:27 +00:00
app = Starlette(debug=True, routes=routes)
2023-09-27 23:05:14 +00:00
2023-10-05 08:51:27 +00:00
if __name__ == "__main__":
import uvicorn
2024-05-06 08:03:24 +00:00
uvicorn.run(app, host='0.0.0.0', port=8080)