AWS-SDK v3 - 使用 Typescript 下载文件

Ser*_*key 13 amazon-s3 amazon-web-services typescript aws-sdk

编写基于 Typescript 的 Lambda 以从 S3 存储桶下载文件进行处理,但遇到了 AWS SDK v3 的问题。

我收到错误““Readable | ReadableStream | Blob”类型上不存在属性“pipe”。“ReadableStream”类型上不存在属性“pipe”。”

import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";

const s3Client = new S3Client();        
const command = new GetObjectCommand({ Bucket: myBucket, Key: myFile });

const s3Item = await s3Client.send(command);
s3Item.Body.pipe(createWriteStream(fileName));
Run Code Online (Sandbox Code Playgroud)

已尝试使用 .pipeTo 和 .pipeThrough 但它们实际上报告了相同的错误。

关于为什么这不起作用或者如何通过 SDK v3 实现下载有什么想法吗?

我很难找到下载示例,甚至官方 AWS 文档也涵盖了上传和其他步骤,但没有涵盖下载。AWS S3 示例

eri*_*2k8 20

for 的类型Body相当笨拙Readable | ReadableStream | Blob | undefined。这是因为该库在节点和浏览器中都可用,但不幸的是只是将它们全部塞入一个定义中。

  • 如果使用节点,它将是Readable
  • 在现代浏览器中,它将是ReadableStream
  • 在较旧的浏览器中,它将是Blob

Body我还没有找到is的情况undefined,但在源代码中查看,它看起来像是底层 http 层的工件。到目前为止,我只是假设它已经定义了,这使得辅助函数的签名更加方便。然而,在某些情况下,假设我没有遇到过这种情况是不安全的。

因此,对于节点,您可以使用如下内容:

import { Readable } from 'stream';
import { GetObjectCommandOutput } from '@aws-sdk/client-s3';

export const asStream = (response: GetObjectCommandOutput) => {
  return response.Body as Readable;
};

export const asBuffer = async (response: GetObjectCommandOutput) => {
  const stream = asStream(response);
  const chunks: Buffer[] = [];
  return new Promise<Buffer>((resolve, reject) => {
    stream.on('data', (chunk) => chunks.push(chunk));
    stream.on('error', (err) => reject(err));
    stream.on('end', () => resolve(Buffer.concat(chunks)));
  });
};

export const asString = async (response: GetObjectCommandOutput) => {
  const buffer = await asBuffer(response);
  return buffer.toString();
};
Run Code Online (Sandbox Code Playgroud)

或者,如果您想更严格undefined

const getBody = (response: GetObjectCommandOutput) => {
  return response.Body && (response.Body as Readable);
};

const getBodyAsBuffer = async (response: GetObjectCommandOutput) => {
  const stream = getBody(response);
  if (stream) {
    const chunks: Buffer[] = [];
    return new Promise<Buffer>((resolve, reject) => {
      stream.on('data', (chunk) => chunks.push(chunk));
      stream.on('error', (err) => reject(err));
      stream.on('end', () => resolve(Buffer.concat(chunks)));
    });
  }
};

const getBodyAsString = async (response: GetObjectCommandOutput) => {
  const buffer = await getBodyAsBuffer(response);
  return buffer?.toString();
};
Run Code Online (Sandbox Code Playgroud)

在浏览器中,以下内容应该适用于现代浏览器,但如果您的应用程序支持 IE,则一定要注意此处的兼容性,因为这不处理旧版浏览器响应Blob: https: //developer.mozilla.org/en-US/文档/Web/API/Streams_API#browser_compatibility

import { GetObjectCommandOutput } from '@aws-sdk/client-s3';

export const asStream = (response: GetObjectCommandOutput) => {
  return response.Body as ReadableStream;
};

export const asBlob = async (response: GetObjectCommandOutput) => {
  return await new Response(asStream(response)).blob();
};

export const asString = async (response: GetObjectCommandOutput) => {
  return await new Response(asStream(response)).text();
};
Run Code Online (Sandbox Code Playgroud)

我认为从长远来看,@aws-sdk/lib-storage将需要一个Download类似于它的Upload,但到目前为止还没有听说它有任何积压。请参阅此 GitHub 问题以进行长时间的讨论:https://github.com/aws/aws-sdk-js-v3/issues/1877