6

I'm running a Django server hosted on DigitalOcean using Nginx and Gunicorn. I tried adding a 2MB picture via the admin interface when I get hit with a 403 error. Looking into error.log indicated permission was denied, as follows:

2017/06/27 01:03:29 [error] 1643#1643: *30 open() "/home/brian/nydkc11/nydkc11/nydkcd11/media/image_main/dtc1.jpg" failed (13: Permission denied), client: 108.29.217.25, server: nydkc11.org, request: "GET /media/image_main/dtc1.jpg HTTP/1.1", host: "nydkc11.org", referrer: "http://nydkc11.org/admin/blog/image/7/change/"

The weirdest thing, however, is that smaller image files work just fine (around 18 kb or so). Anyone know why larger media files may be triggering the problem, and how I should fix it?

I had to set client_max_body_size to 100M in my nginx.conf, if that's a useful thing to know.

Brian Lee
  • 305
  • 4
  • 14
  • There is a django default setting which causes files greater than 2.5 Mb to be streamed to a temp folder (in stead of processing them in memory). That might be the cause for your issue. How big exactly is your file, and what happens in your upload files of about 1-2 Mb in size? – dentemm Jun 27 '17 at 08:27
  • It's 3MB. I haven't uploaded files in the 1 ~ 2MB size, but files in the KB range work just fine. Any way to change that default setting by any chance? – Brian Lee Jun 27 '17 at 14:04
  • The setting is DATA_UPLOAD_MAX_MEMORY_SIZE, with a default value of 2621440 (which is 2.5Mb). So if you want to adjust it to 10Mb, just multiply by four. This worked for a project I had deployed to Heroku. – dentemm Jun 27 '17 at 14:17
  • That wasn't able to fix the problem. But the weirdest thing was that uploading the same file under `runserver` worked just fine. – Brian Lee Jun 27 '17 at 16:14

2 Answers2

7

I just came across the exact same problem. Thanks for your SO thread, otherwise I'd still be searching in the wrong place.

To fix this, you actually do not need to set DATA_UPLOAD_MAX_MEMORY_SIZE as long as you're only experiencing this error when uploading files. The Django documentation links to the other value, FILE_UPLOAD_MAX_MEMORY_SIZE, here.

Setting FILE_UPLOAD_MAX_MEMORY_SIZE to a higher limit in my settings.py resolves the problem, alongside with the aforementioned client_max_body_size in nginx.conf.

This sets the upload maximum to roughly 200 MB:

FILE_UPLOAD_MAX_MEMORY_SIZE = 200000000

Michael Gecht
  • 1,374
  • 1
  • 17
  • 26
4

larger files(>2.5M) are stored using temporary files, which will get 0o600 permissions by default. The problem can easily be fixed by setting a value to FILE_UPLOAD_PERMISSIONS

Set this in settings.py

FILE_UPLOAD_PERMISSIONS = 0o644

Reference

See For Python2

See Documentation