Search code examples
rubyftpsftpnet-sftp

Ruby Net::SFTP upload hangs


I am using the net-sftp gem to upload a file to an ftp server. Here's my code:

require "net/sftp"

Net::SFTP.start(url, username, password: password) do |sftp|
  sftp.upload!(file_path, "/")
end

It just hangs at the upload line, and eventually times out with the error Net::SSH::Disconnect: connection closed by remote host. I am able to connect via SFTP using FileZilla using the same url, username, and password.

I tried running the non-block version with verbose: :debugas well:

sftp = Net::SFTP.start(test.ftphost.com, ftp_username, password: ftp_password, verbose: :debug)

^ this produced output that shows that the connection was good:

I, [2015-04-29T10:32:51.381339 #25769]  INFO -- net.ssh.connection.session[3fc8a502c24c]: channel_success: 0 D, [2015-04-29T10:32:51.381429 #25769] DEBUG -- net.sftp.session[3fc8a5025d70]: sftp subsystem successfully started

Then I entered the following:

sftp.upload!("/Users/marina/Desktop/test.png", "/")

The output is stuck like this:

I, [2015-04-29T10:32:55.035471 #25769]  INFO -- net.sftp.session[3fc8a5025d70]: sending open packet (0)
D, [2015-04-29T10:32:55.035740 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: queueing packet nr 12 type 94 len 44
D, [2015-04-29T10:32:55.036149 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: sent 68 bytes
D, [2015-04-29T10:32:55.119070 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: read 52 bytes
D, [2015-04-29T10:32:55.119356 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: received packet nr 11 type 96 len 28
I, [2015-04-29T10:32:55.119470 #25769]  INFO -- net.ssh.connection.session[3fc8a502c24c]: channel_eof: 0
D, [2015-04-29T10:32:55.195747 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: read 120 bytes
D, [2015-04-29T10:32:55.196037 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: received packet nr 12 type 98 len 44
I, [2015-04-29T10:32:55.196176 #25769]  INFO -- net.ssh.connection.session[3fc8a502c24c]: channel_request: 0 exit-status false
D, [2015-04-29T10:32:55.196445 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: received packet nr 13 type 97 len 28
I, [2015-04-29T10:32:55.196527 #25769]  INFO -- net.ssh.connection.session[3fc8a502c24c]: channel_close: 0
D, [2015-04-29T10:32:55.196743 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: queueing packet nr 13 type 97 len 28
D, [2015-04-29T10:32:55.196806 #25769] DEBUG -- net.sftp.session[3fc8a5025d70]: sftp channel closed
D, [2015-04-29T10:32:55.197022 #25769] DEBUG -- tcpsocket[3fc8a4cf464c]: sent 52 bytes

Any ideas?


Solution

  • The issue was that I was putting the wrong target location. It shouldn't just be a directory, but the actual filename as well. So instead of saying the following

    require "net/sftp"
    
    Net::SFTP.start(url, username, password: password) do |sftp|
      sftp.upload!(file_path, "/")
    end
    

    I needed to add the filename:

    require "net/sftp"
    
    Net::SFTP.start(url, username, password: password) do |sftp|
      sftp.upload!(file_path, "/filename.extension")
    end
    

    And the block syntax did work for me.