4

I'm trying to delete an object from S3 and I can't make it work.

This is what I'm doing:

const AWS = require('aws-sdk');
const s3 = new AWS.S3({
  accessKeyId: ID, //My accessKeyId
  secretAccessKey: SECRET //My secretAccessKey
});
var params = {  
    Bucket: process.env.S3_BUCKET_NAME, //'myBucket'
    Key: file //'places-images/06850015-3d55-427b-a2f3-b8c2a56a42d8madametussauds.jpg'
  }
s3.deleteObject(params, (err, data) => {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
  })

The data object returned is always empty and the file does not go away from S3. This bucket is not versioned btw.

I tried the examples from here and AWS docs but don't seem to work. It's driving me crazy because it seems pretty straighforward! Any help will be greatly appretiated!

  • Take a look at my answer here: https://stackoverflow.com/a/58713546/9931092 – Amit Baranes Dec 04 '19 at 16:51
  • 2
    Your code has no glaring issues that I can see. How are you detecting that the object has *not* been deleted? Also, any chance that you are supplying the wrong bucket name or the wrong key? That would yield a successful response, but nothing would be deleted. – jarmod Dec 04 '19 at 17:00
  • Hi @jarmod, the bucket name is okay because it's the same I use to upload the files. To check if the object was deleted or not I go to AWS S3 and search for the file and they are still there after deleting. – nicolas mondada Dec 05 '19 at 14:27
  • Test this with the awscli. Use `aws s3 ls` to list the object, then `aws s3 rm` to delete the object, then list the object again. – jarmod Dec 05 '19 at 14:45
  • Did you ever figure out what was it? I'm facing the same issue and can't find what's wrong, I've tested almost every possible key value and still shows in S3 :( – Juan Carlos Mar 20 '22 at 18:39

1 Answers1

0

In my case the problem was that the key was escaped, so I just needed to make sure iT was written without the %20 (that replaced spaces).

INCORRECT

    const params = {
       Bucket: bucketName,
       Key: "55bca7-40a-7708-8457-04666cd45f16-Screen%20Shot%202022-05-18%20at%2017.13.49.png", //key,
  };

CORRRECT

const params = {
    Bucket: bucketName,
    Key: "55bca7-40a-7708-8457-04666cd45f16-Screen Shot 2022-05-18 at 17.13.49.png", //key,

};

I believe you also need to pass the region and signature version:

 const s3 = new aws.S3({
  region,
  accessKeyId,
  secretAccessKey,
  signatureVersion: "v4",
})