【问题标题】:Cloud function finished with status: 'connection error' for large files云功能以状态完成:大文件的“连接错误”
【发布时间】:2018-04-08 16:27:20
【问题描述】:

我有一个 Google Cloud 函数,它从 Cloud Storage 读取多行文件并作为带分隔符的单行文件写回 Cloud Storage。

以下代码适用于小文件,但对于大文件 (150 MB),它会停止并显示状态:“连接错误”。

我使用createReadStreamcreateWriteStream,如Documentation 中所述。

注意:我有用于测试的硬编码输入文件名

index.js

const fs = require('fs')
const readline = require('readline');
const byline = require('byline');
const storage = require('@google-cloud/storage')();
const stream = require('stream');

exports.processFiles = function(event, callback) {
const bucket = storage.bucket('src_bucket');
const targetbucket = storage.bucket('cf100');
const remoteFile = bucket.file('/files/mylog.log');


const outremoteFile = targetbucket.file('processed_log.log');

var line_no = 1;
var segmentsLineCheck = ["s1", "s2", "s3"];
var segments = [];

var  gcsStream = remoteFile.createReadStream();



let remoteWriteStream = outremoteFile.createWriteStream({ resumable: false,
     metadata : { 
        contentType : 'text/plain'  
     }
  }); 


var lineStream = byline.createStream(gcsStream);
lineStream.on('data', function(line) {
 transform(line.toString()); 
});

lineStream.on('error', (err) => {
            console.log('lineStream Err'+err);
        });

lineStream.on('finish', () => {
    remoteWriteStream.end();
            console.log('lineStream finished' );
        });


var isSegmentStarted = false;
var segmentString = '';
var segmentFound = '';

function transform(line) {

    //if line contains any of the segmentsLineCheck then add the line to segments array
    //else append the line followed by $$

    if (new RegExp(segmentsLineCheck.join("|")).test(line)) {
        // At least one match in segmentsLineCheck

        for(var i=0; i<segmentsLineCheck.length; i++) {
            if(line.indexOf(segmentsLineCheck[i])!==-1) {
                segmentFound = segmentsLineCheck[i];
                segmentsLineCheck.splice(i, 1);
                break;
            }
        }

        if(!isSegmentStarted) {
            console.log('segment forund - ' , segmentFound);
            console.log('segments remaining - ' , segmentsLineCheck);

            segmentString = ' $$ ' + line;
            isSegmentStarted = true;
        }
    } else if(line.indexOf('[20')!==-1 && isSegmentStarted) {
        remoteWriteStream.write(line+ ' $$ '); 
        isSegmentStarted = false;
        if(segmentString!=='') { 
            segments[segments.length] = segmentString;
            segmentString = ''; 
        }   
    } else if(isSegmentStarted) {
        segmentString += line;
    } else if(!isSegmentStarted){
        remoteWriteStream.write(line+ ' $$ ');
    }

    line_no++;

}
callback();
};

【问题讨论】:

  • 我尝试使用您的代码重现您的问题,但是对于小文件和大文件,我都收到“忽略已完成函数的异常”。我从未遇到过“连接错误”。您能否更新添加 package.json 的帖子?所以我可以确保我们使用相同的库。到目前为止,您测试了哪些文件大小?我想知道文件何时大到足以触发“连接错误”。
  • @RubénC。是的,即使对于大文件,这也很有效。实际上问题出在我试图处理的文件上。谢谢。

标签: node.js google-cloud-storage google-cloud-functions


【解决方案1】:

本文中的代码适用于小型和大型文件。我遇到的问题是由于文件不正确。我保留了这篇文章,以便对其他人有所帮助。

【讨论】:

  • 您能否分享更多关于“不正确的文件”是什么意思的详细信息?出了什么问题?
  • @VicSeedoubleyew 代码从我的输入文件中查找分隔符,例如 s1,s2,s3,但由于我传递了一个不包含这些分隔符的错误文件,因此导致整个文件都保存在内存中。因此错误。
  • 感谢您的回答!所以你的意思是错误是谷歌云函数达到了它的内存限制?
  • 在这种情况下,错误可能与内存限制有关。
猜你喜欢
  • 2020-05-11
  • 2021-10-22
  • 1970-01-01
  • 2017-08-26
  • 2019-12-04
  • 1970-01-01
  • 2019-05-15
  • 2023-03-09
  • 2018-12-14
相关资源
最近更新 更多