Skip to main content
 首页 » 编程设计

python - Django:在自定义 URL 后面提供媒体服务

2023年05月26日5kevingrace

所以我当然知道通过 Django 提供静态文件会让你直接下 hell ,但我对如何使用自定义 url 来掩盖使用 Django 的文件的真实位置感到困惑。 Django: Serving a Download in a Generic View但我接受的答案似乎是“错误”的做事方式。

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 

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

编辑 1 - 附加信息

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

如何在让 nginx/apache 为媒体提供服务的同时完成上面的任务?这是应该在网络服务器级别完成的事情吗?一些疯狂的 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 一起使用的东西。

请您参考如下方法:

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

首先添加一个位置,例如:

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

到你的 nginx.conf 文件(内部使这不能直接访问)。然后你需要一个类似这样的 Django View :

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。