0

How can I acquire a remote file using Net::SFTP and stream it without waiting for the entire file to download?

My test.zip file is 1GB in size. When I run this code my browser does nothing for several minutes and then the download finally begins. I'd like it to begin streaming the file sooner than that. I have to use Net::SFTP or something like it since the file is only available via SSH or SFTP.

I've also tried Net::SFTP's download and download! methods and got the same result.

headers['Content-Type'] = 'application/zip'
headers['Content-disposition'] = "attachment; filename=test.zip"
self.response_body = Enumerator.new do |lines|
  Net::SFTP.start('example.com', 'foo', keys: ['~/.ssh/id_rsa.pub']) do |sftp|
    file = sftp.file.open('/path/to/test.zip')
    lines << file.read(4096) until file.eof?
  end
end
gdonald
  • 984
  • 2
  • 12
  • 23
  • I know pretty much nothing of ruby, so if it's to silly ...bear with me ... does something like `self.response_body = file` can work ? – Lohmar ASHAR Feb 27 '18 at 22:39
  • In the mean time I've found this page [Streaming Large Data Responses with Rails](https://coderwall.com/p/kad56a/streaming-large-data-responses-with-rails), most importantly it hints on using "Transfer-Encoding: chunked" header (beside others). – Lohmar ASHAR Feb 27 '18 at 22:45

0 Answers0