Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete image from Amazon S3 Storage

I am trying to delete an image from Amazon S3 Storage, but it doesn't delete.

I am using following code:

var s3 = AWS.S3(awsCredentials);
s3.deleteObject({
  Bucket: MY_BUCKET,
  Key: myKey
},function (err,data){})

but images that I want to delete still persist.

I even used s3.deleteObjects function,it returns deleted object names, but when I checked the storage, the images were still there.

like image 330
Vladimir Gabrielyan Avatar asked Jun 28 '16 18:06

Vladimir Gabrielyan


2 Answers

You have to use the key in this way and not just the name of the file that is to be deleted then it will work.

var s3 = AWS.S3(awsCredentials);
s3.deleteObject({
  Bucket: MY_BUCKET,
  Key: 'some/subfolders/nameofthefile1.extension'
},function (err,data){})
like image 170
error2007s Avatar answered Sep 22 '22 12:09

error2007s


when you send the key dynamically, use local storage to store the key and send it dynamically to delete image

var s3 = AWS.S3(awsCredentials); 
s3.deleteObject({  
 Bucket: MY_BUCKET, 
 Key: 'yourFileKey' 
},function (err,data){})
like image 27
Raghul SK Avatar answered Sep 18 '22 12:09

Raghul SK