class IOP::SFTPFileWriter
Sink
class to write file to SFTP server.
This class an adapter for Net::SFTP::Session
class.
### Use case: store a number of files filled with random data to remote SFTP server reusing session.
require 'iop/net/sftp' require 'iop/securerandom' sftp = Net::SFTP.start('sftp.server', username: 'user', password: '123') begin (1..3).each do |i| ( IOP::SecureRandomGenerator.new(1024) | IOP::SFTPFileWriter.new(sftp, "random#{i}.dat") ).process! end ensure sftp.session.close end
@note this class depends on external net-sftp
gem. @since 0.2
Public Class Methods
Creates class instance.
@param sftp [String, Net::SFTP::Session] SFTP server to connect to
@param file [String] file name to process
@param options [Hash] extra keyword parameters passed to Net::SFTP::Session
constructor
sftp can be either a String
of Net::SFTP::Session
instance. If it is a string a corresponding Net::SFTP::Session
instance will be created with options passed to its constructor.
If sftp is a string, a created SFTP session is managed, e.g. it is closed after the process is complete, otherwise supplied object is left as is and no closing is performed. This allows to reuse SFTP session for a sequence of operations.
# File lib/iop/net/sftp.rb, line 145 def initialize(sftp, file, **options) @options = options @file = file @sftp = sftp end
Public Instance Methods
# File lib/iop/net/sftp.rb, line 166 def process(data = nil) unless data.nil? @sftp.write!(@io, @offset, data) @offset += data.size end end
IOP::Sink#process!
# File lib/iop/net/sftp.rb, line 151 def process! setup begin @io = @sftp.open!(@file, 'w') @offset = 0 begin super ensure @sftp.close(@io) end ensure cleanup end end