node.js - Node.js ssh2-sftp-client 错误:fastPut:服务器本地没有响应
问题描述
我正在使用node -v v14.17.0
and "ssh2-sftp-client": "^7.0.0"
and 方法fastPut
https://github.com/theophilusx/ssh2-sftp-client#sec-5-2-9
检查远程文件没问题,所以连接正常。
我的环境是wsl2 Ubuntu-20.04
我面临的问题是错误
RuntimeError: abort(Error: fastPut: No response from server Local: /home/draganddrop/testi.txt Remote: Downloads/testi.txt). Build with -s ASSERTIONS=1 for more info.
at process.J (/home/draganddrop/node_modules/ssh2/lib/protocol/crypto/poly1305.js:20:53)
at process.emit (events.js:376:20)
at processPromiseRejections (internal/process/promises.js:245:33)
at processTicksAndRejections (internal/process/task_queues.js:96:32)
我也尝试过使用sftp> put /home/draganddrop/testi.txt Downloads/testi.txt
控制台,它有效。
我正在使用的代码:
let Client = require('ssh2-sftp-client');
let sftp = new Client();
let remotePath = 'Downloads/testi.txt';
let localPath = '/home/draganddrop/testi.txt'
const config = {
host: 'XX.XX.XXX.XXX',
port: '22',
username: 'XXXXX',
password: 'XXXXXX'
};
sftp.connect(config)
.then(() => {
sftp.fastPut(localPath, remotePath);
//return sftp.exists(remotePath);
})
//.then(data => {
// console.log(data); // will be false or d, -, l (dir, file or link)
//})
.then(() => {
sftp.end();
})
.catch(err => {
console.error(err.message);
});
我不知道是什么导致了这个错误,我尝试了不同的路径并得到了错误的路径错误或这个。可能是什么原因?
解决方案
问题的原因是连接在执行完成之前关闭fastPut
。
您正在运行 connect,之后在 first.then
中,该方法fastPut
异步运行,它不等待完成执行并将 undefined 返回到.then
链的下一个方法。
要解决问题,您只需要返回收到的承诺fastPut
sftp.connect(config)
.then(() => sftp.fastPut(localPath, remotePath))
.then((data) => {/* do something*/}
.finally(() => sftp.end())
推荐阅读
- c++ - Qt .exe 文件错误
- sql - 使用 SQL 根据查询中的帐户汇总数据
- javascript - 按键盘上的“Enter”键时更改 IFRAME 地址
- python - OrderdDict 的行为
- laravel-5.5 - 拉拉维尔 5.5。- 此集合实例上不存在属性 [xxxx]
- react-native - React Native:如何在打开链接时设置 HTTP 标头,就像使用 Linking.openURL
- javascript - 如何在一个函数节点中拆分多个 msg.payloads,节点红色
- angularjs - 检查 angularJS 1.7.0 中的 http 请求
- gradle - 添加优雅按钮依赖后出现错误
- mysql - 错误代码:1815 内部错误:JSON 包装器:程序中的意外类型