节点FTP:循环中的多个异步调用



我知道这是我已经进入的一些异步难题,但是我正在尝试下载多个文件,从npm的各种FTP软件包的.list方法中进行回调。p>所以类似的东西(注意:JSFTP这里只是node-ftp周围的包装器,我对此有同样的问题):

  ftp = new JSFTP(conf.FTPConfig)
  ftp.ls(conf.remoteFolder, (err, fileList) => {
    if (err) return console.error(err)
    for (let i=0; i<fileList.length; i++){
      file = fileList[i]
      ftp.get(file.name, `./Downloaded/${file.name}`, err => {
        if (err) return console.error(err)
        console.log(`${file.name} copied.`)
      })
    }
  })

如果我不使用循环而只下载一个文件,则一切正常。但是,有了循环,我不断遇到以下错误,但我没有得到任何文件(一个或两个空的文件占位符除外):

(简而言之):Error: 503 Bad sequence of commands,以及下面:'Probably trying a PASV operation while one is in progress

看起来它在循环中启动了我所有的.get调用(显然没有等待任何一个都可以完成,这很好),但是随后无法实际执行所有操作。他们不仅应该并行,异步运行吗?

{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:257:11)
    at Ftp.parseResponse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:174:8)
    at Stream.<anonymous> (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:GitSenecaFTPFTPMonitornode_modulesduplexerindex.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:257:11)
    at Ftp.parseResponse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:174:8)
    at Stream.<anonymous> (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:GitSenecaFTPFTPMonitornode_modulesduplexerindex.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:257:11)
    at Ftp.parseResponse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:174:8)
    at Stream.<anonymous> (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:GitSenecaFTPFTPMonitornode_modulesduplexerindex.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:257:11)
    at Ftp.parseResponse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:174:8)
    at Stream.<anonymous> (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:GitSenecaFTPFTPMonitornode_modulesduplexerindex.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:257:11)
    at Ftp.parseResponse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:174:8)
    at Stream.<anonymous> (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:GitSenecaFTPFTPMonitornode_modulesduplexerindex.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: 503 Bad sequence of commands.
    at Ftp.parse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:257:11)
    at Ftp.parseResponse (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:174:8)
    at Stream.<anonymous> (C:GitSenecaFTPFTPMonitornode_modulesjsftplibjsftp.js:146:24)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at ResponseParser.reemit (C:GitSenecaFTPFTPMonitornode_modulesduplexerindex.js:70:25)
    at emitOne (events.js:96:13)
    at ResponseParser.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 }
{ Error: connect ECONNREFUSED 192.168.100.161:61229
    at Object.exports._errnoException (util.js:1007:11)
    at exports._exceptionWithHostPort (util.js:1030:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '192.168.100.161',
  port: 61229,
  msg: 'Probably trying a PASV operation while one is in progress' }

查看了JSFTP的源后,似乎是创建一个连接,仅使用一个套接字:https://github.com/sergi/sergi/jsftp/jsftp/blob/master/master/lib/jsftp.js#l120

这意味着它不会为每个命令创建一个新连接,而是使用单个连接来发送命令。基本上,您如何通过为要上传的每个文件创建一个新的SFTP实例来规避这一点。

基本上将代码重新加工到此:

ftp = new JSFTP(conf.FTPConfig)
  ftp.ls(conf.remoteFolder, (err, fileList) => {
    if (err) return console.error(err)
    for (let i=0; i<fileList.length; i++){
      file = fileList[i]
      new JSFTP(conf.FTPConfig).get(file.name, `./Downloaded/${file.name}`, err => {
        if (err) return console.error(err)
        console.log(`${file.name} copied.`)
      })
    }
  })

但是,我建议使用某种流控制库,例如async或使用基于承诺的解决方案,因为它可能更易于管理。

使用我提到的async模块,也有类似的问题,因此使用节点JS

从FTP站点下载多个文件

相关内容

  • 没有找到相关文章

最新更新