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