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