How to delete image on Aws s3 with Node and Express

967 Views Asked by At

I am working on Nodejs and Expressjs applications. I want to delete an image on Aws-s3 image which I uploaded with multer-s3.

I have tried so many examples that I saw online but none of them worked.

I tried this example but it didn't work:

  router.delete("/:id/delete", async (req, res) => {
     const params = {
       Bucket: bucketname,
     Key:
       "https://nodeilders.s3.us-east- 
      2.amazonaws.com/public/uploads/programImages/church4%20%281%29.jpeg",
   };

  s3.deleteObject(params, (error, data) => {
    if (error) {
     res.status(500).send(error);
   } else {
     console.log("File has been deleted successfully");
   }
 });
});

and I also tried this example but it didn't work.

router.delete("/:id/delete", async (req, res) => {
const s3delete = function (params) {
return new Promise((resolve, reject) => {
    s3.createBucket({
        Bucket: BUCKET_NAME        /* Put your bucket name */
    }, function () {
        s3.deleteObject(params, function (err, data) {
            if (err) console.log(err);
            else
                console.log(
                    "Successfully deleted file from bucket";
                );
            console.log(data);
        });
    });
});
};
});

The first example logged that the file was successfully deleted but it was not deleted.

What can I try to resolve this?

0

There are 0 best solutions below