引言:
最近工作中有机会接触FastAPI这个框架,所以就把官方文档看了一遍,对框架的各个特性及使用方法做了总结,会逐步的发出来,希望对您有用。
如果您之前接触过python的其他框架,看起来会非常简单和顺畅,其实就是很简单。
【上一篇】:【FastAPI基础】17、文件操作
【下一篇】:【FastAPI基础】18、表单和文件操作
【FastAPI搭建好的产品框架源码,直接上手】:【FastAPI搭建好的产品架构】,直接上手
废话不多说,直接上源码,复制直接用呗,其他的就不粘贴了,没啥特别的。
如有需要help,下面私信我
# encoding: utf-8 """ @author: 陈建华 @file: views_minio.py @time: 2021/4/8 10:26 """ import platform import shutil import time import datetime from typing import List from tempfile import NamedTemporaryFile from fastapi import APIRouter, File, Form, UploadFile, status, Query from minio import Minio # from starlette.requests import Request from server.app_file.schemas.file_schems import SystemFileIn from server.app_file.models.file_model import system_file_db from server import config from server.command.extensions.responses import ResponsesExampleVale from server.command.schemas import response_data router = APIRouter() @router.post( "/upload/", tags=["图片上传"], # dependencies=[Depends(verify_token)], responses=ResponsesExampleVale( { "code": 200, "message": "上传成功!", "content": { "id": "图片id" } } ) ) async def picture_upload( # request: Request, image_type: str = Form(..., description = "图片类型"), file: UploadFile = File(..., description = "文件流"), ): if image_type not in config.PICTURE_LIST: return response_data(code=status.HTTP_404_NOT_FOUND, message=f"不存在的图片类型", data={}) sys_type = platform.system() if sys_type == 'Windows': temporary_delete = False else: temporary_delete = True with NamedTemporaryFile(delete=temporary_delete) as tmp: shutil.copyfileobj(file.file, tmp) file_client = Minio(config.MinIO_URL, access_key=config.MinIO_ACCESS_KEY, secret_key=config.MinIO_SECRET_KEY, secure=config.MinIO_SECURE) upload_name = str(int(time.mktime(time.localtime()))) + '.' + file.filename.split('.')[1] file_name = file.filename # bucket_name我这儿设置成了图片类型,看下面的图 file_client.fput_object(bucket_name=image_type, object_name=upload_name, file_path=tmp.name, content_type=file.content_type) # 存入自己本地 insert_dict = SystemFileIn(**{'name': file_name, 'path': f'http://{config.MinIO_URL}/{image_type}/{upload_name}', 'type': image_type, 'size': file_client.stat_object(image_type, upload_name).size / 1024, 'notes': upload_name, 'status': '1', 'create_time': datetime.datetime.now()}) return response_data(data=await system_file_db.create(insert_dict))
这是minio服务的样子: