Issue
I am running FastAPI application on a embedded device. The embedded device has limited resources (disk space and RAM). However, an SD card with plenty of space is available. I would like to upload and store a large file on the SD card. The FastAPI documentation suggests using UploadFile
parameter.
I tried a simple application:
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.filename}
... and after posting a large file, I get a response with status code 400
and body
{"detail": "There was an error parsing the body"}
.
I was monitoring disk usage during the upload process and I saw the free space on partition /tmp
was decreasing until it ran out of space. I assume FastAPI figures out that the uploaded file is too big to be stored in memory and decides to store it on disk. Unfortunately, the selected disk is also too small.
How can I select the location which FastAPI internally uses to store the uploaded file?
Solution
You could use Python's tempfile module to get, as well as change, the default temporary
directory. Example:
import tempfile
print("Temp directory before change:", tempfile.gettempdir())
tempfile.tempdir = "path/to/tempdir/here"
print("Temp directory after change:", tempfile.gettempdir())
Answered By - Chris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.