gpt4 book ai didi

javascript - 如何在 Node js中使用scp2将文件复制到远程服务器?

转载 作者:搜寻专家 更新时间:2023-10-31 23:36:57 25 4
gpt4 key购买 nike

我想使用 scp2 包将文件从本地服务器复制到 Node js 中的远程服务器。首先.. 使用 multer 将文件上传到本地服务器,然后将文件复制或移动到远程服务器。

我的代码:

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: 'www.do********.in',
username: 'username',
password: '*********',
path: '/uploads/'
}, function(err) {
console.log(req.files[0].path);
console.log("files uploaded in remote server");
res.json({error_code:0,err_desc:null});
});
}
});
}

文件上传到本地服务器正常工作,然后到远程服务器抛出错误

错误:

{ date: 'Mon Nov 13 2017 01:00:22 GMT+0530 (India Standard Time)',
process:
{ pid: 5664,
uid: null,
gid: null,
cwd: 'D:\\sample',
execPath: 'C:\\Program Files\\nodejs\\node.exe',
version: 'v8.2.1',
argv: [ 'C:\\Program Files\\nodejs\\node.exe', 'D:\\sample\\app.js' ],
memoryUsage:
{ rss: 69619712,
heapTotal: 45162496,
heapUsed: 39166256,
external: 149849 } },
os: { loadavg: [ 0, 0, 0 ], uptime: 3537.1088452 },
trace:
[ { column: 11,
file: 'util.js',
function: 'Object.exports._errnoException',
line: 1024,
method: '_errnoException',
native: false },
{ column: 20,
file: 'util.js',
function: 'exports._exceptionWithHostPort',
line: 1047,
method: '_exceptionWithHostPort',
native: false },
{ column: 14,
file: 'net.js',
function: 'TCPConnectWrap.afterConnect [as oncomplete]',
line: 1150,
method: 'afterConnect [as oncomplete]',
native: false } ],
stack:[ 'Error: Can\'t set headers after they are sent.',
' at validateHeader (_http_outgoing.js:504:11)',
' at ServerResponse.setHeader (_http_outgoing.js:511:3)',
' at ServerResponse.header (D:\\sample\\node_modules\\express\\lib\\response.js:730:10)',
' at ServerResponse.send (D:\\sample\\node_modules\\express\\lib\\response.js:170:12)',
' at ServerResponse.json (D:\\sample\\node_modules\\express\\lib\\response.js:256:15)',
' at D:\\sample\\routes\\part.js:302:10',
' at Client.closeHandler (D:\\sample\\node_modules\\scp2\\lib\\scp.js:48:13)',
' at emitNone (events.js:105:13)',
' at Client.emit (events.js:207:7)',
' at Client.<anonymous> (D:\\sample\\node_modules\\scp2\\lib\\client.js:88:10)',
' at emitNone (events.js:105:13)',
' at Client.emit (events.js:207:7)',
' at Socket.<anonymous> (D:\\sample\\node_modules\\ssh2\\lib\\client.js:225:10)',
' at emitOne (events.js:115:13)',
' at Socket.emit (events.js:210:7)',
' at Pipe._handle.close [as _onclose] (net.js:549:12)' ] }

无法识别错误,等待建议或解决问题的可能方法。

提前致谢!

最佳答案

直接使用multer上传文件到远程服务器是不可能的,但是我们可以尝试使用multer-sftp, scp, ssh 技术方法论

使用Node Js上传文件到远程服务器时,我们需要注意一些事情

  1. 用户名和密码必须正确
  2. 相应的端口号应该在远程服务器上打开
  3. 远程目录应该有写入权限

使用 scp2 将文件移动到远程服务器的工作代码:

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");
});
}
});
}

注意:应该安装所需的包并在代码中声明需要的东西。

引用文献:multer-sftp , scp2 , node-ssh

关于javascript - 如何在 Node js中使用scp2将文件复制到远程服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47253109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com