3

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?

AcK
  • 2,063
  • 2
  • 20
  • 27
Matej Jeglič
  • 403
  • 3
  • 11
  • 1
    It uses a SpooledTemporaryFile behind the scenes: https://docs.python.org/3/library/tempfile.html#tempfile.SpooledTemporaryFile - see https://stackoverflow.com/questions/18280245/where-does-python-tempfile-writes-its-files for how you can change where Python's tempfile module stores its temporary files. – MatsLindh Mar 06 '22 at 22:44
  • 1
    Specificically, looks like you can set the the env variables TMPDIR. TEMP or TMP with your prefered temp directory https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir – Gonzalo Odiard Mar 06 '22 at 22:47

1 Answers1

3

You could use gettempdir() method from Python's tempfile module to get the default temporary directory, as well as use .tempdir variable to set/change the default directory used for temporary files.

Example:

import tempfile

print("Temp directory before changing it:", tempfile.gettempdir())
tempfile.tempdir = "path/to/tempdir/here"
print("Temp directory after changing it:", tempfile.gettempdir())
Chris
  • 18,724
  • 6
  • 46
  • 80