Skip to content Skip to sidebar Skip to footer

Erasing Aws/s3 Objects From Node.js (javascript)

I have a Node.JS app using mLab as a DB and also AWS/S3 to store files. I just implemented a functionality allowing the user to erase information from the mLab DB, this works fine.

Solution 1:

AWS provides an SDK for JavaScript. First, ensure that you have set your credentials in whatever way makes sense for you. Next install the sdk:

npm i aws-sdk

Deleting an object from a bucket:

constAWS = require('aws-sdk');
const s3 = newAWS.S3();

const params = {
  Bucket: 'examplebucket', 
  Key: 'objectkey.jpg'
};

s3.deleteObject(params, function(err, data) {
  if (err) {
    console.log(err, err.stack); // an error occurredelse {
    console.log(data);           // successful response
  }
});

A couple of notes:

  • There is also a deleteObjects function that can delete multiple objects with one call.
  • Optional but recommended, if you are using a recent version of node, you can use util.promisify to turn the callback style that the AWS sdk uses into promises.

Post a Comment for "Erasing Aws/s3 Objects From Node.js (javascript)"