Skip to content

Instantly share code, notes, and snippets.

@charger
Last active August 29, 2015 14:00
Show Gist options
  • Save charger/11204997 to your computer and use it in GitHub Desktop.
Save charger/11204997 to your computer and use it in GitHub Desktop.
SftpUploader
class SftpUploader
def upload!(local_filename, remote_filename)
raise "#{local_filename} not readable" unless File.exist?(local_filename)
connect do |sftp|
dirs = File.dirname(remote_filename).split('/')
dir_to_create = credentials.path
dirs.each do |dir|
dir_to_create = File.join(dir_to_create, dir)
sftp.mkdir(dir_to_create)
end
remote_path = File.join(credentials.path, remote_filename)
sftp.upload!(local_filename, remote_path)
end
end
def connect(&block)
Net::SFTP.start('host', 'pass') do |sftp|
block.call(sftp)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment