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��...
I figured out just to use zlib.unzip
and use util.promisify
to return the final value as a promise. If anyone knows any better solution (with pako
maybe), please share, thank you!
import { Buffer } from 'buffer';
import zlib from "react-zlib-js";
import util from "util";
const getLog = async (itemName) => {
const response = await fetch(
"url?" +
new URLSearchParams({
some_param: "some_value",
})
);
if (!response.ok) {
throw new Error("Fail ....!");
}
const responseJson = await response.json();
const buffer = Buffer.from(responseJson.data, "base64");
const responseData = (await util.promisify(zlib.unzip)(buffer)).toString();
return responseData;
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.