Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript aws-sdk S3 deleteObject(s) succedes but doesn't actually delete anything

In the MEAN.js app I'm building I upload images to AWS S3. I am trying to use the AWS SDK to delete unwanted images from the site but after a successful ajax call the file remains on S3.

I have required the AWS SDK like so, it works both with and without the config variables (as it should):

var aws = require('aws-sdk');
aws.config.update({accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY});

For my route I have the following code:

router.post('/delete', auth, function(req,res, next){
if(req.body.key) {
    var s3 = new aws.S3();
    var params = {
        Bucket: 'bucket name',
        Key: req.body.key
    };
    s3.deleteObject(params, function (err, data) {
        if (err) {
            console.log(err, err.stack);
            return next(err);
        }
        console.log(data);
        res.end('done');

I get a 200 response and {} is logged to the console but the file is not deleted from storage. I've also tried using the deleteObjects method like so:

var params = {
        Bucket: 'bucket name',
        Delete: {
            Objects: [
                {
                    Key: req.body.key
                }
            ]
        }

    };
    s3.deleteObjects(params, function (err, data) {
        if (err) {
            console.log(err, err.stack);
            return next(err);
        }
        console.log(data);
        res.end('done');

When I use deleteObjects I get { Deleted: [ { Key: 'file name' } ], Errors: [] } as a response but the file is still on S3.

Am I doing something wrong? I thought I followed the documentation to a T.

Also, issue occurs wether or not versioning is enabled on the bucket. With versioning enabled my response is:

{ Deleted: 
[ { Key: 'file name',
   DeleteMarker: true,
   DeleteMarkerVersionId: 'long id' } ],
Errors: [] }
like image 403
Kevin Avatar asked Jan 27 '16 21:01

Kevin


1 Answers

Try this one. You need to use promise() to ensure that the object is deleted before ending the execution. 6 hours waiting just for a simple object deletion is not normal, even with considering S3 99.999999999% durability.

var params = {
        Bucket : bucket,
        Key : video
};
try {
    await s3.deleteObject(params,function(err,data){
        if (err)    console.log(err,err.stack);
        else        console.log("Response:",data);
    }).promise();
} catch (e) {}
like image 142
ali Avatar answered Oct 20 '22 02:10

ali