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

后端 未结 3 1857
醉话见心
醉话见心 2021-01-17 01:49

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

3条回答
  •  一生所求
    2021-01-17 02:25

    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.

提交回复
热议问题