如何在React中解压缩GZIP AWS Gateway API lambda响应?
我关注此答案并成功使用GZIP来压缩数据并避免AWS LAMBDA 6MB响应限制。但是,在接收到前端React应用程序中的响应后,我不知道如何解压缩并转换为字符串。我的文件是一个日志文件。
我试图解决:
// this is my “response.json()” will look like
const baseData = {
“data”: “H4sIAAAAA.....”
}
// decode the base64 encoded data
const gzipedData = Buffer.from(baseData.data, “base64");
const ungzip = async (input) => {
return new Promise((resolve, reject) =>
zlib.gzip(input, (err, data) => {
if (err) {
reject(err);
}
resolve(data);
})
);
};
// unzip and return a Buffer
const ungzipedData = await ungzip(gzipedData);
// convert Buffer to string
const buf = Buffer.from(ungzipedData, ‘utf8’);
console.log(buf.toString());
结果是这样:
g@����r��.{�/)fx^�R�d�J%��y�c��P��...
I followed this answer and successfully used gzip to compress the data and avoid AWS lambda 6MB response limitation. But I can't figure out how to decompress and convert to the string after the response is received in front end react app. My file is a log file.
I tried to solve:
// this is my “response.json()” will look like
const baseData = {
“data”: “H4sIAAAAA.....”
}
// decode the base64 encoded data
const gzipedData = Buffer.from(baseData.data, “base64");
const ungzip = async (input) => {
return new Promise((resolve, reject) =>
zlib.gzip(input, (err, data) => {
if (err) {
reject(err);
}
resolve(data);
})
);
};
// unzip and return a Buffer
const ungzipedData = await ungzip(gzipedData);
// convert Buffer to string
const buf = Buffer.from(ungzipedData, ‘utf8’);
console.log(buf.toString());
The result was something like this:
g@����r��.{�/)fx^�R�d�J%��y�c��P��...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想出的只是要使用
zlib.unzip
并使用util.promisify
以返回最终值作为承诺。如果有人知道任何更好的解决方案(也许使用
pako
),请分享,谢谢!I figured out just to use
zlib.unzip
and useutil.promisify
to return the final value as a promise.If anyone knows any better solution (with
pako
maybe), please share, thank you!