Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a file image on amazon s3 when

Tags:

I have a program Model, and i the program has an image attribute which I use multers3 to upload when creating the Program.

The challenge that I am facing now is that, when I delete the program, everything gets deleted on my local machine but I realized that the file(image) still exists on my Aws s3 console. How do I get the file deleted both on my database and on Amazon s3?

Here are my Program routes

This is how I delete my Program

router.delete("/:id/delete", function (req, res) {
  const ObjectId = mongoose.Types.ObjectId;
  let query = { _id: new ObjectId(req.params.id) };

   Program.deleteOne(query, function (err) {
    if (err) {
       console.log(err);
     }
    res.send("Success");
  });
});

and this is how i creates my program.

  router.post("/create", upload.single("cover"), async (req, res, next) => {
    const fileName = req.file != null ? req.file.filename : null;
    const program = new Program({
      programtype: req.body.programtype,
      title: req.body.title,
      description: req.body.description,
      programImage: req.file.location,
   });
 try {
   console.log(program);
   const programs = await program.save();
   res.redirect("/programs");
  } catch {
   if (program.programImage != null) {
    removeprogramImage(program.programImage);
  }
  res.render("programs/new");
 }
});
like image 310
Chukwuma Kingsley Avatar asked Dec 03 '20 11:12

Chukwuma Kingsley


People also ask

How do I delete photos from AWS S3?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Bucket name list, choose the name of the bucket that you want to delete an object from. To delete an object in a versioning-enabled bucket with versioning: Off, Amazon S3 creates a delete marker.

Can you delete files from S3?

You can delete one or more objects directly from Amazon S3 using the Amazon S3 console, AWS SDKs, AWS Command Line Interface (AWS CLI), or REST API. Because all objects in your S3 bucket incur storage costs, you should delete objects that you no longer need.

What happens to an object when we delete it from Amazon S3?

What happens to an object when we Delete it from Amazon S3? If the version ID maps to a specific object version, Amazon S3 deletes the specific version of the object. If the version ID maps to the delete marker of that object, Amazon S3 deletes the delete marker. This makes the object reappear in your bucket.


1 Answers

Looking through the Multer-s3 repo, I can't find anything which mentions deleting from s3. There is this function in the source code, but, I can't figure out how to use it.

You could try using the AWS SDK directly via deleteObject:

const s3 = new aws.S3({
    accessKeyId: 'access-key-id',
    secretAccessKey: 'access-key',
    Bucket: 'bucket-name',
});

s3.deleteObject({ Bucket: 'bucket-name', Key: 'image.jpg' }, (err, data) => {
    console.error(err);
    console.log(data);
});
like image 185
Daniel_Knights Avatar answered Sep 30 '22 17:09

Daniel_Knights