//----- DELETE OLD S3 FILES -----
  try
  {
    
    //----- SETUP AWS SDK FOR S3 ACCESS -----
    $sdk = new Aws\Sdk([
        'version' => 'latest',
        'region'  => 'us-west-2',
        'credentials' => [
            'key'    => AWS_S3_ACCESS_KEY_ID,				//<<<<< S3 ACCESS KEY ID
            'secret' => AWS_S3_ACCESS_SECRET_KEY,		//<<<<< S3 SECRET ACCESS KEY
        ],
    ]);
    $s3Client = $sdk->createS3();
    
    //----------------------------------------------
    //----- DELETE ANY OLD FILES IN THE BUCKET -----
    //----------------------------------------------
    $objects = $s3Client->getIterator('ListObjects', array(
        "Bucket" => AWS_S3_BUCKET_NAME		//<<<<S3 BUCKET NAME
    ));
    foreach ($objects as $object)
    {
      $filename =  $object['Key'];
      $LastModified =  $object['LastModified'];

      //Work out how old the file is
      $end = strtotime(date("Y-m-d H:i:s"));		//Now
      $start = strtotime($LastModified);
      if ($start > $end)
      {
        $FileDaysOld = 0;
      }
      else
      {
        $FileDaysOld = floor(abs($end - $start) / 86400);   //ceil rounds the amount of days up to the next full day. Use floor if you want to get the amount of full days between the two dates.
      }

      if ($FileDaysOld > 30)			//<<<< DELETE FILES OLDER THAN
      {
		
      }
    }	//foreach ($objects as $object)

    }
    catch (Exception $e)
    {
		
    }
UK Electronic Designer
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.