且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django:让用户下载大文件

更新时间:2023-11-28 09:29:04

实际上不建议使用Django下载大文件.通常,您会拥有一个前端复用器(例如NginX),并仅使用Django来验证文件.

Using Django to download large files isn't really recommended. Usually you'd have a front-end multiplexer such as NginX, and use Django only to validate the file.

然后,如果下载经过验证,您将向多路复用器发出信号.对于NginX,您可以设置一个特殊的标头("X-Accel-Redirect")以指向本地文件的真实位置. Django将只提供几个字节,所有繁重的工作将由NginX承担;同时原始URL将是Django的URL,因此无法绕过安全性.

Then, if the download is validated, you'd issue a signal to the multiplexer. For NginX, you can set up a special header ("X-Accel-Redirect") to point to the true location of the local file. Django will only serve a few bytes, and all the heavy lifting will be taken up by NginX; at the same time the original URL will be that of Django, so that it is not possible to bypass security.

请参阅: http://wiki.nginx.org/X-accel

您可以在此处找到有关如何提供静态文件(可通过身份验证扩展)的注释

You can find notes on how to serve static files (extensible with authentication) here

https://docs.djangoproject.com/en/dev/howto/static-files/

但是正如页面上所说,它是"快速而肮脏的帮助程序视图",不适用于生产或高流量站点.即使Django 可以做到这一点,这也不是Django的初衷.

but as the page says, it is "a quick and dirty helper view" not intended for production or high-traffic sites. That's not what Django was designed to do, even if it can do it.