Par*_*osh 2 javascript json gzip amazon-s3 node.js
我正在节点模块中从 S3 存储桶检索对象。该对象已object.json.gz格式化。我需要将其解压缩,object.json以便能够在节点模块中解析它。以下是代码片段
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);
});
}
}
);
Run Code Online (Sandbox Code Playgroud)
如果我将对象记录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 ...> }
Run Code Online (Sandbox Code Playgroud)
如果我登录buffer,它会记录以下内容,
[TypeError: Invalid non-string/buffer chunk]
Run Code Online (Sandbox Code Playgroud)
如果我让 zlib.gunzip 在 data.body 上运行,它会记录以下内容,
<Buffer >
Run Code Online (Sandbox Code Playgroud)
我在互联网上尝试了很多解决方法,但没有成功。作为 Node.js 的新手,这真的让我很沮丧。任何帮助,将不胜感激。
Paritosh Walvekar 的回答对我有帮助,但我想为遇到此问题的其他人添加更多实现细节。我的用例是尝试从 S3 读取 ELB 日志(它们不是 json,但仍然是 .gz 文件)。
我的代码:
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
}
};Run Code Online (Sandbox Code Playgroud)
我终于得到了纯文本输出,这让我松了一口气(是的,我混淆了一些数字来隐藏敏感信息):
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“获取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,如 Gecko) Chrome/58.0.3029.96 Safari/537.36" - - arn:aws:elasticloadbalancing:us-east-1:676540590099:targetgroup/app-elb-targetgroup/980f40fc23gc342e "Root=1-590f5da0-37650b96857fe0943a a7900b"