我很难通过ssh2-sftp-client(https://www.npmjs.com/package/ssh2-sftp-client)从缓冲区上传文件
我使用Multer上传文件,然后我只是定位req.file.buffer
来访问要通过sftp客户端上传的数据,如下所示:
sftp.connect({
host: 'some.IP.address.here',
port: '22',
username: 'username',
password: 'password'
})
.then(() => {
return sftp.put(req.file.buffer, '/var/www/bucket', false);
})
.catch((err) => {
console.log(err, 'catch error');
});
我无法弄清楚如何为存储在Multer对象req.file.originalname
中的上传文件指定名称。
顺便说一句,ssh2-sftp-client返回以下错误:
Error: Failure
image.routes.js:39
code:4
lang:""
message:"Failure"
stack:"Error: Failure\n at SFTPStream._transform (/Users/petrfila/Apps/lifeIQ/liq-api/node_modules/ssh2-streams/lib/sftp.js:410:27)\n at SFTPStream.Transform._read (_stream_transform.js:185:10)\n at SFTPStream._read (/Users/petrfila/Apps/lifeIQ/liq-api/node_modules/ssh2-streams/lib/sftp.js:181:15)\n at SFTPStream.Transform._write (_stream_transform.js:173:12)\n at doWrite (_stream_writable.js:406:12)\n at writeOrBuffer (_stream_writable.js:392:5)\n at SFTPStream.Writable.write (_stream_writable.js:290:11)\n at Channel.ondata (_stream_readable.js:646:20)\n at Channel.emit (events.js:127:13)\n at addChunk (_stream_readable.js:269:12)\n at readableAddChunk (_stream_readable.js:256:11)\n at Channel.Readable.push (_stream_readable.js:213:10)\n at SSH2Stream.<anonymous> (/Users/petrfila/Apps/lifeIQ/liq-api/node_modules/ssh2/lib/Channel.js:166:15)\n at SSH2Stream.emit (events.js:127:13)\n at parsePacket (/Users/petrfila/Apps/lifeIQ/liq-api/node_modules/ssh2-streams/lib/ssh.js:3444:10)\n at ...
__proto__:Object {constructor: , name: "Error", message: "", …}
constructor:function Error() { … }
message:""
name:"Error"
toString:function toString() { … }
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}
catch error
答案 0 :(得分:0)
有点晚了,但是以防万一.....
您使用的版本存在一些错误,并且错误处理非常差。在更高版本中已解决此问题。另外,相对路径名的处理也不统一。此问题已得到解决。
要上传文件并为其指定远程文件名,请确保远程路径包括文件名,而不仅仅是目标目录。
let client = new Client();
client.connect(config)
.then(() => {
return client.put(buffer.data, '/path/to/new-file-name');
.then(f => {
console.log(`File uploaded to ${f}`);
.catch(err => {
console.log(`Error: ${err.message}`);
});
请注意,如果您只能使用本地文件名而不是缓冲区或流,则可以使用fastPut(),它比put()快,因为它并行传输。但是,它只能与路径名字符串一起使用。