我已经编写了代码来建立SFTP连接,并使用Node.jssftp.put
命令将文件传输到SFTP服务器。传输文件时出现以下错误。我可以成功建立连接。但是我无法向服务器读取/写入文件。我在下面附上了代码
代码
let sftp = new client();
let filename = "sound.mp3";
const filePath = path.join(__dirname, '../audio', filename)
const putConfig = {
flags: 'w', // w - write and a - append
encoding: null, // use null for binary files
mode: 0o666, // mode to use for created file (rwx)
autoClose: true // automatically close the write stream when finished
};
sftp.connect({
host: 'host',
port: '22',
username: 'xxxx',
password: 'xxx'
}).then(() => {
return sftp.put(filePath, '/', putConfig)
}).then(data => {
console.log(data, 'the data info');
}).catch(err => {
console.log(err, 'catch error');
});
错误
Error: put->put: Failure /data
at fmtError (D:projectnode_modulesssh2-sftp-clientsrcutils.js:53:18)
at SftpClient.put (D:projectnode_modulesssh2-sftp-clientsrcindex.js:684:13)
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
code: 4,
custom: true
}
D:projectnode_modulesssh2libprotocolcryptopoly1305.js:20
function J(a){if(b.onAbort)b.onAbort(a);L(a);O=!0;a=new WebAssembly.RuntimeError("abort("+a+"). Build with -s ASSERTIONS=1 for more info.");r(a);throw a;}var V="data:application/octet-stream;base64,",W="data:application/octet-stream;base64,AGFzbQEAAAABIAZgAX8Bf2ADf39/AGABfwBgAABgAAF/YAZ/f39/f38AAgcBAWEBYQAAAwsKAAEDAQAAAgQFAgQFAXABAQEFBwEBgAKAgAIGCQF/AUGAjMACCwclCQFiAgABYwADAWQACQFlAAgBZgAHAWcABgFoAAUBaQAKAWoBAAqGTQpPAQJ/QYAIKAIAIgEgAEEDakF8cSICaiEAAkAgAkEAIAAgAU0bDQAgAD8AQRB0SwRAIAAQAEUNAQtBgAggADYCACABDwtBhAhBMDYCAEF/C4wFAg5+Cn8gACgCJCEUIAAoAiAhFSAAKAIcIREgACgCGCESIAAoAhQhEyACQRBPBEAgAC0ATEVBGHQhFyAAKAIEIhZBBWytIQ8gACgCCCIYQQVsrSENIAAoAgwiGUEFbK0hCyAAKAIQIhpBBWytIQkgADUCACEIIBqtIRAgGa0hDiAYrSEMIBatIQoDQCASIAEtAAMiEiABLQAEQQh0ciABLQAFQRB0ciABLQAGIhZBGHRyQQJ2Qf///x9xaq0iAyAOfiABLwAAIAEtAAJBEHRyIBNqIBJBGHRBgICAGHFqrSIEIBB+fCARIAEtAAdBCHQgFnIgAS0ACEEQdHIgAS0ACSIRQRh0ckEEdkH///8fcWqtIgUgDH58IAEtAApBCHQgEXIgAS0AC0EQdHIgAS0ADEEYdHJBBnY
RuntimeError: abort(Error: put->put: Failure /data). Build with -s ASSERTIONS=1 for more info.
at process.J (D:projectnode_modulesssh2libprotocolcryptopoly1305.js:20:53)
at process.emit (events.js:210:5)
at process.EventEmitter.emit (domain.js:475:20)
at processPromiseRejections (internal/process/promises.js:201:33)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
SftpClient.put
的第二个参数是目标远程文件的路径,而不仅仅是目标远程文件夹的路径。
所以应该是这样的:
return sftp.put(filePath, '/' + filename, putConfig)