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

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

提交回复
热议问题