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

594
Visualizações
How to use aws s3 image url in node js lambda?

I am trying to use aws s3 image in lambda node js but it throws an error 'no such file or directory'. But I have made that image as public and all permissions are granted.

fs = require('fs');
exports.handler = function( event, context ) {

         var img = fs.readFileSync('https://s3-us-west-2.amazonaws.com/php-7/pic_6.png');
         res.writeHead(200, {'Content-Type': 'image/png' });
         res.end(img, 'binary');
};
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

fs is node js file system core module. It is for writing and reading files on local machine. That is why it gives you that error.

over 4 years ago · Santiago Trujillo Relatório

0

There are multiple things wrong with your code.

fs is a core module used for file operations and can't be used to access S3.

You seem to be using express.js code in your example. In lambda, there is no built-in res defined(unless you define it yourself) that you can use to send response.

You need to use the methods on context or the new callback mechanism. The context methods are used on the older lambda node version(0.10.42). You should be using the newer node version(4.3.2 or 6.10) which return response using the callback parameter.

It seems like you are also using the API gateway, so assuming that, I'll give a few suggestions. If the client needs access to the S3 object, these are some of your options:

  • Read the image from S3 using the AWS sdk and return the image using the appropriate binary media type. AWS added support for binary data for API gateway recently. See this link OR
  • Send the public S3 URL to client in your json response. Consider whether the S3 objects need to be public. OR
  • Use the S3 sdk to generate pre-signed URLs that are valid for a configured duration back to the client.

I like the pre-signed URL approach. I think you should check that out. You might also want to check the AWS lambda documentation

over 4 years ago · Santiago Trujillo Relatório

0

To get a file from S3, you need to use the path that S3 give you. The base path is https://s3.amazonaws.com/{your-bucket-name}/{your-file-name}.

On your code, you must replace the next line:

var img = fs.readFileSync('https://s3.amazonaws.com/{your-bucket-name}/pic_6.png');

If don't have a bucket, you should to create one to give permissions.

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