简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM