【问题标题】:Convert JSON.gz to JSON in node js在节点 js 中将 JSON.gz 转换为 JSON
【发布时间】:2015-06-05 11:41:18
【问题描述】:

我正在节点模块中检索来自 S3 存储桶的对象。该对象采用object.json.gz 格式。我需要将其解压缩为object.json,以便能够在节点模块中对其进行解析。以下是代码sn -p

aws.config.update({ accessKeyId: <my_key>, secretAccessKey: <my_secret_key>, region: <my_region> });
var s3 = new aws.S3();
s3.getObject(
  { Bucket: "<my_bucket>", Key: "<my_file_key>"},
  function (error, data) {
    if (error != null) {
        console.log("Error retrieving the Object from the S3 bucket.")
        console.log(error);
    } else {
        zlib.gunzip(data, function(err, buffer){
              if (!err) {
                console.log(buffer);
              }
              else console.log(err);
            });
    }
  }
);

如果我将对象data 记录到控制台,它会记录以下内容,

{ AcceptRanges: 'bytes',
LastModified: 'Thu, 04 Jun 2015 17:41:12 GMT',
ContentLength: '12677',
ETag: '"ebb8f339f569b9aea1038c005442eedd"',
ContentEncoding: 'gzip',
ContentType: 'application/json',
ServerSideEncryption: 'AES256',
Metadata: {},
Body: <Buffer 1f 8b 08 00 00 00 00 00 00 00 ed 7d fb 73 1a 47 d6 f6   bf a2 f2 4f ef 5b b5 c3 f6 fd 32 bf 39 de d8 eb dd 38 71 6c 25 ce e6 ab ad ad be da bc 91 84 02 92 ...> }

如果我登录buffer,它会记录以下内容,

[TypeError: Invalid non-string/buffer chunk]

如果我让 zlib.gunzip 在 data.body 上运行,它会记录以下内容,

<Buffer >

我在互联网上尝试了许多解决方法,但都没有奏效。作为 node.js 的新手,这真的让我很沮丧。 任何帮助将不胜感激。

【问题讨论】:

标签: javascript json node.js amazon-s3 gzip


【解决方案1】:

Paritosh Walvekar 的回答对我有所帮助,但我想为遇到此问题的其他人添加更多实施细节。我的用例是尝试从 S3 读取 ELB 日志(不是 json,而是 .gz 文件)。

  • 不用担心 ResponseContentType。您可以将其保留为默认值。
  • 要将 buff 转换为字符串,您可以使用 buff.toString('utf8') 至少验证某项工作是否正常。
  • 如果在节点运行时 6.10(撰写本文时最新)中使用 lambda,则只需输入 var zlib = require('zlib');在顶部

我的代码:

var AWS = require('aws-sdk');
var zlib = require('zlib');
var s3 = new AWS.S3({apiVersion: '2006-03-01'});

exports.handler = (event, context, callback) => {
    //Other code to keep lambda alive, other bus logic, etc...
    
    //Use s3 sdk to list objects
    //This code is inside a loop of list results
    (function(key){
        var params = {
            Bucket: bucket, /* required */
            Key: key /* required */
        };
        s3.getObject(params, function(err, data) {
            if (err) getLogEvents(null, err); // an error occurred
            else     getLogEvents(data);           // successful response
        });
    })(data.Contents[i].Key);
    
    function getLogEvents(data, err){
        if(err == undefined){
           zlib.gunzip(data.Body,function(error, buff){
           if(error != null){                                 
              console.log(error)
           }
           else{                                             
            console.log(buff.toString('utf8'))
           }
           context.done(null, null);
         });
        }else{
            console.log(err, err.stack);
        }
        
        //Do final bus logic before exiting
     }
};

终于得到纯文本输出让我松了一口气(是的,我混淆了一些数字以隐藏敏感信息):

2017-05-07T18:03:06.319Z 6a92874b-334f-11e7-b021-031f400273ab http 2017-05-07T17:47:12.249487Z app/app-elb/165c3641668dfeh4 50.123.146-1131 -1 503 - 435 788 "GET http://app-elb-9898692007.us-east-1.elb.amazonaws.com:80/ HTTP/1.1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36" - - arn: aws:elasticloadbalancing:us-east-1:676540590099:targetgroup/app-elb-targetgroup/980f40fc23gc342e "Root=1-590f5da0-37650b96857fe0943aa7900b"

【讨论】:

    【解决方案2】:

    我找到了解压缩 JSON.gz 文件的解决方案。我使用了zlib 模块。

     var zlib = require('zlib');
    
     zlib.gunzip(data,function(error, buff){
       if(error != null){                                       
          //An error occured while unzipping the .gz file.
       }
       else{                                                    
        //Use the buff which contains the unzipped JSON.
       }
     });
    

    zlib 可以使用npm install zlib 安装。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 2015-11-05
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多