Test minio S3
This commit is contained in:
+13
-3
@@ -1,8 +1,9 @@
|
|||||||
from fastapi import FastAPI, File, UploadFile
|
from fastapi import FastAPI, File, UploadFile
|
||||||
from fastapi.staticfiles import StaticFiles
|
|
||||||
|
|
||||||
import dotenv
|
import dotenv
|
||||||
import os
|
import os
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import uvicorn
|
import uvicorn
|
||||||
@@ -18,11 +19,20 @@ app: FastAPI = FastAPI(title="DropMeFiles analog")
|
|||||||
# redoc_url=None if disable_docs else "/redoc",
|
# redoc_url=None if disable_docs else "/redoc",
|
||||||
# openapi_url=None if disable_docs else "/openapi.json")
|
# openapi_url=None if disable_docs else "/openapi.json")
|
||||||
|
|
||||||
# app.mount("/", StaticFiles(directory="frontend", html=True), name="frontend")
|
data = dict()
|
||||||
|
|
||||||
@app.post("/upload_file")
|
@app.post("/upload_file")
|
||||||
async def create_upload_file(file: UploadFile = 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():
|
async def main():
|
||||||
config = uvicorn.Config("main:app", port=5000, host="0.0.0.0", log_level="debug")
|
config = uvicorn.Config("main:app", port=5000, host="0.0.0.0", log_level="debug")
|
||||||
|
|||||||
@@ -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())
|
||||||
Reference in New Issue
Block a user