Django的是S3代理的是、Django

2023-09-11 23:44:11 作者:静若繁花

我扩展了一个自定义字段下载文件,这是一个链接到一个网址在我的Django项目,像的ModelAdmin:

I extended a ModelAdmin with a custom field "Download file", which is a link to a URL in my Django project, like:

http://www.myproject.com/downloads/1

在这里,我想要提供的存储在S3中桶的文件。桶中的文件是不公开的可读性,用户可能不能直接访问它。现在我想

There, I want to serve a file which is stored in a S3-bucket. The files in the bucket are not public readable, and the user may not have direct access to it. Now I want to

避免该文件在服务器的内存(这是多GB的文件)来加载 避免在服务器上的临时文件

理想的解决办法是让Django的行为作为一个代理,直接流S3-块给用户。我用博托,但是没有找到一种可能性流的块。 任何想法?

The ideal solution would be to let django act as a proxy that streams S3-chunks directly to the user. I use boto, but did not find a possibility to stream the chunks. Any ideas?

感谢。

推荐答案

而不是代理,为什么不能简单地重定向?

Rather than proxying, why not simply redirect?

使用Django的观点在www.myproject.com/downloads/1服务于HTTP重定向到S3存储网址 - 有可能产生有时间限制的验证URL如看到这里 http://docs.amazonwebservices.com/AmazonS3/2006-03- 01的/ dev /

Use the django view at www.myproject.com/downloads/1 to serve a HTTP Redirect to the S3 storage URL - it's possible to generate time-limited authenticated URLs e.g. see here http://docs.amazonwebservices.com/AmazonS3/2006-03-01/dev/

然后,客户机从S3直接下载该文件,但内容仍然是安全的,并获得了才能通过你的Django应用程序

Then the client downloads the file directly from S3, but the contents is still secure and access has to come through your Django application