NodeJS 错误代码:connect:已定义现有的SFTP连接

vnjpjtjt  于 2022-11-22  发布在  Node.js
关注(0)|答案(1)|浏览(117)

在我的node.js应用程序中,我使用ssh 2-sftp-client每5秒上传一张图片。
上传工作正常,但在重复该功能时,我收到错误消息

node .\upload.js
uploaded screenshot to server as test_screenshot_uploadI3Mjc.png (first run)
Error: connect: An existing SFTP connection is already defined (second run)

我的代码是:

let Client = require('ssh2-sftp-client');
let sftp = new Client();

function uploadScreenshot() {
    randomNumber = Buffer.from(Math.random().toString()).toString("base64").substring(10,15);
    filename = "test_screenshot_upload"+randomNumber+".png";
    sftp.connect({
      host: 'ssh.strato.de',
      port: '22',
      username: 'user',
      password: 'pass'
  }).then(() => {
    sftp.put('screenshot.png', '/path/folder/folder/'+filename);
  }).then(data => {
      console.log("uploaded screenshot to server as " + filename);
  }).catch(err => {
    console.log(err, 'catch error');
  });  
}

function foo() {
    uploadScreenshot();
    setInterval(foo, 5000);
}

foo();

我试着在sftp.put后添加sftp.end();,但是没有用。你能给予我一个提示吗?我做错了什么?

xriantvc

xriantvc1#

我所需要做的就是

let sftp = new Client();

进入

function uploadScreenshot()

它现在起作用了。

相关问题