Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete folder on s3 with node.js?

Yes, I know. There is no folder concept on s3 storage. but I really want to delete a specific folder from s3 with node.js. I tried two solutions, but both didn't work. My code is below: Solution 1: Deleting folder directly.

var key='level/folder1/folder2/'; var strReturn;         var params = {Bucket: MyBucket};         var s3 = new AWS.S3(params);         s3.client.listObjects({             Bucket: MyBucket,             Key: key         }, function (err, data) {             if(err){                 strReturn="{\"status\":\"1\"}";              }else{                 strReturn=+"{\"status\":\"0\"}";             }             res.send(returnJson);             console.log('error:'+err+' data:'+JSON.stringify(data));         }); 

Actually, I have a lot of files under folder2. I can delete single file from folder2 if I define key like this: var key='level/folder1/folder2/file1.txt', but it didn't work when I deleted a folder(key='level/folder1/folder2/'). Solution 2: I tried to set expiration to an object when I uploaded this file or folder to s3. code is below:

s3.client.putObject({                 Bucket: Camera_Bucket,                 Key: key,                             ACL:'public-read',                  Expires: 60              } 

But it didn't either. After finishing uploading, I checked the properties of that file. it showed there was nothing value for expiry date:

Expiry Date:none Expiration Rule:N/A 

How can I delete folder on s3 with node.js?

like image 488
user3034559 Avatar asked Nov 26 '13 01:11

user3034559


People also ask

How do I delete a folder from my AWS S3?

In the Buckets list, choose the name of the bucket that you want to delete folders from. In the Objects list, select the check box next to the folders and objects that you want to delete. Choose Delete.

How do you delete a directory in node JS?

In a Node. js application, you can use the fs. rmdir() method to delete a directory. This method works asynchronously to remove the directory.

How do I delete items from my AWS S3?

To delete the object, select the object, and choose delete and confirm your choice by typing delete in the text field. On, Amazon S3 will permanently delete the object version. Select the object version that you want to delete, and choose delete and confirm your choice by typing permanently delete in the text field.


1 Answers

Here is an implementation in ES7 with an async function and using listObjectsV2 (the revised List Objects API):

async function emptyS3Directory(bucket, dir) {     const listParams = {         Bucket: bucket,         Prefix: dir     };      const listedObjects = await s3.listObjectsV2(listParams).promise();      if (listedObjects.Contents.length === 0) return;      const deleteParams = {         Bucket: bucket,         Delete: { Objects: [] }     };      listedObjects.Contents.forEach(({ Key }) => {         deleteParams.Delete.Objects.push({ Key });     });      await s3.deleteObjects(deleteParams).promise();      if (listedObjects.IsTruncated) await emptyS3Directory(bucket, dir); } 

To call it:

await emptyS3Directory(process.env.S3_BUCKET, 'images/') 
like image 151
Emi Avatar answered Oct 07 '22 00:10

Emi