ungzip nodejs中的数据

时间:2016-02-09 00:54:24

标签: node.js zlib aws-lambda amazon-kinesis

我试图使用下面的代码解压缩base64解码的缓冲区对象,但回调函数没有被执行。谁能告诉我我在这里缺少什么

function (event, context) {
    event.Records.forEach(function (record) {
        // Kinesis data is base64 encoded so decode here
        var payload = new Buffer(record.kinesis.data, 'base64');
        zlib.gunzip(payload, function (err, data) {
                console.log("new data2");
            if (!err) {
                console.log("new data3");
                console.log(data);
            }
            else {
                console.log("new data4");
                console.log(err, err.stack);
            }
        });
    });
    context.done();
};

1 个答案:

答案 0 :(得分:3)

context.done()调用移动到您的回调中,以便Lambda函数不会提前终止。像这样:

function (event, context) {
    event.Records.forEach(function (record) {
        // Kinesis data is base64 encoded so decode here
        var payload = new Buffer(record.kinesis.data, 'base64');
        zlib.gunzip(payload, function (err, data) {
            console.log("new data2");
            if (!err) {
                console.log("new data3");
                console.log(data);
            }
            else {
                console.log("new data4");
                console.log(err, err.stack);
            }
            context.done();
        });
    });
};