使用django服务受保护的媒体文件

时间:2016-09-28 10:16:04

标签: python django python-3.x nginx nginx-location

我希望Django仅为登录用户提供一些媒体文件(例如用户上传的文件)。由于我的网站流量很低,我想我会保持简单,不要使用django-sendfile告诉Nginx何时提供文件。相反,我会让Django / Gunicorn做这个工作。对我而言,这似乎更简单,对于低流量站点,这可能更安全。

但是,组织文件存储位置的最佳方法是什么?媒体文件全部存储在MEDIA_ROOT以下,此目录由Nginx在生产中提供。如果我将文件上传到MEDIA_ROOT/protected/,我必须告诉Nginx不要提供子目录protected中的文件。

但这是个好主意吗?对我来说,首先允许Nginx访问/media/然后保护子目录/media/protected/似乎有点冒险。最好不要使用MEDIA_ROOT的子目录来存储受保护的文件吗?

但是如果我在我的模型中尝试这样快速和肮脏的东西:

upload_to='../protected/documents/%Y/%m/'

Django抱怨道:

SuspiciousFileOperation at /admin/core/document/add/
The joined path (/home/me/projects/project/protected/documents/2016/09/test.file) is located outside of the base path component (/home/me/projects/project/media)

所以我认为"离开"这是不好的做法。 MEDIA_ROOT

存储和提供受保护媒体文件的最佳解决方案是什么?

2 个答案:

答案 0 :(得分:2)

我现在想出了以下解决方案:

我在Django设置中有这个:

MEDIA_ROOT = "/projects/project/media/"
MEDIA_URL = "/media/

在我的模特中,我做了:

document = models.FileField(upload_to="public/documents")

document = models.FileField(upload_to="protected/documents")

这样,我现在有两个子目录' public'并且'受保护'在我的媒体文件目录中。

Nginx或Djangos开发服务器仅提供公共'公共'子目录。

对于Djangos开发服务器:

if os.environ["ENVIRONMENT_TYPE"] == 'development':
    urlpatterns += static(settings.MEDIA_URL + "public/", document_root=settings.MEDIA_ROOT + "public/")

对于Nginx(用于制作):

location /media/public/ {
    alias   /projects/project/media/public/;
}

当我想提供受保护的文档时,我会执行以下操作:

在urls.py中:

url(r'^media/protected/documents/(?P<file>.*)$', core.views.serve_protected_document, name='serve_protected_document'),

在views.py中:

@login_required()
def serve_protected_document(request, file):
    document = get_object_or_404(ProtectedDocument, file="protected/documents/" + file)

    # Split the elements of the path
    path, file_name = os.path.split(file)

    response = FileResponse(document.file,)
    response["Content-Disposition"] = "attachment; filename=" + file_name

    return response

感谢任何评论!有没有更好的方法来实现这个?

答案 1 :(得分:1)

直接从视图中提供媒体文件(可能是大文件)并不好。您可以使用nginx服务器中提供的sendfile扩展名;示例nginx配置如下所示。

 location /projects/project/media/{
    # this path is not public
    internal;
    # absolute path
    alias /projects/project/media/;
 }

将您的观点更改为

@login_required
def serve_protected_document(request, file):
    document = get_object_or_404(ProtectedDocument, file="protected/documents/" + file)

    # Split the elements of the path
    path, file_name = os.path.split(file)

    response = HttpResponse()
    response["Content-Disposition"] = "attachment; filename=" + file_name
    # nginx uses this path to serve the file
    response["X-Accel-Redirect"] = document.name # path to file
    return response

链接:有关在nginx上配置sendfile扩展的更多详细信息是here

相关问题