spmjs / node-scp2

[MAINTAINER WANTED] A pure javascript scp program based on ssh2.
384 stars 96 forks source link

Support SCP Buffer Rather Than Local File #89

Open jthomerson opened 7 years ago

jthomerson commented 7 years ago

I would like to be able to pass in a buffer rather than a file path as the first argument to scp. This would allow me to SCP files from AWS S3 (or elsewhere - something I generate in-memory) without needing to write it to a temporary file.

onebytegone commented 7 years ago

@jthomerson While a little more verbose than passing a Buffer directly to the scp function, the code snippet below should provide the desired behavior. The low level API allows for copying data from a Buffer, see write under Methods.

var scpClient = require('scp2'),
    client;

client = new scpClient.Client({
   host: 'example.com',
   username: 'your-user',
   password: '******'
});

client.write({
   destination: 'target.txt',
   content: new Buffer('hello world\n')
}, function(err) {
   client.close();
   if (err) {
      throw err;
   }
});

Note: The docs on write are not completely correct as it will not take a string for content, an error will be thrown from lib/client.js#L228.

Expansion of the scp API to allow for running the following would be nice:

var scpClient = require('./lib/scp');

scpClient.scp(
   new Buffer('hello world\n'),
   'admin:password@example.com:port:/home/admin/target.txt',
   function(err) {
      if (err) {
         throw err;
      }
   }
);