How To Copy Files To Remote Server Using Scp2 In Node Js?
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 after that copy or move that
Solution 1:
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
- Username and password should be correct
- The Corresponding port number should be open in remote server
- 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
Post a Comment for "How To Copy Files To Remote Server Using Scp2 In Node Js?"