kitaro-tn
3/26/2018 - 3:28 AM

[Ruby]Tempfile to scp upload

Tempfile を SCPでアップロードする

# coding: utf-8
require 'tempfile'
require 'net/scp'
require 'base64'

contents = Base64.encode64("contents")
save_path = "/var/tmp"
host = "localhost"
user = "username"
password = ""

Tempfile.create("upload") do |tf|
  tf.binmode
  tf.write(Base64.decode64(contents))
  tf.rewind
  file_size = tf.size
  Net::SCP.start(host, user, 
                 password: password) do |scp|
    scp.upload! tf.path, save_path
  end
end