Node.js ssh2-sftp-client错误:fastPut:服务器没有响应 本地

4
我正在使用 node -v v14.17.0"ssh2-sftp-client": "^7.0.0",并且使用方法 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);
        });

我不知道是什么原因导致了这个错误,我已经尝试过不同的路径,但要么出现了错误的路径,要么就是出现了这种情况。可能的原因是什么?

2
嗨 @eemilk,看起来异步函数fastPut和第二个then中存在问题,它在放置文件之前关闭连接,请尝试在第一个then中返回promise return sftp.fastPut(localPath, remotePath); - Pavlo Naumenko
@PavloNaumenko 这似乎解决了问题。 - eemilk
我在答案中写了同样的内容。 - Pavlo Naumenko
请帮忙解决这个问题 https://stackoverflow.com/questions/72277310/node-js-ssh2-sftp-client-error-fastput-unhandledpromiserejectionwarning-error - its me
1个回答

6
问题的原因是连接在执行 fastPut 之前就关闭了。
你正在运行 connect,然后在第一个 .then 中异步运行方法 fastPut,它不会等待执行完成并返回 undefined 到链的下一个 .then 方法。
要解决问题,只需返回从 fastPut 接收到的 promise 即可。
sftp.connect(config)
        .then(() => sftp.fastPut(localPath, remotePath))
        .then((data) => {/* do something*/}
        .finally(() => sftp.end())

第一个“then”应该返回什么?返回sftp.fastPut.... - BertC
抱歉@BertC,我没有理解问题。第一个then中的回调是箭头函数表达式,因此它返回sftp.fastPut的结果。 - Pavlo Naumenko
1
是的,@Pavlo,你说得完全正确。我错过了缺少的花括号。对此我很抱歉。 - BertC
请帮忙解决这个问题 https://stackoverflow.com/questions/72277310/node-js-ssh2-sftp-client-error-fastput-unhandledpromiserejectionwarning-error - its me

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接