如何将文件从 S3 流式传输到浏览器?
在我的应用程序中,我使用 Zend_Service_Amazon_S3 来存储一些大媒体文件。当用户下载文件时,应用程序首先将文件复制到本地服务器,然后将文件与强制下载文件的一些标头一起发送到浏览器。
有没有办法让亚马逊将文件直接流式传输到浏览器,而不必将整个文件复制到本地服务器?理想情况下,应用程序仍然可以选择强制下载文件的名称(在许多情况下与亚马逊上的文件不同)。
In my application I'm using Zend_Service_Amazon_S3 to store some big media files. When a user downloads a file, the application first copies the file to the local server and from there the file, together with some headers to force download the file, is sent to the browser.
Is there a way to let Amazon stream the file directly to the browser without having to copy the entire file to the local server? Ideally the application still can choose the name of the force downloaded file (which in many cases is not the same as the file on Amazon).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种解决方案是使用反向代理服务器将请求代理到 Amazon S3,同时重写响应标头以更改文件名并强制下载。
这将消除制作该文件的本地副本的需要,但实际的数据传输仍将通过您的服务器。
我建议研究 Nginx,更具体地研究 proxy_pass 和 add_header 配置选项。
另一个解决方案是在 S3 中创建文件的临时副本(使用 PUT 对象复制调用,因此您不需要将其传输到服务器),然后将此对象上的 Content-Type 和 Content-Disposition 标头设置为强制下载。
然后,您可以将这些文件直接从 S3 流式传输到客户端的浏览器,但您需要定期在 S3 上清理它们,因为下载完成后它们不会自动删除。
One solution is to use a reverse proxy server that proxies the request to Amazon S3 but also rewrites the response headers to change the file name and force the download.
This will eliminate the need to make a local copy of that file but the actual data transfer will still pass through your server.
I suggest looking into Nginx for this and more specifically into the proxy_pass and add_header configuration options.
Another solution would be to make a temporary copy of the file in S3 (using the PUT object copy call so you don't need to transfer it to your server) and then set the Content-Type and Content-Disposition headers on this object to force the download.
You can then stream these files directly from S3 to the client's browser but you'll need to regularly clean them up on S3 since they won't be automatically removed when the download is complete.