Django:在自定义 URL 后面提供媒体服务 [英] Django: Serving Media Behind Custom URL

查看:20
本文介绍了Django:在自定义 URL 后面提供媒体服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我当然知道通过 Django 提供静态文件会让你直接下地狱,但我对如何使用自定义 url 使用 Django 屏蔽文件的真实位置感到困惑.Django:在通用视图中提供下载服务 但答案是我接受了似乎是错误"的做事方式.

So I of course know that serving static files through Django will send you straight to hell but I am confused on how to use a custom url to mask the true location of the file using Django. Django: Serving a Download in a Generic View but the answer I accepted seems to be the "wrong" way of doing things.

urls.py:

url(r'^song/(?P<song_id>d+)/download/$', song_download, name='song_download'),

views.py:

def song_download(request, song_id):
    song = Song.objects.get(id=song_id)
    fsock = open(os.path.join(song.path, song.filename))

    response = HttpResponse(fsock, mimetype='audio/mpeg')
    response['Content-Disposition'] = "attachment; filename=%s - %s.mp3" % (song.artist, song.title)

    return response

这个解决方案完美,但事实证明还不够完美.如何在仍然通过 nginx/apache 提供服务的同时避免直接链接到 mp3?

This solution works perfectly but not perfectly enough it turns out. How can I avoid having a direct link to the mp3 while still serving through nginx/apache?

编辑 1 - 附加信息

目前我可以使用以下地址获取我的文件:http://www.example.com/music/song/1692/download/但是上面提到的方法是魔鬼的工作.

Currently I can get my files by using an address such as: http://www.example.com/music/song/1692/download/ But the above mentioned method is the devil's work.

我怎样才能在仍然让 nginx/apache 为媒体服务的同时完成我上面的内容?这是应该在网络服务器级别完成的事情吗?一些疯狂的mod_rewrite?

How can I accomplished what I get above while still making nginx/apache serve the media? Is this something that should be done at the webserver level? Some crazy mod_rewrite?

http://static.example.com/music/Aphex%20Twin%20-%20Richard%20D.%20James%20(V0)/10%20Logon-Rock%20Witch.mp3

编辑 2 - 附加附加信息

我使用 nginx 作为我的前端和反向代理后端 apache/开发服务器,所以我认为如果它确实需要某种 mod_rewrite 工作,我将不得不找到可以与 nginx 一起使用的东西.

I use nginx for my frontend and reverse proxy back apache/development server so I think if it does require some sort of mod_rewrite work I will have to find something that would work with nginx.

推荐答案

为了扩展之前的答案,您应该能够修改以下代码并让 nginx 直接为您的下载文件提供服务,同时仍然保护文件.

To expand on the previous answers you should be able to modify the following code and have nginx directly serve your download files whilst still having the files protected.

首先添加一个位置如:

location /files/ {
   alias /true/path/to/mp3/files/;
   internal;
}

到您的 nginx.conf 文件(内部使其无法直接访问).然后你需要一个像这样的 Django 视图:

to your nginx.conf file (the internal makes this not directly accessible). Then you need a Django View something like this:

def song_download(request, song_id):
    try:
        song = Song.objects.get(id=song_id)
        response = HttpResponse()
        response['Content-Type'] = 'application/mp3'
        response['X-Accel-Redirect'] = '/files/' + song.filename
        response['Content-Disposition'] = 'attachment;filename=' + song.filename
    except Exception:
        raise Http404
    return response

这会将文件下载交给 nginx.

which will hand off the file download to nginx.

这篇关于Django:在自定义 URL 后面提供媒体服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆