我在嵌入式設備上運行 FastAPI 應用程式。嵌入式設備的資源有限(磁盤空間和 RAM)。但是,可以使用具有足夠空間的 SD 卡。我想在 SD 卡上上傳和存盤一個大檔案。FastAPI檔案建議使用UploadFile引數。
我嘗試了一個簡單的應用程式:
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.filename}
...在發布一個大檔案后,我收到了狀態代碼400和正文
的回應{"detail": "There was an error parsing the body"}。
我在上傳程序中監控磁盤使用情況,我看到磁區上的可用空間/tmp正在減少,直到空間用完。我假設 FastAPI 發現上傳的檔案太大而無法存盤在記憶體中,并決定將其存盤在磁盤上。不幸的是,選擇的磁盤也太小了。
如何選擇 FastAPI 內部用于存盤上傳檔案的位置?
uj5u.com熱心網友回復:
您可以將以下內容添加到您的應用程式以更改temp檔案的默認位置(使用 Python 的tempfile模塊):
import tempfile
tempfile.tempdir = "path/to/tempdir/here"
print("Temp directory after change:", tempfile.gettempdir())
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/438691.html
