[英]How to select the disk location for UploadFile parameter in FastAPI?
我在嵌入式設備上運行 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
和 body {"detail": "There was an error parsing the body"}
的響應。
我在上傳過程中監控磁盤使用情況,我看到分區/tmp
上的可用空間正在減少,直到空間用完。 我假設 FastAPI 發現上傳的文件太大而無法存儲在 memory 中,並決定將其存儲在磁盤上。 不幸的是,選擇的磁盤也太小了。
我怎樣才能 select FastAPI 內部用於存儲上傳文件的位置?
您可以使用 Python 的tempfile模塊來獲取和更改默認temporary
目錄。 例子:
import tempfile
print("Temp directory before change:", tempfile.gettempdir())
tempfile.tempdir = "path/to/tempdir/here"
print("Temp directory after change:", tempfile.gettempdir())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.