gdonald
gdonald

Reputation: 1031

How to make Ruby Net::SFTP not block, so I can begin streaming a remote download immediately?

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

Upvotes: 0

Views: 377

Answers (0)

Related Questions