Reading file from SFTP server using Node.js and SSH2

前端 未结 2 1375
不知归路
不知归路 2021-02-20 05:41

I have a pretty strange problem working with read streams in Node.js. I\'m using SSH2 to create a sftp connection between me and a sftp server. I then try to create a read strea

2条回答
  •  情话喂你
    2021-02-20 06:05

    So after a lot of investigation I finally realized that there was something wrong with the last bits of data that was transmitted in the 'data' event. From my understanding it seems to be a bug in the implementation of the read stream. I was able to get around this problem by using the more simplistic functions (open, fstat, read) in the SSH2 library. This solution works for me. Wanted to share the solution if someone else comes across the same problem.

    Working code:

    sftp.open(config.ftpPath + "/" + m_fileName, "r", function(err, fd) {
    sftp.fstat(fd, function(err, stats) {
        var bufferSize = stats.size,
            chunkSize = 16384,
            buffer = new Buffer(bufferSize),
            bytesRead = 0,
            errorOccured = false;
    
        while (bytesRead < bufferSize && !errorOccured) {
            if ((bytesRead + chunkSize) > bufferSize) {
                chunkSize = (bufferSize - bytesRead);
            }
            sftp.read(fd, buffer, bytesRead, chunkSize, bytesRead, callbackFunc);
            bytesRead += chunkSize;
        }
    
        var totalBytesRead = 0;
        function callbackFunc(err, bytesRead, buf, pos) {
            if(err) {
                writeToErrorLog("downloadFile(): Error retrieving the file.");
                errorOccured = true;
                sftp.close(fd);
            }
            totalBytesRead += bytesRead;
            data.push(buf);
            if(totalBytesRead === bufferSize) {
                m_fileBuffer = Buffer.concat(data);
                writeToLog("downloadFile(): File saved to buffer.");
                sftp.close(fd);
                m_eventEmitter.emit(' downloadFile_Completed ');
            }
        }
    })
    });   
    

提交回复
热议问题