{"id":3130,"date":"2020-08-07T11:28:05","date_gmt":"2020-08-07T10:28:05","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3130"},"modified":"2022-02-17T07:13:47","modified_gmt":"2022-02-17T07:13:47","slug":"list-s3-objects","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/platforms\/aws\/s3\/list-s3-objects","title":{"rendered":"List S3 Objects"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>  \/\/----- DELETE OLD S3 FILES -----\n  try\n  {\n    \n    \/\/----- SETUP AWS SDK FOR S3 ACCESS -----\n    $sdk = new Aws\\Sdk(&#91;\n        'version' => 'latest',\n        'region'  => 'us-west-2',\n        'credentials' => &#91;\n            'key'    => AWS_S3_ACCESS_KEY_ID,\t\t\t\t\/\/&lt;&lt;&lt;&lt;&lt; S3 ACCESS KEY ID\n            'secret' => AWS_S3_ACCESS_SECRET_KEY,\t\t\/\/&lt;&lt;&lt;&lt;&lt; S3 SECRET ACCESS KEY\n        ],\n    ]);\n    $s3Client = $sdk->createS3();\n    \n    \/\/----------------------------------------------\n    \/\/----- DELETE ANY OLD FILES IN THE BUCKET -----\n    \/\/----------------------------------------------\n    $objects = $s3Client->getIterator('ListObjects', array(\n        \"Bucket\" => AWS_S3_BUCKET_NAME\t\t\/\/&lt;&lt;&lt;&lt;S3 BUCKET NAME\n    ));\n    foreach ($objects as $object)\n    {\n      $filename =  $object&#91;'Key'];\n      $LastModified =  $object&#91;'LastModified'];\n\n      \/\/Work out how old the file is\n      $end = strtotime(date(\"Y-m-d H:i:s\"));\t\t\/\/Now\n      $start = strtotime($LastModified);\n      if ($start > $end)\n      {\n        $FileDaysOld = 0;\n      }\n      else\n      {\n        $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.\n      }\n\n      if ($FileDaysOld > 30)\t\t\t\/\/&lt;&lt;&lt;&lt; DELETE FILES OLDER THAN\n      {\n\t\t\n      }\n    }\t\/\/foreach ($objects as $object)\n\n    }\n    catch (Exception $e)\n    {\n\t\t\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-3130","post","type-post","status-publish","format-standard","hentry","category-s3"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3130"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3130\/revisions"}],"predecessor-version":[{"id":3131,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3130\/revisions\/3131"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}