class Net::SFTP::Session
The Session
class encapsulates a single SFTP
channel on a Net::SSH
connection. Instances of this class are what most applications will interact with most, as it provides access to both low-level (mkdir, rename, remove, symlink, etc.) and high-level (upload, download, etc.) SFTP
operations.
Although Session
makes it easy to do SFTP
operations serially, you can also set up multiple operations to be done in parallel, too, without needing to resort to threading. You merely need to fire off the requests, and then run the event loop until all of the requests have completed:
handle1 = sftp.open!("/path/to/file1") handle2 = sftp.open!("/path/to/file2") r1 = sftp.read(handle1, 0, 1024) r2 = sftp.read(handle2, 0, 1024) sftp.loop { [r1, r2].any? { |r| r.pending? } } puts "chunk #1: #{r1.response[:data]}" puts "chunk #2: #{r2.response[:data]}"
By passing blocks to the operations, you can set up powerful state machines, to fire off subsequent operations. In fact, the Net::SFTP::Operations::Upload
and Net::SFTP::Operations::Download
classes set up such state machines, so that multiple uploads and/or downloads can be running simultaneously.
The convention with the names of the operations is as follows: if the method name ends with an exclamation mark, like read!, it will be synchronous (e.g., it will block until the server responds). Methods without an exclamation mark (e.g. read) are asynchronous, and return before the server has responded. You will need to make sure the SSH
event loop is run in order to process these requests. (See loop
.)
Constants
- HIGHEST_PROTOCOL_VERSION_SUPPORTED
The highest protocol version supported by the
Net::SFTP
library.
Attributes
The Net::SSH::Connection::Channel object that the SFTP
session is being processed by.
The hash of pending requests. Any requests that have been sent and which the server has not yet responded to will be represented here.
The protocol instance being used by this SFTP
session. Useful for querying the protocol version in effect.
The state of the SFTP
connection. It will be :opening, :subsystem, :init, :open, or :closed.
Public Class Methods
Creates a new Net::SFTP
instance atop the given Net::SSH
connection. This will return immediately, before the SFTP
connection has been properly initialized. Once the connection is ready, the given block will be called. If you want to block until the connection has been initialized, try this:
sftp = Net::SFTP::Session.new(ssh) sftp.loop { sftp.opening? }
# File lib/net/sftp/session.rb, line 78 def initialize(session, version = nil, &block) @session = session @version = version @input = Net::SSH::Buffer.new self.logger = session.logger @state = :closed @pending_requests = {} connect(&block) end