Rajath
Rajath

Reputation: 2980

How to delete the only files (image) located inside the folder

I want to delete the files which is located inside myfolder folder in Amazon S3.

I don't want to delete the folder

This is the code which I have already tried but it is not working.

async function deleteFiles(req, res, next) {
    const folderName = req.params.imagesFolder;

    const s3 = new AWS.S3();

    const params = {
        Bucket: config.s3bucket,
        Delete: { 
            Objects: [ 
              {
                Key: `${folderName}/*`
              }
            ],
          },
    }


    console.log(`Params : ${params}`);

    try {
        await s3.headObject(params).promise()
        console.log("File Found in S3")
        try {
            await s3.deleteObject(params).promise()
            console.log("files deleted Successfully");
            next();
        }
        catch (err) {
            console.log("ERROR in file Deleting : " + JSON.stringify(err));
            res.status.JSON({success:0, message: `ERROR in file Deleting`, error: JSON.stringify(err)});
        }
    } catch (err) {
        console.log("File not Found ERROR : " + err.code);
        res.status(400).json({success:0, message: `File not found`, error: JSON.stringify(err)});
    }
}

I'm getting this console error:

File not Found ERROR : MultipleValidationErrors

But the file is present. Even it have image files 3

response from the server

{
    "success": 0,
    "message": "File not found",
    "error": "{\"message\":\"There were 2 validation errors:\\n* MissingRequiredParameter: Missing required key 'Key' in params\\n* UnexpectedParameter: Unexpected key 'Delete' found in params\",\"code\":\"MultipleValidationErrors\",\"errors\":[{\"message\":\"Missing required key 'Key' in params\",\"code\":\"MissingRequiredParameter\",\"time\":\"2019-04-01T09:04:22.640Z\"},{\"message\":\"Unexpected key 'Delete' found in params\",\"code\":\"UnexpectedParameter\",\"time\":\"2019-04-01T09:04:22.641Z\"}],\"time\":\"2019-04-01T09:04:22.642Z\"}"
}

Any kind help will much appreciated.

Upvotes: 0

Views: 629

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 270154

Folders do not exist in Amazon S3. Sort of.

For example, you could create an object with a Key of:

folder1/folder2/foo.txt

This would instantly "create" folder1 and folder2. However, if you were then to delete foo.txt, the folders would disappear. This is because the folders do not actually exist — the system just makes them 'appear to exist'. They are officially called common prefixes rather than folders.

You can create a folder by creating a zero-length object with the same name as the folder. This will cause the folder to "appear" even if there are no objects "in" the folder. But, in general, this isn't necessary. There is very little reason to require an empty folder.

Upvotes: 1

First thing there is no folder in your S3 bucket it's just a prefix for your files.

try like this :

  var params = {
    Bucket: 'bucketName',
    Prefix: 'myfolder/'
  };

  s3.listObjects(params, function(err, data) {
    if (err) return  console.log(err);

    params = {Bucket: bucketName};
    params.Delete = {Objects:[]};

    data.Contents.forEach(function(content) {
      params.Delete.Objects.push({Key: content.Key});
    });

    s3.deleteObjects(params, function(err, data) {
      if (err) console.log(err);
      else console.log('well done!');
    });
  });

Upvotes: 1

Related Questions