【发布时间】: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 的新手,这真的让我很沮丧。 任何帮助将不胜感激。
【问题讨论】:
-
在搜索“node un-gz”后我发现了这个,我想这就是你想要的:gist.github.com/countnazgul/5330216
标签: javascript json node.js amazon-s3 gzip