How to make Rails send a file that is being written

Remi Gabillet wrote:

I'm trying to build a simple proxy to Amazon S3 that would allow a client to request a url that gets mapped to a S3 Object by Rails and streamed back to the client. For S3 integration, I use the highly recommended gem AWS::S3 (http://amazon.rubyforge.org/), and to send a file, I simply use the send_file function.

The concept works BUT the I can't get the file to be sent as it's been downloaded. Even better would be to download the file to memory instead of the disk, I'm really not familiar with Ruby's IO Class though.. If anyone has a suggestion.. ... Then I figured I needed to use the streaming feature of AWS::S3

  # Start a thread that downloads the file in the background   Thread.start do     open('/tmp/tempfile', 'w') do |file|       AWS::S3::S3Object.stream('object_name', 'bucket_name') do |chunk|         file.write chunk       end     end   end

  ## Then send the file as it's being written.   send_file "/tmp/tempfile", :stream => true

This doesn't work, the sent file is often between 0 and 4 bytes.. not cool.

Try using a ruby IO.pipe object instead of the tempfile.