最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to resize image from S3 with Sharp? - Stack Overflow

programmeradmin9浏览0评论

I am trying to resize the uploaded image from S3 using @aws-sdk/v3 in Node.js API.

First, I get the object (image) from S3 as shown here: .ts

...

  const params = { Bucket: AWS_BUCKET_NAME, Key: key }
  const data = await s3.send(new GetObjectCommand(params))
  console.log(` >> Success, bucket returned:`, data) // Log correctly

Then I try to resize the data (image):

  const convert = sharp(data).resize(size, size).webp()

But it throws:

 >> data.type: object
(node:35583) UnhandledPromiseRejectionWarning: Error: Input file is missing
...

And I am not sure how should I upload it back?

What am I doing wrong?

I am trying to resize the uploaded image from S3 using @aws-sdk/v3 in Node.js API.

First, I get the object (image) from S3 as shown here: https://github./awsdocs/aws-doc-sdk-examples/blob/master/javascriptv3/example_code/s3/src/s3_getobject.ts

...

  const params = { Bucket: AWS_BUCKET_NAME, Key: key }
  const data = await s3.send(new GetObjectCommand(params))
  console.log(` >> Success, bucket returned:`, data) // Log correctly

Then I try to resize the data (image):

  const convert = sharp(data).resize(size, size).webp()

But it throws:

 >> data.type: object
(node:35583) UnhandledPromiseRejectionWarning: Error: Input file is missing
...

And I am not sure how should I upload it back?

What am I doing wrong?

Share Improve this question edited Feb 4, 2021 at 0:13 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jan 21, 2021 at 11:02 SwixSwix 2,12311 gold badges38 silver badges58 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

data.Body is of type Readable (a readable stream)

You can use a stream directly with sharp (see stream example on sharp readme)

In your case this gives:

  const params = { Bucket: AWS_BUCKET_NAME, Key: key }
  const data = await s3.send(new GetObjectCommand(params))

  const converter = await sharp().resize(size, size).webp()
  const resizedBody = (data.Body as Readable).pipe(converter);

I think the sharp constructor accepts a buffer, but what you're passing it is the entire response from the SDK request. Instead, try passing data.Body to sharp fn like this:

const convert = sharp(data.Body).resize(size, size).webp();

发布评论

评论列表(0)

  1. 暂无评论