Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does django store temporary upload files?

I have a Django/uwsgi/nginx stack running on CentOS. When uploading a large file to django (1GB+), I expect it to create a temp file in /tmp and I should be able to watch it grow as the upload progresses. However, I don't. ls -lah /tmp doesn't show any new files being created or changing in size. I even specified in my settings.py explicitly that FILE_UPLOAD_TEMP_DIR = '/tmp' but still nothing.

I'd appreciate any help in tracking down where the temp files are stored. I need this to determine whether there are any large uploads in progress.

like image 393
Tony Abou-Assaleh Avatar asked May 25 '12 16:05

Tony Abou-Assaleh


2 Answers

They are stored in your system's temp directory. From https://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs:

Where uploaded data is stored

Before you save uploaded files, the data needs to be stored somewhere.

By default, if an uploaded file is smaller than 2.5 megabytes, Django will hold the entire contents of the upload in memory. This means that saving the file involves only a read from memory and a write to disk and thus is very fast.

However, if an uploaded file is too large, Django will write the uploaded file to a temporary file stored in your system's temporary directory. On a Unix-like platform this means you can expect Django to generate a file called something like /tmp/tmpzfp6I6.upload. If an upload is large enough, you can watch this file grow in size as Django streams the data onto disk.

These specifics -- 2.5 megabytes; /tmp; etc. -- are simply "reasonable defaults". Read on for details on how you can customize or completely replace upload behavior.

Additionally, this only happens after a given size, defaulted to 2.5MB

FILE_UPLOAD_MAX_MEMORY_SIZE The maximum size, in bytes, for files that will be uploaded into memory. Files larger than FILE_UPLOAD_MAX_MEMORY_SIZE will be streamed to disk.

Defaults to 2.5 megabytes.

like image 199
stevedbrown Avatar answered Nov 19 '22 01:11

stevedbrown


I just tracked this down on my OS X system with Django 1.4.1.

In django/core/files/uploadedfile.py, a temporary file is created using django.core.files.temp, imported as tempfile

from django.core.files import temp as tempfile 

This simply returns Python's standard tempfile.NamedTemporaryFile unless it's running on Windows.

To see the location of the tempdir, you can run this command at the shell:

python -c "import tempfile; print tempfile.gettempdir()"

On my system right now it outputs /var/folders/9v/npjlh_kn7s9fv5p4dwh1spdr0000gn/T, which is where I found my temporary uploaded file.

like image 34
Joseph Sheedy Avatar answered Nov 19 '22 01:11

Joseph Sheedy