4

I am working with Django 2.2 and got stuck with file upload size validation. I have read django documentation:

DATA_UPLOAD_MAX_MEMORY_SIZE
FILE_UPLOAD_MAX_MEMORY_SIZE

I only set DATA_UPLOAD_MAX_MEMORY (to 20 MB), as mentioned in documentation:

The check is done when accessing request.body or request.POST and is calculated against the total request size excluding any file upload data.

But in my project it also checks my uploading file size in request.FILES.

Can someone explain differences between FILE_UPLOAD_MAX_MEMORY and DATA_UPLOAD_MAX_MEMORY? And how to use them properly?

Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
Aidas K
  • 131
  • 1
  • 6
  • Can you explain “it also checks my uploading file size”? – dirkgroten Feb 27 '20 at 10:42
  • @dirkgroten as I understand DATA_UPLOAD_MAX_MEMORY should exclude uploaded files size, which are in request.FILES, but when it is set and FILE_UPLOAD_MAX_MEMORY is default, I can upload files up to size which is set in DATA_UPLOAD_MAX_MEMORY. – Aidas K Feb 27 '20 at 11:45
  • On your development environment? And how do you upload the files exactly? – dirkgroten Feb 27 '20 at 12:51

2 Answers2

3

DATA_UPLOAD_MAX_MEMORY

The check is done when accessing request.body or request.POST and is calculated against the total request size excluding any file upload data.

FILE_UPLOAD_MAX_MEMORY_SIZE

The maximum size (in bytes) that an upload will be before it gets streamed to the file system. If uploading files are larger than FILE_UPLOAD_MAX_MEMORY_SIZE, the data of files will be streamed to FILE_UPLOAD_TEMP_DIR https://docs.djangoproject.com/en/2.2/ref/settings/#file-upload-temp-dir

I don't think you have to set FILE_UPLOAD_MAX_MEMORY_SIZE, because it's the size of memory cache.

yanqzhi
  • 382
  • 3
  • 8
  • Thank you for an answer, maybe you could clarify with an example how total request size is calculated when DATA_UPLOAD_MAX_MEMORY is set to 20mb and FILE_UPLOAD_MAX_MEMORY_SIZE is default and when FILE_UPLOAD_MAX_MEMORY_SIZE is set as an example to 20mb? How do FILE_UPLOAD_MAX_MEMORY_SIZE and DATA_UPLOAD_MAX_MEMORY_SIZE correlate? – Aidas K Feb 27 '20 at 12:11
  • DATA_UPLOAD_MAX_MEMORY is set as 20M, FILE_UPLOAD_MAX_MEMORY_SIZE is set as 15M. If you upload a file(25M), an error will occur as in your question. So, DATA_UPLOAD_MAX_MEMORY is changed to 30M, there will be no error.
    if the uploaded data size of the file is little than 15M, the data is in memory. If the uploaded size is more than 15M, the data will be streamed to disk.
    – yanqzhi Feb 28 '20 at 06:15
2

FILE_UPLOAD_MAX_MEMORY will load the file contents into the RAM, if the request body or the file which is uploaded is more than FILE_UPLOAD_MAX_MEMORY then the file will be stored in the /tmp directory.

You can restrict the file size in the webserver (Nginx) by adding client_max_body_size 20M;

Here 20 megabytes is the maximum data that request body can have. So if you are uploading a file more than 20 MB, the webserver will not accept it.

  • Thank you for an answer, maybe you could clarify with examples how FILE_UPLOAD_MAX_MEMORY and DATA_UPLOAD_MAX_MEMORY correlate? When one is set to something or both is set to something. – Aidas K Feb 27 '20 at 12:15
  • They are unrelated. – dirkgroten Feb 27 '20 at 12:52