Help us understand the problem. What is going on with this article?

RubyでSFTP

More than 3 years have passed since last update.

net/sftpを用いてsftpを利用する方法を簡単にまとめる。

接続

net/sshに依存している

パスワードを用いて接続する方法

option = {
  password: 'password',
  port: 22,
}

Net::SFTP.start('host', 'username', option) do |sftp|
end

秘密鍵を用いる方法

option = {
  keys: '/path/to/your/identity_key',
  passphrase: 'your-identity-key-pass-phrase',
  port: 22,
}

Net::SFTP.start('host', 'username', option) do |sftp|
end

ファイルのput/get

put

Net::SFTP.start('host', 'username', option) do |sftp|
  sftp.upload!('/path/to/local_file', '/path/to/remote_file')
end

get

Net::SFTP.start('host', 'username', option) do |sftp|
  sftp.download!("/path/to/remote_file", "/path/to/local_file")
end

finc
健康寿命を伸ばすアプリFiNCの開発・運営を行うモバイルヘルステクノロジーベンチャー
https://finc.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away