0

リモートのsftpサイトからxmlをダウンロードし、ファイルの内容をREXMLに読み込んで、データをDBに保存しようとしています。これまでのところ、コードはファイルの読み取りまでであり、f.gets行は次のエラーで失敗します。

/usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:846:in `wait_for': Net::SFTP::StatusException (4, "failure") (Net::SFTP::StatusException)
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:257:in `read!'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:162:in `fill'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:100:in `block in gets'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:93:in `loop'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file.rb:93:in `gets'
        from ./test.rb:10:in `block (3 levels) in <main>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/file_factory.rb:45:in `open'
        from ./test.rb:9:in `block (2 levels) in <main>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/dir.rb:33:in `block in foreach'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/dir.rb:33:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/operations/dir.rb:33:in `foreach'
        from ./test.rb:8:in `block in <main>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `call'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `block in do_version'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `do_version'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:909:in `when_channel_polled'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/channel.rb:311:in `call'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/channel.rb:311:in `process'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:214:in `block in preprocess'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:214:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:214:in `preprocess'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:197:in `process'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in `block in loop'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in `loop'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-ssh-2.5.2/lib/net/ssh/connection/session.rb:161:in `loop'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:802:in `loop'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:787:in `connect!'
        from /usr/local/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp.rb:32:in `start'
        from ./test.rb:7:in `<main>'

これが私のコードです:

#!/usr/bin/ruby

require 'rubygems'
require 'net/sftp'
require 'rexml/document'

Net::SFTP.start('some.example.host','myuser',:password => 'fake') do |sftp|
    sftp.dir.foreach("/path/to/mydir/") do |xml|
        sftp.file.open("/path/to/mydir/"+xml.name) do |f|
            puts f.gets # < THIS BREAKS IT
        end
    end  
end

コードはダーティで、今のところテストです。リモートディレクトリ内のすべてのファイルを読み取り、各ファイルの最初の行を印刷することを期待しています。

助けてくれてありがとう。

4

2 に答える 2

2

私はあなたが探しているのはこれのどちらかだと思います:

sftp.download!("/path/to/remote", "/path/to/local")

またはこれ

 data = sftp.download!("/path/to/remote")

ドキュメントからこれを取得しました:http://net-ssh.rubyforge.org/sftp/v2/api/

于 2012-09-25T10:25:56.280 に答える
0

SFTPユーザーがアクセスできないファイルにアクセスしようとしたときに、同様のエラーが発生しました。

Net::SFTP::StatusException: Net::SFTP::StatusException
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:846:in `wait_for'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:561:in `stat!'
    from (irb):80
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
于 2013-05-31T20:30:44.787 に答える