3

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?

Barkermn01
  • 6,781
  • 33
  • 83
  • 1
    [ServerFault](https://serverfault.com/a/874594/73155) has a solution for this in PHP as well as bash. – GolezTrol Mar 30 '18 at 11:37
  • This would be great if it was not going on to a production public endpoint of an API server there is no way I would put a shell command on a public endpoint I'm afraid :) i was hoping for a purely PHP implementation – Barkermn01 Mar 30 '18 at 11:50
  • 2
    Side-note: You can use [Object Lifecycle Management](https://docs.aws.amazon.com/AmazonS3/latest/dev/object-lifecycle-mgmt.html) to automatically delete objects _N_ days after creation. – John Rotenstein Mar 30 '18 at 21:45

1 Answers1

4

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"]
        ));
    }
}
Barkermn01
  • 6,781
  • 33
  • 83
  • This does perform a http request for each file, right? This is not batch deletion? – Miguel Stevens Dec 07 '20 at 15:06
  • yeah thats what the `$this->client->deleteObject` call is doing in the loop so for each item – Barkermn01 Dec 07 '20 at 16:27
  • what to do when we have a huge number of files inside the bucket? – Er.KT Apr 11 '22 at 06:40
  • @Er.KT the same thing it's using an obtained iterator so it will automatically handle paging and such for you, just put in some output so you can see how it's doing. – Barkermn01 Apr 11 '22 at 16:09
  • @Barkermn01 that's right but it will not take a longer time to complete the operation? – Er.KT Apr 12 '22 at 11:25
  • @Er.KT yes always, it does not matter how you do it it will always take longer to delete more files, really you can only just leave it to run, or you put it in a cron script that runs every X amount of time so it cleaning up more often, thus reducing the total files when it does run – Barkermn01 Apr 12 '22 at 23:39
  • 1
    @Barkermn01 so now I stored file keys in my local DB, and I run cron on my table only so now deleting and downloading both are very easy. just need to pass the key and that's it – Er.KT May 10 '22 at 07:51