django / file uploads permissions

2019-03-17 08:15发布

I wrote a django app, but i have a little problem with the file permissions of the uploads files from a web form.

Basically I can upload a .mp3 file but it always keep chmod 600.

The container folder has chmod 775, and the umask is set to 022.

I'm in a shared hosting service.

2条回答
疯言疯语
2楼-- · 2019-03-17 08:21

Hope this is useful. The below method can be used. This has 2 other advantages other than resolving permission errors.

  • No issues with file permissions
  • More faster
  • The file is not copied to /tmp/ folder for files which are more than 2.5 MB (saving space as well).

with open(file_name, 'wb+') as temp_file:
    for chunk in up_file.chunks():
        temp_file.write(chunk)
查看更多
手持菜刀,她持情操
3楼-- · 2019-03-17 08:27

Try this in your settings.py if you use Python 2:

FILE_UPLOAD_PERMISSIONS = 0644

In Python 3 octal numbers must start with 0o so the line would be:

FILE_UPLOAD_PERMISSIONS = 0o644

For more details see the documentation.

查看更多
登录 后发表回答