我正在尝试使用 ssh2-sftp 库在 Node.js 中读取/写入文件。当我在 sftp 站点上对较大的 CSV 文件(但不是太大 - 仅像 2 MB)执行 sftp.get,然后在返回的流上读取数据时,在第 14 个 stream.on 之后呼叫挂在我身上(“数据”)调用。我已经用几个不同的示例文件对此进行了测试,并且代码在较小的文件上运行良好。但是,如果 CSV 文件足够大,可以通过第 14 次调用,它就会挂起,就好像它无法再读取一样,即使还有更多内容要读取。而且 stream.on("close") 永远不会被调用。
显然,这是非常奇怪的行为。希望也许有人使用这个库遇到了类似的事情并得到了一些指导。
如果它有帮助,这里有一些代码
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("data") 之后,它就冻结了。可能读取了一半的文件。