Reputation: 6841
I have seen How to delete or purge old files on S3?, however, this does not fit for me as I have a public (Unauthenticated Ident pool) that is allowed to upload to my bucket. as this is public I don't trust the uploader to set the expiry,
I have seen https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#listobjects but this does not seem to show that there is a created, added, uploaded or updated time on the objects. so I'm unsure how I can integrate through the object check if they are more than x time old then delete them. the unauthed user can only upload they can't see/edit or delete objects but I can't trust any form of information from them so does S3 have an added time of some form I can access in PHP and if so how?
Upvotes: 3
Views: 1633
Reputation: 6841
I found out there is a LastUpdated Time it's called LastModified
i found this with
$iterator = $this->client->getIterator('ListObjects', array(
'Bucket' => $bucketName
));
foreach($iterator as $object){
var_dump($object); die();
}
Which means I can do the following code to evaluate the time between uploaded and now for this example, I set x to 24 hours ago.
$iterator = $this->client->getIterator('ListObjects', array(
'Bucket' => $bucketName
));
$xtime = strtotime("now -24 hours");
foreach($iterator as $object){
$uploaded = strtotime($object["LastModified"]->date);
if($uploaded < $xtime){
$this->client->deleteObject(array(
"Bucket" => $bucketName,
"Key" => $object["Key"]
));
}
}
Upvotes: 4