簡體   English   中英

如何 select FastAPI 中 UploadFile 參數的磁盤位置?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM