简体   繁体   English

如何逐步通过我的Rails服务器从Amazon S3下载大文件

[英]How can I download a large file from Amazon S3 through my Rails server, progressively

I would like to read a content of a file from S3 and pass it to the user. 我想从S3读取文件的内容并将其传递给用户。 I have large files so I cannot just wait until it's saved on my server and then send it to the browser using *x_send_file* because it would take to much time. 我有大文件,所以我不能等到它保存在我的服务器上,然后使用* x_send_file *将其发送到浏览器,因为它需要很长时间。 I would like to send the content to the browser while I am downloading it on my server. 我想在我的服务器上下载内容时将内容发送到浏览器。

So it all passes through my server like some kind of streamed download. 所以这一切都通过我的服务器像某种流式下载。

Yes, this is possible - just fetch the remote file with Rails and either store it temporarily on your server or send it directly from the buffer. 是的,这是可能的 - 只需使用Rails获取远程文件并将其临时存储在服务器上或直接从缓冲区发送。 The problem with this is of course the fact that you need to fetch the file first before you can serve it to the user. 这个问题当然是您需要先获取文件才能将其提供给用户。 See https://www.ruby-forum.com/topic/98626 for a discussion, their solution is something like this: 有关讨论,请参阅https://www.ruby-forum.com/topic/98626 ,他们的解决方案如下:

#environment.rb
require 'open-uri'

#controller
def index
  data = open(params[:file])
  send_data data, :filename => params[:name], ...
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM