适用于 Javascript v3 S3Client GetObjectCommand 的 AWS SDK 给出错误:文件意外结束

编程入门 行业动态 更新时间:2024-10-06 08:23:10

<a href=https://www.elefans.com/category/jswz/34/1770972.html style=适用于 Javascript v3 S3Client GetObjectCommand 的 AWS SDK 给出错误:文件意外结束"/>

适用于 Javascript v3 S3Client GetObjectCommand 的 AWS SDK 给出错误:文件意外结束

在我的 AWS Lambda 函数中,我有这段代码将 S3 存储桶中的 zip 文件的内容提取到本地文件系统中。它使用 nodejs 流来避免首先下载整个 zip 文件,以节省内存:

const streamUnzipFromS3ToLocal = async (
  objectBucket: string,
  objectKey: string,
  extractDir: string,
) => {
  logger.info(`Start to stream unzip ${objectBucket}:${objectKey} to local.`);
  var start = new Date().getTime();
  try {
    const data = await S3.getFile(objectBucket, objectKey); // data.Body is a readable stream;
    data.Body.on('error', (error) => {
      logger.error(`Error while reading the stream: ${error}`);
      console.log(error);
      throw error;
    });
    await pipeline(data.Body, unzipper.Extract({ path: extractDir }));
    logger.info(`${objectKey} is decompressed to ${extractDir}`);
  } catch (error) {
    logger.error(`Failed to stream unzip file ${objectKey}: ${error}`);
    console.log(error);
    throw error
  }
  var end = new Date().getTime();
  var time = (end - start) / 1000;
  logger.info(`Finished stream-unzipping ${objectKey} to local. execution time: ${time} sec;`);
  return time;
};

S3.getFile
如下:

export const getFile = async (bucket_name, object_key) => {
  let res;
  const params = {
    Bucket: bucket_name,
    Key: object_key,
  };

  try {
    const x = new GetObjectCommand(params);
    const data = await s3Client.send(new GetObjectCommand(params));
    res = data;
  } catch (err) {
    logger.error(`Failed when s3Controller getFile: ${JSON.stringify(err)}`);
    throw err;
  }

  return res;
};

使用上述代码在 S3 存储桶中流解压缩一个非常大的文件时出现此错误:

Failed to stream unzip file myapp/a/s/d/2GBfile.zip: Error: unexpected end of file

Error while reading the stream: Error: aborted

怎么回事?

我该如何解决这个问题?

回答如下:

更多推荐

适用于 Javascript v3 S3Client GetObjectCommand 的 AWS SDK 给出错误:文件意外结束

本文发布于:2024-05-30 12:06:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770480.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   意外   错误   结束   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!