Node.js:使用 Sharp 操作来自 S3 的图像

问题描述 投票:0回答:1

我在 Amazon S3 中存储了图像。图像类型包括 jpg、png 和 svg。我有一个打字稿节点后端,我应该从 S3 存储桶中检索图像以使用 Sharp 库进行旋转。我正在使用 aws-sdk 来获取该对象,但我无法理解我可以对 S3 对象执行哪些操作以将其转换为可与 Sharp 一起使用的形式。

const getRequest: S3.GetObjectRequest = {
    Bucket: config.aws.bucketName,
    Key: uuid
};
  
const response = await s3client.getObject(getRequest).promise();
const macigalForm = response.Body as ???; // From documentation: Body = Readable | ReadableStream | Blob

sharp(magicalForm); // Accepts: Buffer  | Uint8Array  | Uint8ClampedArray  | Int8Array  | 
// Uint16Array  | Int16Array  | Uint32Array  | Int32Array  | Float32Array  | Float64Array  | string 

我需要帮助来理解“magicalForm”输入锐利输入的最佳类型。

node.js typescript amazon-s3 aws-sdk sharp
1个回答
0
投票

有同样的问题。您可以使用响应中的transformToByteArray方法将其转换为Uint8Array,但由于您使用的是sdk3,因此您需要更新使用getObject的方式(现在是GetObjectCommand)。下面的代码应该可以工作。我有类似的按需图像调整器。

    const getRequest = {
        Bucket: config.aws.bucketName,
        Key: uuid
    };

    const command = new S3.GetObjectCommand(getRequest);
      
    const response = await s3client.send(command);
    const body = await response.Body.transformToByteArray();

    sharp(body);

© www.soinside.com 2019 - 2024. All rights reserved.