delete oldest object from S3

Go To StackoverFlow.com

1

I have a script that does a backup of my system every day. In the end I get 3 zip files each day and they get stored to S3. What I'd like to do is always keep a week or 10 days worth of these backups. After the 10th day, delete the oldest 3 zip files from S3. Any ideas on how I could tackle this. I don't see a way I could query the date modified to find the oldest.

I'm using the S3 PHP SDK. For reference, here is what I do to create the objects.

<?php
require_once 'AWSSDKforPHP/sdk.class.php';
define('BACKUPDIR','/backups/');

$backup1="backup1_".time().".zip";
$backup2="backup2_".time().".zip";
$backup3="backup3_".time().".zip";

$s3 = new AmazonS3();
$s3->create_object('mybucket', 'backups/'.$backup1, array(
'fileUpload' => BACKUPDIR.$backup1,
'acl' => $s3::ACL_PRIVATE
));

$s3->create_object('mybucket', 'backups/'.$backup2, array(
'fileUpload' => BACKUPDIR.$backup2,
'acl' => $s3::ACL_PRIVATE
));

$s3->create_object('mybucket', 'backups/'.$backup3, array(
'fileUpload' => BACKUPDIR.$backup3,
'acl' => $s3::ACL_PRIVATE
));
?>
2012-04-04 18:04
by Tom


0

Use a GET Bucket (list objects) API call, as documented here: http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketGET.html

This returns a list of all items in the bucket along with some meta data for every item, including the date on which the item was last modified. You can then use PHP to figure out which of these files you want to delete and use a DELETE Object API call to delete them :)

2012-04-04 18:28
by Daan


1

Using list_object requires a lot more work to parse so instead I used object expiration. Nothing to do here other than to let S3 handle it. All I have to do is add the expiry:

$s3->create_object('mybucket', $backup1, array(
'fileUpload' => BACKUPDIR.$backup1,
'acl' => $s3::ACL_PRIVATE,
'headers' => array(
    'Expires' => gmdate(CFUtilities::DATE_FORMAT_RFC2616, strtotime('+10 days'))
)
));

Now S3 will clean this up automatically after 10 days. Perfect for me when handling backup files.

2012-04-09 03:03
by Tom
Ads