'Cannot delete image from aws s3 bucket

I want to delete one of the images stored in the AWS S3 bucket by express.js, but I am facing an issue. Below is the code I have written

const imageurl=req.user.imageurl.split("/");
  // console.log(imageurl);
  const imgfile=imageurl[imageurl.length-1]
  // imgfile[0] = "4a05123a-506f-4c22-896a-8b6595b95d66Cover.jpg"
  console.log(imgfile);
  const params ={
    Bucket: process.env.aws_bucket_name,
    Key: imgfile
  }
  s3.deleteObject(params, (error, data) => {
    if (error) {
      res.status(500).send(error);
    }
    console.log(data);
    res.status(200).send("File has been deleted successfully");
  });

Here, everything works fine but when I run this code and then look at my S3 bucket, the image is still present.



Solution 1:[1]

I found the problem that was causing an error. My 'imgfile' is an array that contains the string at the 0th index. I was passing it as imgfile instead of imgfile[0].

So sorry for the trouble. Thanks

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Subham Kumar