Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

654
Visualizações
File is missing when trying to resize in Sharp in AWS Lambda function

I am sincerely puzzled by this. I am simply trying to invoke a Lambda function from within a Node JS application and perform image resizing on it. The invocation succeeds and the function is called (I can pass parameters to it and see them returned), however when I attempt to use Sharp for image resizing, I get the strange error that the input file is missing. It's not, as I can definitively confirm that the buffer is indeed passed to the Lambda.

I can confirm that using Sharp locally works. In the invocation I am JSON stringifying the payload, and I can confirm that the parameters are indeed passed to and are received by the Lambda. I am mapping them correctly from the first 'event' argument in the invocation. However the inclusion of Sharp within the Lambda just simply breaks.

I have created a new 'Sharp' project and have installed the node modules. These are packaged with the Lambda's index function into an archive and this is uploaded into the zip. The one thing that is quite different is that in order to use Sharp inside of Lambda, I had to install the linux version.

npm install --arch=x64 --platform=linux sharp

I have a test script in the Lambda where I have copied over a complete payload including a proper buffer. Both the test script and my local machine throw the same error:

{
  "errorType": "Error",
  "errorMessage": "Input file is missing",
  "trace": [
    "Error: Input file is missing"
  ]
}

This is my Lambda function.

const sharp = require('sharp');

exports.handler = async (event) => {
  return await sharp(event.buffer)
    .resize({
      width: event.maxWidth,
      height: event.maxHeight,
      fit: 'inside',
      withoutEnlargement: true
    })
    .jpeg({
      quality: 80
    })
    .toBuffer();
}

And this is my invocation.

const thumbImage = await lambda.invoke({
   FunctionName: 'sharp-resize',
   Payload: JSON.stringify({ buffer: file.buffer, maxWidth, maxHeight })
}).promise();

Any ideas or assistance would be appreciated.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I finally figured this out, and it really was such a simple thing. When chaining the buffer, Lambda needed to recreate the buffer from the event.buffer property. When the function returned the resized image, I chained it again before recreating the buffer on receipt. Then it was a simple matter of posting the new buffer to S3.

My final lambda:

 const sharp = require('sharp'); exports.handler = async (event) => { let buffer = Buffer.from(event.buffer.data); buffer = await sharp(Buffer.from(buffer)) .resize({ width: event.maxWidth, height: event.maxHeight, fit: 'inside', withoutEnlargement: true }) .jpeg({ quality: 80 }) .toBuffer(); } return { statusCode: 200, headers: { "Access-Control-Allow-Origin": "*" }, buffer: JSON.stringify(buffer) }; }

And my invocation and post to S3:

 const thumbImage = await lambda.invoke({ FunctionName: 'sharp-resize', Payload: JSON.stringify({ extension, buffer: file.buffer, maxWidth, maxHeight }) }).promise(); const payload = JSON.parse(thumbImage.Payload); const buffer = JSON.parse(payload.buffer); const parsedBuffer = Buffer.from(buffer.data); response.thumbImage = await uploadToS3({ key: amazonS3ThumbKey, file: parsedBuffer });
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda