Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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.

like image 303
claudiut Avatar asked Aug 02 '11 06:08

claudiut


1 Answers

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. 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:

#environment.rb
require 'open-uri'

#controller
def index
  data = open(params[:file])
  send_data data, :filename => params[:name], ...
end
like image 67
MZaragoza Avatar answered Oct 07 '22 05:10

MZaragoza