Basic usage of Net::SFTP
masterUse Net::SFTP.start to establish a session. The block provides an sftp session object used to perform file transfers, directory operations, and file I/O.
Common operations include:
upload!(local, remote): Upload a file or directory.download!(remote, local): Download a file or directory.download!(remote): Download remote data directly to a buffer.mkdir!(path): Create a directory.dir.foreach(path): Iterate through directory entries.
require 'net/sftp'
Net::SFTP.start('host', 'username', :password => 'password') do |sftp|
# upload a file or directory to the remote host
sftp.upload!("/path/to/local", "/path/to/remote")
# download a file or directory from the remote host
sftp.download!("/path/to/remote", "/path/to/local")
# grab data off the remote host directly to a buffer
data = sftp.download!("/path/to/remote")
# create a directory
sftp.mkdir! "/path/to/directory"
# list the entries in a directory
sftp.dir.foreach("/path/to/directory") do |entry|
puts entry.longname
end
end