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

419
Visualizações
How to catch an error when using S3.getsignedurlpromise() in node.js

I have a node.js server that downloads images from my s3 bucket and serves the images to my React client. I want to manage the case when the wrong file key is sent to the S3 client. In order to do so, I am intentionally sending a wrong key from React. I am expecting to receive an error that I could catch and render my view accordingly. Unfortunately, the s3 API is not returning an error, so all the points in my code where I intend to catch the error are being passed and I cant render a view when I get the image and another view when I get an error.

My code looks like so:

//s3Connect.js

download:  async (fileKey)=>{

        const downloadParams={
            Key:fileKey, //this is a wrong key like 123 or anything
            Bucket:bucketName
        }
        const data =   s3.getSignedUrlPromise('getObject', downloadParams);
        return data

    }

//adminPanel.js
//I call the above function below

getBackgroundCheck:async (req,res)=>{
        const readStream = await s3.download(req.params.key).then(url=> {
            console.log(url)
            res.send({url})
        })
            .catch(error=>{
                console.log(error)
                res.send('the error is',error) //since I sent a wrong key I expect to catch an error here
            } )
       

        }

Now in the client side. I use React and the fetch method to retrieve the image

const getBackgroundFile = async (e) => {

    try {
        e.preventDefault()
        const response = await fetch(`http://localhost:3003/adminPanel/getbackgroundcheck/${id}`)
        console.log('this is the response ',response)
        const parseResponse = await response.json()
       
        console.log('this is the parseResponse', parseResponse)
        setImage(parseResponse)
        setShowBackImage(true)
    } 
    catch (error) {
        console.log('this is the error',error) //again I expect to see this in console
    }
}

Finally: What do I get with the console.logs from the above function

this is the response  Response {type: 'cors', url: 'http://localhost:3003/adminPanel/getbackgroundcheck/nll%C3%B1', redirected: false, status: 200, ok: true, …}

So as you can see I get a 200 status. So how can I manage an error if I get a 200 ok status when I know that the response is failed because my server could not find the image in the s3 bucket and serve it to my client.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Creating a pre-signed URL is an operation that cannot fail, in normal operation. There's no actual API call behind the creation of a pre-signed URL - it's a simple local signing exercise in the SDK. It will happily generate pre-signed URLs for objects that don't exist, for example.

If you wan't to detect vending URLs for non-existent S3 objects, you'll have to do that some other way. For example, by proxying the downloads or pushing responsibility to the client.

about 4 years ago · Juan Pablo Isaza 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