0

适用于 aws-sdk-js v2:

const Sharp = require('sharp');
const AWS = require('aws-sdk');

const S3 = new AWS.S3({ signatureVersion: 'v4' });

const originalPhoto = (await S3.getObject({ Bucket: "myBucket", Key: "myKey" }).promise()).Body;
const originalPhotoDimensions = await Sharp(originalPhoto).metadata();

在此处输入图像描述

aws-sdk-js v3 出错:我知道那是因为originalPhoto它不是缓冲区。

const Sharp = require('sharp');
const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3");

const S3 = new S3Client({ region: "us-east-1" });

const originalPhoto = (await S3.send(new GetObjectCommand({ Bucket: "myBucket", Key: "myKey" }))).Body;
const originalPhotoDimensions = await Sharp(originalPhoto).metadata(); // <--- Error

在此处输入图像描述

错误:输入文件丢失

ERROR   Unhandled Promise Rejection     
{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: Input file is missing",

夏普版本: 0.28.1

4

1 回答 1

0

尝试这个:

// See https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/migrating-to-v3.html
// npm init
// npm install sharp @aws-sdk/client-s3
// node s3.js

const Sharp = require('sharp');

const { S3Client, GetBucketTaggingCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
const { fromIni } = require("@aws-sdk/credential-provider-ini");

const s3 = new S3Client({
  credentials: fromIni({
    profile: 'default'
  })
});


bucket = 'nameOfBucket';
key = 'imageName';

const run = async () => {
  try {
    const photo = await s3.send(new GetObjectCommand({Bucket: bucket, Key: key})).Body;
    const originalPhotoDimensions = await Sharp(photo).metadata();
  } catch (err) {
    console.log("Error", err);
  }
};

run();
于 2021-04-15T02:09:40.283 回答