LoginSignup
0
0

More than 5 years have passed since last update.

[Ruby]Base64エンコードされたデータをSCPでアップロードする

Posted at

概要

API経由でBase64エンコードされたデータを、指定サーバにアップロードするようなケースを想定してます。

必須Gem

$ gem install net-scp

サンプル

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

contents = Base64.encode64("contents")
save_path = "/var/tmp/test.txt"
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
  • Tempfileを利用し、一時ファイルからサーバへ直接ファイルを送信
  • 一時ファイルに書き込む際に、デコードを行う
  • 保存先のファイル名は、APIから送信されてきたファイル名を用いる等の工夫は必要
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0