how to copy files to remote server using scp2 in node js?

后端 未结 1 2009
不思量自难忘°
不思量自难忘° 2021-01-21 08:48

I want to copy the files from local server to remote server in Node js using scp2 package. First of all.. Files uploaded to local server using multer

相关标签:
1条回答
  • 2021-01-21 09:24

    Uploading Files to remote server using multer is not possible directly, But we can play around with multer-sftp, scp, ssh techniques Methodologies

    When Uploading Files to remote server using Node Js, We need to take care few things

    1. Username and password should be correct
    2. The Corresponding port number should be open in remote server
    3. The Remote Directory Should have Write Access

    Working Code using scp2 to move the files to remote server:

    exports.newFileUpload =  function(req , res , next){
    
        var storage = multer.diskStorage({ //multers disk storage settings
          destination: function (req, file, cb) {
             cb(null, 'uploads/');
          },
          filename: function (req, file, cb) {
              var datetimestamp = Date.now();
              cb(null,  datetimestamp+ '-' +file.originalname);
          }
      });
            var upload = multer({ storage: storage, limits: { fieldSize: 25 * 1024 * 1024 }}).array('file');
            upload(req,res,function(err){
                    console.log(req.body);
                    console.log(req.files);
                if(err){
                    res.json({error_code:1,err_desc:err});
                    console.log("Error Occured", err);
                    return;
                }else{
    
    
                    client.scp(req.files[0].path, {
                        host: 'host',
                        username: 'username',
                        password: '*********',
                        path: '/uploads/'
                    }, function(err) {
                        console.log(req.files[0].path);
                        console.log("files uploaded in remote server");
                    });
                }
            });
    }
    

    Note: Should install required packages as well as declare require things in code.

    References: multer-sftp, scp2 , node-ssh

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