node.js - how do I send (put) multiple files using nodejs ssh2-sftp-client

node.js – how do I send (put) multiple files using nodejs ssh2-sftp-client?

node.js – how do I send (put) multiple files using nodejs ssh2-sftp-client?

Below code using es6-promise-pool as an example and it is working for me:

First need to install es6-promise-pool:

npm install es6-promise-pool --save

Code:

let Client = require(ssh2-sftp-client);
let PromisePool = require(es6-promise-pool);

var files = [ (list of files to transfer) ]; // results.romlist[i].filename
var config = { (sftp config) };

const sendFile = (config, filename) => {
    return new Promise(function (resolve, reject) {
    let sftp = new Client();
    console.log(filename);
    sftp.on(keyboard-interactive, (name, instructions, instructionsLang, prompts, finish) => { finish([config.password]); });
    sftp.connect(config).then(() => {
        return sftp.put((local path) + filename, /home/pi/RetroPie/roms/atari2600/ + filename);
    }).then(() => {
        console.log(finish +filename);
        sftp.end();
        resolve(filename);
    }).catch((err) => {
        console.log(err, catch error);
    });
  });
};

var count = 0;
var sendFileProducer = function () {
    console.log(count=+count);
    if (count < 100) {
        count++;
        return(sendFile(config, files[count]));     
    } else {
        return null;
    }
}

// The number of promises to process simultaneously.
var concurrency = 10;

// Create a pool.
var pool = new PromisePool(sendFileProducer, concurrency)

pool.start().then(function () {
    console.log({message:OK}); // res.send({message:OK});
});

The code does not optimized for network traffic since it start a SFTP for each file. However, it is still very effective as you can control the concurrency that suit for different situations.

This worked for me:
https://github.com/theophilusx/ssh2-sftp-client/issues/73

const putFiles = (sftpConfig, fileList) => {
  const sftp = new sftpClient()
  return new Promise(function(resolve, reject) {
    sftp
    .connect(sftpConfig)
    .then(() => {
      return Promise.all(fileList.map(args => {
        return sftp.put.apply(sftp, args)
      }))
    })
    .then(() => sftp.end())
    .then(resolve)
    .catch(reject)
  });
}

node.js – how do I send (put) multiple files using nodejs ssh2-sftp-client?

The npm package sftp-upload worked for me to upload more than 10 files in a directory to a remote server.
You can refer this : https://www.npmjs.com/package/sftp-upload

Please find the below example code in nodejs, which I have tested and it will work . Please substitute your required data for the fields :

  • username: sftp servers username.
  • path: Path to a directory(in the local system) containing the files that is going to be uploaded to the server.
  • remoteDir:Path in the remote directory where files are going to be uploaded.
var SftpUpload = require(sftp-upload);
var fs = require(fs);
         
       sftp2 = new SftpUpload({
            host:remote ip address,
            username:username,
            password:****,
            path: /user/Desktop/test1,
            remoteDir: /user/Desktop/test1,
            excludedFolders: [**/.git, node_modules],
            exclude: [.gitignore, .vscode/tasks.json],
            dryRun: false
        });
     
        sftp2.on(error, function(err) {
            throw err;
        })
        .on(uploading, function(progress) {
            console.log(Uploading, progress.file);
            console.log(progress.percent+% completed);
        })
        .on(completed, function() {
            console.log(Upload Completed);
        })
        .upload();

This code will copy all the files present in the test1 directory in the local system to the test1 directory in the remote server. If the test1 directory is not present in the remote server, it will be automatically created and the files will be copied.

If there is subfolder inside the test1 directory ,then that subfolder and its contents will also be copied to the remote machine.

Related posts on node js :

Leave a Reply

Your email address will not be published. Required fields are marked *