gpt4 book ai didi

javascript - 在 Node js中将JSON.gz转换为JSON

转载 作者:搜寻专家 更新时间:2023-11-01 00:17:55 25 4
gpt4 key购买 nike

我正在从 Node 模块中的 S3 存储桶中检索对象。该对象采用 object.json.gz 格式。我需要将它解压缩到 object.json 以便能够在 Node 模块中解析它。以下是代码片段

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 的新手,这真的让我很沮丧。任何帮助将不胜感激。

最佳答案

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

  • 不用担心 ResponseContentType。您可以将其保留为默认值。
  • 要将 buff 转换为字符串,您可以使用 buff.toString('utf8') 至少验证某些功能是否正常工作。
  • 如果将 lambda 与 Node 运行时 6.10(撰写本文时的最新版本)一起使用,则只需输入 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.312:59275 - -1 -1 -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"

关于javascript - 在 Node js中将JSON.gz转换为JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30665642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com