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

后端 未结 3 1856
醉话见心
醉话见心 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:20

    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)
      });
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-17 02:39

    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 server's 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 it's contents will also be copied to the remote machine.

    0 讨论(0)
提交回复
热议问题