diff --git a/app/main.py b/app/main.py index 3aec6bf..b4b767d 100644 --- a/app/main.py +++ b/app/main.py @@ -1,8 +1,9 @@ from fastapi import FastAPI, File, UploadFile -from fastapi.staticfiles import StaticFiles import dotenv import os +import random +import string import asyncio import uvicorn @@ -18,11 +19,20 @@ app: FastAPI = FastAPI(title="DropMeFiles analog") # redoc_url=None if disable_docs else "/redoc", # openapi_url=None if disable_docs else "/openapi.json") -# app.mount("/", StaticFiles(directory="frontend", html=True), name="frontend") +data = dict() @app.post("/upload_file") async def create_upload_file(file: UploadFile = File(...)): - return {"filename": file.filename, "content_type": file.content_type} + random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=6)) + data[random_string] = file.filename + return {"filename": file.filename, "content_type": file.content_type, "id": random_string} + +@app.get("/{item_id}") +async def read_item(item_id: str): + if item_id in data.keys(): + return {"return": data[item_id]} + else: + return {"return": "NO DATA!"} async def main(): config = uvicorn.Config("main:app", port=5000, host="0.0.0.0", log_level="debug") diff --git a/app/minio_test.py b/app/minio_test.py new file mode 100644 index 0000000..2292202 --- /dev/null +++ b/app/minio_test.py @@ -0,0 +1,44 @@ +import asyncio +from aiobotocore.session import get_session + + +AWS_ACCESS_KEY_ID = "some_username" +AWS_SECRET_ACCESS_KEY = "some_password" +ENDPOINT_URL = "" + + +async def main(): + session = get_session() + + # Создаем S3 клиент + async with session.create_client( + "s3", + region_name="us-east-1", + aws_secret_access_key=AWS_SECRET_ACCESS_KEY, + aws_access_key_id=AWS_ACCESS_KEY_ID, + endpoint_url=ENDPOINT_URL, # важно для MinIO + ) as s3: + + bucket = "mybucket" + key = "example.txt" + + # 1. Создаем bucket + print("Создаю bucket…") + await s3.create_bucket(Bucket=bucket) + + # 2. Загружаем файл (put_object) + print("Загружаю файл…") + await s3.put_object( + Bucket=bucket, + Key=key, + Body=b"Hello from aiobotocore + MinIO!" + ) + + # 3. Скачиваем файл + print("Скачиваю файл…") + obj = await s3.get_object(Bucket=bucket, Key=key) + data = await obj["Body"].read() + print("Содержимое:", data.decode()) + + +asyncio.run(main())