Where does django store temporary files for download?

I have a Django / uwsgi / nginx stack running on CentOS. When uploading a large file to django (1GB +), I expect it to create a temporary file in /tmp , and I will have to watch it grow as the download progresses. However, I do not. ls -lah /tmp does not show any new files that are created or ls -lah /tmp . I even explicitly indicated in settings.py that FILE_UPLOAD_TEMP_DIR = '/tmp' , but still nothing.

I would appreciate help in tracking where the temp files are stored. I need this to determine if there are any large downloads in the process.

+4
source share
2 answers

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

Saving uploaded data

Before saving downloaded files, data must be saved somewhere.

By default, if the downloaded file is less than 2.5 megabytes, Django will contain the entire contents of the download in memory. This means that saving a file is only associated with reading from memory and writing to disk, and therefore, very fast.

However, if the downloaded file is too large, Django will write the downloaded file to a temporary file stored in your temporary system directory. On a Unix-like platform, this means that you can expect Django to generate a file called / tmp / tmpzfp 6I6.upload. If the load is large enough, you can see how this file grows in size, as Django streams data to disk.

These features are 2.5 megabytes; / TMP; etc. - just β€œsmart” by default. Read on to find out how you can customize or completely replace the download behavior.

In addition, this only happens after a given size, the default value is 2.5 MB

FILE_UPLOAD_MAX_MEMORY_SIZE The maximum size in bytes for files to be loaded into memory. Files larger than FILE_UPLOAD_MAX_MEMORY_SIZE will be transferred to disk.

The default is 2.5 megabytes.

+4
source

I just tracked this on my OS X system using 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 just returns the standard tempfile.NamedTemporaryFile Python if it doesn't work on Windows.

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

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

On my system right now it produces / var / folders / 9v / npjlh_kn7s9fv5p4dwh1spdr0000gn / T, where I found my temporarily downloaded file.

+4
source

Source: https://habr.com/ru/post/1414415/


All Articles