最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用fastapi實現(xiàn)上傳文件

 更新時間:2022年06月23日 11:02:54   作者:小飛牛  
FastAPI是一個現(xiàn)代的,快速(高性能)python?web框架。本文將利用fastapi實現(xiàn)上傳文件功能,文中的示例代碼講解詳細,需要的可以參考一下

使用File實現(xiàn)文件上傳

使用Form表單上傳文件,fastapi使用File獲取上傳的文件。

指定了參數(shù)類型是bytes:file: bytes = File(),此時會將文件內(nèi)容全部讀取到內(nèi)存,比較適合小文件。

使用File需要提前安裝 python-multipart

from fastapi import FastAPI, File
 ?
app = FastAPI()
 ?
@app.post("/files/")
async def create_file(file: bytes = File()):
   return {"file_size": len(file)}

只要在路徑操作函數(shù)中聲明了變量的類型是bytes且使用了File,則fastapi會將上傳文件的內(nèi)容全部去讀到參數(shù)中。

使用UploadFile實現(xiàn)文件上傳

對于大文件,不適合將文件內(nèi)容全部讀取到內(nèi)存中,此時使用UploadFile

from fastapi import FastAPI, UploadFile
 ?
app = FastAPI()
 ?
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
     return {"filename": file.filename}

bytes相比,使用UploadFile有如下好處:

  • 不需要在使用File()作為路徑操作函數(shù)中參數(shù)的默認值
  • 不會把文件內(nèi)容全部加載到內(nèi)存中,而是批量讀取一定量的數(shù)據(jù),邊讀邊存硬盤。
  • 可以獲取文件的元數(shù)據(jù)。
  • 該類型的變量可以像文件變量一樣操作。

UploadFile的屬性

  • filename:類型是str,用來獲取文件的名字,比如:myimage.png
  • content_type: 類型是str, 用來獲取文件的類型,比如:image/png
  • file: 類文件對象,是一個標準的python文件對象

除了這四個基礎(chǔ)屬性外,UploadFile還有三個async方法:

  • write, 將str或者bytes寫到文件中
  • read: 讀文件
  • seek: 移動光標
  • close: 關(guān)閉文件
 # 獲取文件內(nèi)容
 contents = await myfile.read()

設(shè)置上傳文件是可選的

設(shè)置默認值是None即可

 from typing import Union
 ?
 from fastapi import FastAPI, File, UploadFile
 ?
 app = FastAPI()
 ?
 ?
 @app.post("/files/")
 async def create_file(file: Union[bytes, None] = File(default=None)):
     if not file:
         return {"message": "No file sent"}
     else:
         return {"file_size": len(file)}
 ?
 ?
 @app.post("/uploadfile/")
 async def create_upload_file(file: Union[UploadFile, None] = None):
     if not file:
         return {"message": "No upload file sent"}
     else:
         return {"filename": file.filename}

上傳多個文件

參數(shù)的參數(shù)的類型是列表:列表元素是bytes或者UploadFile

 from typing import List
 ?
 from fastapi import FastAPI, File, UploadFile
 ?
 app = FastAPI()
 ?
 ?
 @app.post("/files/")
 async def create_files(files: List[bytes] = File()):
     return {"file_sizes": [len(file) for file in files]}
 ?
 ?
 @app.post("/uploadfiles/")
 async def create_upload_files(files: List[UploadFile]):
     return {"filenames": [file.filename for file in files]}

知識點補充

1.FastAPI簡介

FastAPI是什么

FastAPI是一個現(xiàn)代的,快速(高性能)python web框架?;跇藴实膒ython類型提示,使用python3.6+構(gòu)建API的Web框架。

FastAPI的主要特點如下:

  • 快速:非常高的性能,與NodeJS和Go相當(這個要感謝Starlette和Pydantic),是最快的Python框架之一。
  • 快速編碼:將開發(fā)速度提高約200%到300%。
  • 更少的bug:減少大約40%的開發(fā)人員人為引起的錯誤。
  • 直觀:強大的編輯器支持,調(diào)試時間更短。
  • 簡單:易于使用和學習。減少閱讀文檔的時間。
  • 代碼簡潔:盡量減少代碼重復。每個參數(shù)可以聲明多個功能,減少程序的bug。
  • 健壯:生產(chǎn)代碼會自動生成交互式文檔。
  • 基于標準:基于并完全兼容API的開放標準:OpenAPI和JSON模式。

FastAPI 站在巨人的肩膀上:

  • Starlette 用于構(gòu)建 Web 部件。
  • Pydantic 用于數(shù)據(jù)部分。

環(huán)境準備

安裝fastapi

pip install fastapi

對于生產(chǎn)環(huán)境,還需要一個ASGI服務(wù)器,如Uvicorn或Hypercorn

pip install "uvicorn[standard]"

入門示例程序

新建一個main.py,編寫如下程序:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}

運行程序:

uvicorn main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [28720]
INFO: Started server process [28722]
INFO: Waiting for application startup.
INFO: Application startup complete.

到此這篇關(guān)于Python利用fastapi實現(xiàn)上傳文件的文章就介紹到這了,更多相關(guān)Python fastapi上傳文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

石门县| 随州市| 临漳县| 四子王旗| 隆回县| 山丹县| 阿荣旗| 神池县| 买车| 镇坪县| 东兰县| 大港区| 潜江市| 天水市| 巴塘县| 普兰店市| 囊谦县| 集贤县| 高州市| 偏关县| 藁城市| 通辽市| 青州市| 岳池县| 林芝县| 河北省| 区。| 通州区| 抚顺县| 鄂托克前旗| 子长县| 剑川县| 都安| 秦安县| 平舆县| 卢湾区| 海兴县| 吉木萨尔县| 威信县| 蕲春县| 伊吾县|