I am retrieving an object form S3 bucket, in a node module. The object is in object.json.gz
format. I need to decompress it to object.json
in order to be able to parse it in the node module. Following is the code snippet
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);
});
}
}
);
If I log the object data
to the console, it logs the following,
{ 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 ...> }
If I log buffer
, it logs the following,
[TypeError: Invalid non-string/buffer chunk]
If I make zlib.gunzip run on data.body, it logs the following,
<Buffer >
I've tried many workarounds over the internet, didn't work. Being a newbie in node.js, this is really frustrating me. Any help would be appreciated.
I found the solution for unzipping the JSON.gz file. I've used the
zlib
module.zlib
can be installed usingnpm install zlib
.