{"id":1889,"date":"2018-10-26T18:57:48","date_gmt":"2018-10-26T18:57:48","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=1889"},"modified":"2022-02-17T07:14:02","modified_gmt":"2022-02-17T07:14:02","slug":"delete-object","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/platforms\/aws\/s3\/delete-object","title":{"rendered":"Delete S3 File\/Object"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Delete single file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/Create S3 connection\n\t$sdk = new Aws\\Sdk(&#91;\n\t\t\t'version' => 'latest',\n\t\t\t'region'  => 'eu-west-1',\t\t\t\t\/\/&lt;&lt;&lt;&lt;&lt; REGION\n\t\t\t'credentials' => &#91;\n\t\t\t\t\t'key'    => $AwsS3AccessKeyId,\t\t\t\t\/\/&lt;&lt;&lt;&lt;&lt; S3 ACCESS KEY ID\n\t\t\t\t\t'secret' => $AwsS3AccessSecretKey,\t\t\/\/&lt;&lt;&lt;&lt;&lt; S3 SECRET ACCESS KEY\n\t\t\t],\n\t]);\n\t$s3Client = $sdk->createS3();\n\n\ttry\n\t{\n\t\t$result = $s3Client->deleteObject(&#91;\n\t\t\t\t'Bucket' => $AwsS3BucketName,\n\t\t\t\t'Key' => $S3Filename\n\t\t]);\n\t}\n\tcatch (S3Exception $e)\n\t{\n\t\techo $e->getMessage();\n\t}\n\tcatch (Exception $e)\n\t{\n\t\techo $e->getMessage();\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete files based on date<\/h4>\n\n\n\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    \/\/If a file hasn't been imported then it didn't get added to the database properly so should be cleaned up at some point\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        \/\/----- DELETE FILE -----\n        $result = $s3Client->deleteObject(&#91;\n            'Bucket' => AWS_S3_BUCKET_NAME,\n            'Key' => $filename\n        ]);\t\t\n      }\n    }\t\/\/foreach ($objects as $object)\n\n    }\n    catch (Exception $e)\n    {\n        \t\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Delete single file Delete files based on date<\/p>\n","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-1889","post","type-post","status-publish","format-standard","hentry","category-s3"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1889","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=1889"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1889\/revisions"}],"predecessor-version":[{"id":3132,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1889\/revisions\/3132"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=1889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=1889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=1889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}