Shaokan
Shaokan

Reputation: 7694

amazon s3 deleteObjects nodejs - can't get it working

I am using nodejs and trying to delete multiple objects at a time. But for some reason, despite not returning any error, the operation does not work as expected (the files are not being deleted). Here is the code:

s3.deleteObjects({
    Bucket: 'myprivatebucket/some/subfolders',
    Delete: {
        Objects: [
             { Key: 'nameofthefile1.extension' },
             { Key: 'nameofthefile2.extension' },
             { Key: 'nameofthefile3.extension' }
        ]
    }
}, function(err, data) {
  
    if (err)
        return console.log(err);
    
    console.log('success');

});

If I try to iterate over the files, and use the s3.deleteObject method then it works pretty good.

I also tried to specify to bucket without its subfolders (like 'myprivatebucket') but I got no result again.

How can I make this work? I am using node version: 0.10.32 and the aws should be 2.0.17.

Upvotes: 10

Views: 6152

Answers (1)

Shaokan
Shaokan

Reputation: 7694

Well finally I've resolved the problem.

When inserting the files, I was including the so-called sub-folders into the bucket name. For example:

{ Bucket: 'myprivatebucket/some/subfolders', Key: 'nameofthefile1.extension' }

This is apparently wrong and should be avoided. The correct use case is as follows:

{ Bucket: 'myprivatebucket', Key: 'some/subfolders/nameofthefile1.extension' }

After inserting the items like this, just use the same bucket and keys to delete objects and it will work! At least, for me it worked!

Upvotes: 16

Related Questions