2016-10-10 85 views
1

我正在尝试使用ssh2-sftp库在Node中读取/写入文件。当我在sftp站点上对更大的CSV文件(但不是太大 - 如2 MB)执行sftp.get,然后读取返回的流上的数据时,第14个流之后的呼叫挂在我身上。 (“数据”)呼叫。我已经用几个不同的示例文件测试过这些代码,并且代码在较小的文件上工作正常。但是,如果一个CSV文件足够大以至于无法通过第14次调用,那么它就会挂起,就好像它不能再读取一样,即使有更多需要阅读的文件。并且stream.on(“关闭”)也不会被调用。使用ssh2 sftp读取较大的sftp文件节点

显然这是非常奇怪的行为。希望也许有人遇到类似的问题,并使用这个库,并提供一些指导。

如果有帮助可言,这里是一些代码

sftp.get(currentFileName).then((readStream) => { 
    var counter = 0; 

    readStream.on("data", function(d) { 
     counter++; 
     console.log("counter = " + counter); 
    }); 

    readStream.on("close", function(err) { 
     if (err) { 
     console.error("Problem with read stream for file " + currentFileName + ", error = ", err); 
     } 
     //done reading the individual file, all went well 
     else { 
     console.log("done reading the stream"); 
     } 
    }); 

    readStream.on('error', function(e){ 
     console.error("Error retrieving file: " + e); 
    }) 

    readStream.resume(); 

,第14届呼叫到readStream.on(“数据”)之后,它只是冻结了。也许有一半的文件被读取。

回答

1

问题原来是ssh2-sftp似乎在运行基础ssh2库的过期版本。从ssh2-sftp切换到ssh2的最新(0.5.2)版本并直接使用该库修复问题(可能是这一个:https://github.com/mscdex/ssh2/issues/450