Home > Mobile >  S3 bucket objects are not getting deleted by s3.deleteObject() method
S3 bucket objects are not getting deleted by s3.deleteObject() method

Time:04-09

I've used the s3.deleteObject() method to delete the uploaded file on AWS S3 bucket. But in response, I'm getting no error but still not deletion of that file. I thought it could be permission issue but, I'm not event getting permission error and this file is already uploaded by multerS3.

const aws = require('aws-sdk');

aws.config.update({
  accessKeyId: accessKeyId,
  secretAccessKey: secretAccessKey
});
const s3 = new aws.S3();

(async () => {
  let params = {
    Bucket: 'bucket',
    Key: 'https://bucket.s3.amazonaws.com/profiles/1648634140824-xyz.png'
  };

  console.log(`params ->> `, params);

  s3.deleteObject(params, function (error, data) {
    if (error) {
      console.log(`\nDeleteImageFromS3 error ->> ${error}`);
      return;
    } else {
      console.log('\ns3.deleteObject data ->> ', data);
      return;
    }
  });
})();

In response, I'm getting below log and file is still existing in S3 bucket

s3.deleteObject data ->>  { DeleteMarker: true,
  VersionId: 'DtD9ccuW2zl1ylWvQTrjJfk3yVa_Ze1A' }

Thank you advance

CodePudding user response:

Please change the key part, you wont have have to define the domain of aws s3 (all these things are handled by aws-sdk only), just the file name present inside that bucket.

 let params = {
    Bucket: 'bucket',
    Key: 'profiles/1648634140824-xyz.png'
  };
  • Related