6

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
claudiut
  • 1,643
  • 4
  • 15
  • 20
  • I am using right_aws. I tried this: `s3 = RightAws::S3Interface.new(aws_access_key, aws_secret_key, :port => 80, :protocol => "http") file = File.new('large.file', 'wb') res = s3.get(bucket_name, file_name) do |chunk| file.write(chunk) end`. I was able to download the file in chunk but how will I send it to the browser in real-time as one piece of file? How will I sync it? Any ideas? Any help will be highly appreciated! Thanks! – claudiut Aug 02 '11 at 07:20
  • I download the file in chunks in a thread on the server. How can I send the data to the client until that thread finishes? I tried to send the file but it sends only a small amount of it. I need it sending continuously until the whole file is downloaded the the server. Can this be done? Can I somehow request the file in byte-ranges smaller that the byte ranges that my file is being downloaded to the server meanwhile? – claudiut Aug 02 '11 at 12:17
  • You get no benefit from using x_send_file if your rails process needs to be locked, you might as well return an iterator and set headers so that browser knows it needs to download the file instead of rendering it inline (http://stackoverflow.com/questions/3507594/ruby-on-rails-3-streaming-data-through-rails-to-client) However, you probably don't want to lock your rails process with this kind of action, try thinking of an option that has the client download the file directly from S3 – bbozo Nov 13 '13 at 15:18

1 Answers1

1

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
MZaragoza
  • 10,108
  • 9
  • 71
  • 116
  • mZaragoza, please reconsider all the "code formatting" suggested edits you're making. They're not an appropriate use of code block tags. See the [Meta site](http://meta.stackexchange.com/questions/209305/user-making-inappropriate-edits-with-backticks) [for more](http://meta.stackexchange.com/questions/135112/inline-code-spans-should-not-be-used-for-emphasis-right). – Michael Petrotta Dec 22 '13 at 01:14
  • 1
    Why did you choose to ignore @Michael's advice then? Flagging this for moderator attention. These are completely pointless, BS edits that others have to clean up just so you can gain your reputation points. – Pekka Dec 22 '13 at 05:38