If I try more then 10 files I got the warning, but the other files are not uploaded, I cannot upload more than 10 files. What am I doing wrong?
{ node
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.