Home > Net >  Delete an S3 "folder" using Perl
Delete an S3 "folder" using Perl

Time:12-19

I'm trying to use Net::Amazon::S3 to manipulate an S3 storage bucket for backup rotation. One thing I need to do is delete folders that contain old backups. But, while I can delete files, I cannot get the code to delete even an empty folder.

For example:

our $s3 = Net::Amazon::S3->new({
    aws_access_key_id  => 'key_id',
    aws_secret_access_key => 'access_key',
    retry => 1
});

our $bucket_name = 'bucketName';
our $s3Bucket = $s3->bucket($bucket_name);

# Assume bucket 'bucketName' has the following key. This works fine:
$s3Bucket->delete_key('weeklyTest/test.pdf') or say STDERR 'File delete failed:'  . $s3Bucket->errstr;

# Assume bucket 'bucketName' has the following empty key. 
# This fails to happen, but without reporting any error.
$s3Bucket->delete_key('weeklyTest/') or say STDERR 'Folder delete failed:'  . $s3Bucket->errstr;

Is there something I'm missing that needs to be done to get a "folder" key to delete?

CodePudding user response:

All libraries for AWS services closely track with aws-cli. If you can get things to work with aws-cli, then the process for doing the same programmatically will be similar. I usually do my prototyping with aws-cli to get the syntax/options right, then repeat in code.

delete_key is used for objects is a bucket, but if you want to delete the bucket itself, you need to use delete_bucket.

https://metacpan.org/pod/Amazon::S3#delete_bucket

  • Related