{"id":1776,"date":"2017-09-28T11:49:17","date_gmt":"2017-09-28T11:49:17","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=1776"},"modified":"2022-02-17T07:14:02","modified_gmt":"2022-02-17T07:14:02","slug":"s3-general","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/platforms\/aws\/s3\/s3-general","title":{"rendered":".S3 General"},"content":{"rendered":"<h4>\nGet AWS SDK<br \/>\n<\/h4>\n<h5>\nSimple include the SDK .phar method&nbsp;<br \/>\n<\/h5>\n<p>\nDownload the &quot;aws.phar&quot; file and store it in a subdirectory called, says awssdk.&nbsp; Then in your php files use:\n<\/p>\n<pre>\r\n<code>\r\nrequire &#39;awssdk\/aws.phar&#39;;\r\n<\/code><\/pre>\n<p>\nDefine your S3 access values ready to use in your code\n<\/p>\n<pre>\r\n<code>\r\n\t\t$AwsS3BucketName = &#39;&#39;;\t\t\t\t\/\/&lt;&lt;&lt;&lt;SET THIS\r\n\t\t$AwsS3AccessKeyId = &#39;&#39;;\t\t\t\t\/\/&lt;&lt;&lt;&lt;SET THIS\r\n\t\t$AwsS3AccessSecretKey = &#39;&#39;;\t\t\/\/&lt;&lt;&lt;&lt;SET THIS\r\n<\/code><\/pre>\n<p>\n(Remember if you define them globally then you need to define them as global variables in functions which use them)\n<\/p>\n<p>\nThese values are <a href=\"https:\/\/ibex.tech\/cloud\/amazon-web-services\/s3\/creating-a-s3-bucket\">created here<\/a>.\n<\/p>\n<p>\nNo you can create the S3 client:\n<\/p>\n<pre>\r\n<code>\r\n\t\t$sdk = new Aws\\Sdk([\r\n\t\t\t\t&#39;version&#39; =&gt; &#39;latest&#39;,\r\n\t\t\t\t&#39;region&#39;  =&gt; &#39;us-west-2&#39;,\r\n\t\t\t\t&#39;credentials&#39; =&gt; [\r\n\t\t\t\t\t\t&#39;key&#39;    =&gt; $AwsS3AccessKeyId,\t\t\t\t\/\/&lt;&lt;&lt;&lt;&lt; S3 ACCESS KEY ID\r\n\t\t\t\t\t\t&#39;secret&#39; =&gt; $AwsS3AccessSecretKey,\t\t\/\/&lt;&lt;&lt;&lt;&lt; S3 SECRTET ACCESS KEY\r\n\t\t\t\t],\r\n\t\t]);\r\n\r\n\t\t$s3Client = $sdk-&gt;createS3();\r\n<\/code><\/pre>\n<h4>\nMethods<br \/>\n<\/h4>\n<p>\n<a href=\"http:\/\/docs.aws.amazon.com\/aws-sdk-php\/v3\/api\/class-Aws.S3.S3Client.html\">http:\/\/docs.aws.amazon.com\/aws-sdk-php\/v3\/api\/class-Aws.S3.S3Client.html<\/a>\n<\/p>\n<h4>\nList All Files In A bucket<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\t\/\/----- LIST CONTENTS OF BUCKET -----\r\n\t\t$objects = $s3Client-&gt;getIterator(&#39;ListObjects&#39;, array(\r\n\t\t\t\t&quot;Bucket&quot; =&gt; $AwsS3BucketName\t\t\/\/&lt;&lt;&lt;&lt;S3 BUCKET NAME\r\n\t\t));\r\n\t\tforeach ($objects as $object)\r\n\t\t{\r\n\t\t\t$filename =  $object['Key'];\r\n\t\t\t$LastModified =  $object[&#39;LastModified&#39;];\r\n\t\t\t\r\n\t\t\t\/\/Work out how old the file is\r\n\t\t\t$end = strtotime(date(&quot;Y-m-d H:i:s&quot;));\t\t\/\/Now\r\n\t\t\t$start = strtotime($LastModified);\r\n\t\t\tif ($start &gt; $end)\r\n\t\t\t{\r\n\t\t\t\t$FileDaysOld = 0;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\t$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.\r\n\t\t\t}\r\n\r\n\t\t\techo &quot;$filename | $LastModified | $FileDaysOld &lt;br&gt;&quot;;\r\n\t\t}\r\n<\/code><\/pre>\n<h4>\nDelete File<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/----- DELETE FILE -----\r\n\t$result = $s3Client-&gt;deleteObject([\r\n\t\t\t&#39;Bucket&#39; =&gt; $AwsS3BucketName,\r\n\t\t\t&#39;Key&#39; =&gt; $filename\r\n\t]);\r\n\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get AWS SDK Simple include the SDK .phar method&nbsp; Download the &quot;aws.phar&quot; file and store it in a subdirectory called, says awssdk.&nbsp; Then in your php files use: require &#39;awssdk\/aws.phar&#39;; Define your S3 access values ready to use in your code $AwsS3BucketName = &#39;&#39;; \/\/&lt;&lt;&lt;&lt;SET THIS $AwsS3AccessKeyId = &#39;&#39;; \/\/&lt;&lt;&lt;&lt;SET THIS $AwsS3AccessSecretKey = &#39;&#39;; \/\/&lt;&lt;&lt;&lt;SET [&hellip;]<\/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-1776","post","type-post","status-publish","format-standard","hentry","category-s3"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1776","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=1776"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1776\/revisions"}],"predecessor-version":[{"id":1871,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1776\/revisions\/1871"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=1776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=1776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=1776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}