如何在React中解压缩GZIP AWS Gateway API lambda响应?

发布于 2025-02-11 04:59:36 字数 872 浏览 0 评论 0原文

我关注此答案并成功使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2025-02-18 04:59:36

我想出的只是要使用zlib.unzip并使用util.promisify以返回最终值作为承诺。
如果有人知道任何更好的解决方案(也许使用pako),请分享,谢谢!

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;
    };

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;
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文