Ver Fonte

fastapi+minio:v2
这是更新了配置文件版本

clxHardstudy há 1 ano atrás
pai
commit
a81199292e

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 0 - 0
main.py → app/main.py


+ 16 - 31
routers/files.py

@@ -4,6 +4,7 @@
     @file:files.py.py
     @time:2023/5/30 6:08
 """
+import json
 import os
 import random
 import time
@@ -14,24 +15,25 @@ from fastapi.responses import FileResponse, StreamingResponse
 from typing import Annotated, List
 from minio import Minio
 from io import BytesIO
+from config import config
 
-minio_client = Minio(
+"""
+    minio_client = Minio(
     "127.0.0.1:9000",
     access_key="minioadmin",
     secret_key="minioadmin",
     secure=False
 )
+"""
 
-router = APIRouter()
-
+# 创建minio对象
+minio_class = config.MinioOperate()
+# 连接minio
+minio_client = minio_class.link_minio()
+# 创建bucket
+minio_class.create_bucket()
 
-# 测试
-# @router.post("/file/local")
-# async def create_file(file: UploadFile = File(...)):
-#     filename = file.filename
-#     stream = await file.read()
-#     with open(r"D:\pythonProject\django\fastapi_01\static\img\{}".format(filename), "ab+") as fp:
-#         fp.write(stream)
+router = APIRouter()
 
 
 # minio ?上传文件时在重复时,需要修改文件名
@@ -40,10 +42,12 @@ async def create_file(file: UploadFile = File(...)):
     timestamp = str(time.time())
     uid = re.sub(r"\.", "", timestamp)
     front, ext = os.path.splitext(file.filename)
+
     file_name = uid + ext  # 168549427474778.png
     data = await file.read()
     file_stream = BytesIO(initial_bytes=data)
     size = len(data)
+
     uri = os.path.join("/file/", file_name)
     if (minio_client.put_object(
             "file1",
@@ -56,21 +60,6 @@ async def create_file(file: UploadFile = File(...)):
         return {"msg": "上传失败", "status": 400}
 
 
-# @router.get("/file/minio/{file_name}")
-# async def download_file(file_name:str):
-#     if not minio_client.bucket_exists("file1"):
-#         return {"msg":"The bucket not exist","status":404}
-#     data =  minio_client.get_object("file1",file_name)
-#     content = data.read()
-#     with open(r"D:\pythonProject\django\fastapi_01\static\file\{}".format(file_name),"wb") as fp:
-#         # for d in data.stream(32 * 1024):
-#         #     file_data.write(d)
-#         fp.write(content)
-#     return {"msg":"下载图片成功!","status":200}
-
-# 响应式下载
-
-
 # 图片预览
 # response = StreamingResponse(file_content,media_type='image/jpg)
 
@@ -88,17 +77,13 @@ async def download_file(uid: str):
         return {"error": str(e)}
     return response
 
+
 # 删除
 @router.delete("/file/{uid}")
 async def delete_file(uid: str):
     try:
         minio_client.get_object("file1", uid)
         minio_client.remove_object("file1", uid)
-        return {"msg": "删除图片成功!", "status": 204}
+        return {"msg": "删除图片成功!", "status": 200}
     except:
         return {"msg": "Not Found", "status": 404}
-
-# test
-# @router.get("/file/response")
-# async def file():
-#     return FileResponse(r'D:\pythonProject\django\fastapi_01\static\file\wallhaven-6d5k6x.jpg', filename='demo.jpg')

BIN
static/file/wallhaven-6d5k6x.jpg


BIN
static/file/wallhaven-kx9e61.png


BIN
static/img/wallhaven-5gw639.jpg


BIN
static/img/wallhaven-6d5k6x.jpg