PHP Amazon S3 File Upload Code AWS Signature Version 4
PHP Amazon S3 File Upload Code AWS Signature Version 4
2017-07-24 / Unknown

Amazon offers a PHP SDK for taking care of AWS and S3 asks for, yet it tips the scales at more than 500 documents and almost 5MB. In the event that you simply need to transfer a record to a S3 basin utilizing PHP, you can make the HTTP POST ask for yourself utilizing just around 50 lines of code. This is likewise helpful on the off chance that you need to see how the demand and approval prepare function.

AWS Signature Version 4
This code uses Amazon AWS Signature Version 4. To view the old Version 2 code, see this post.

Sample Code
This is test PHP code to help you comprehend and test transferring to Amazon S3. Requires PHP variant 5.4 or fresher because of exhibit language structure. On the off chance that you need to utilize this in your venture, you ought to most likely alter this and place it into a capacity or strategy. I have it designed like this particularly to help demonstrate how the procedure functions.

Perused the inline code remarks for a clarification of each segment:

<?php

// USER OPTIONS
// Replace these values with ones appropriate to you.
accessKeyId&nbsp;=&nbsp;'YOUR_ACCESS_KEY_ID';<br>secretKey = ‘YOUR_SECRET_KEY’;
bucket&nbsp;=&nbsp;'YOUR_BUCKET_NAME';<br>region = ‘BUCKET_AMAZON_REGION’; // us-west-2, us-east-1, etc
acl&nbsp;=&nbsp;'ACCESS_CONTROL_LIST';&nbsp;<span id="span_a5da_8">//&nbsp;private,&nbsp;public-read,&nbsp;etc</span><br>filePath = ‘path/to/file.jpg’;
fileName&nbsp;=&nbsp;'myimage.jpg';<br>fileType = ‘image/jpeg’;

// VARIABLES
// These are used throughout the request.
longDate&nbsp;=&nbsp;gmdate('Ymd\THis\Z');<br>shortDate = gmdate(‘Ymd’);
credential&nbsp;=&nbsp;accessKeyId . ‘/’ . shortDate&nbsp;.&nbsp;'/'&nbsp;.&nbsp;region . ‘/s3/aws4_request’;

// POST POLICY
// Amazon requires a base64-encoded POST policy written in JSON.
// This tells Amazon what is acceptable for this request. For
// simplicity, we set the expiration date to always be 24H in 
// the future. The two “starts-with” fields are used to restrict
// the content of “key” and “Content-Type”, which are specified
// later in the POST fields. Again for simplicity, we use blank
// values (’’) to not put any restrictions on those two fields.
policy&nbsp;=&nbsp;base64_encode(json_encode([<br>&nbsp;&nbsp;&nbsp;&nbsp;'expiration'&nbsp;=&gt;&nbsp;gmdate('Y-m-d\TH:i:s\Z',&nbsp;time()&nbsp;+&nbsp;86400),<br>&nbsp;&nbsp;&nbsp;&nbsp;'conditions'&nbsp;=&gt;&nbsp;[<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['acl'&nbsp;=&gt;&nbsp;acl],
        [‘bucket’ => bucket],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['starts-with',&nbsp;'Content-Type’, ‘’],
        [‘starts-with’, 'key',&nbsp;''],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['x-amz-algorithm'&nbsp;=&gt;&nbsp;'AWS4-HMAC-SHA256'],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['x-amz-credential'&nbsp;=&gt;&nbsp;credential],
        [‘x-amz-date’ => longDate]<br>&nbsp;&nbsp;&nbsp;&nbsp;]<br>]));<br><br><span id="span_a5da_19">//&nbsp;SIGNATURE</span><br><span id="span_a5da_20">//&nbsp;A&nbsp;base64-encoded&nbsp;HMAC&nbsp;hashed&nbsp;signature&nbsp;with&nbsp;your&nbsp;secret&nbsp;key.</span><br><span id="span_a5da_21">//&nbsp;This&nbsp;is&nbsp;used&nbsp;so&nbsp;Amazon&nbsp;can&nbsp;verify&nbsp;your&nbsp;request,&nbsp;and&nbsp;will&nbsp;be</span><br><span id="span_a5da_22">//&nbsp;passed&nbsp;along&nbsp;in&nbsp;a&nbsp;POST&nbsp;field&nbsp;later.</span><br>signingKey = hash_hmac(‘sha256’, shortDate,&nbsp;'AWS4'&nbsp;.&nbsp;secretKey, true);
signingKey&nbsp;=&nbsp;hash_hmac('sha256',&nbsp;region, signingKey,&nbsp;true);<br>signingKey = hash_hmac(‘sha256’, ‘s3’, signingKey,&nbsp;true);<br>signingKey = hash_hmac(‘sha256’, ‘aws4_request’, signingKey,&nbsp;true);<br>signature = hash_hmac(‘sha256’, policy,&nbsp;signingKey);

// CURL
// The cURL request. Passes in the full URL to your Amazon bucket.
// Sets RETURNTRANSFER and HEADER to true to see the full response from
// Amazon, including body and head. Sets POST fields for cURL.
// Then executes the cURL request.
ch&nbsp;=&nbsp;curl_init('https://'&nbsp;.&nbsp;bucket . ‘.s3-’ . region&nbsp;.&nbsp;'.amazonaws.com');<br>curl_setopt(ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt(ch,&nbsp;CURLOPT_HEADER,&nbsp;true);<br>curl_setopt(ch, CURLOPT_POST, true);
curl_setopt(ch,&nbsp;CURLOPT_POSTFIELDS,&nbsp;[<br>&nbsp;&nbsp;&nbsp;&nbsp;'Content-Type'&nbsp;=&gt;&nbsp;&nbsp;fileType,
    ‘acl’ => acl,<br>&nbsp;&nbsp;&nbsp;&nbsp;'key'&nbsp;=&gt;&nbsp;fileName,
    ‘policy’ =>  policy,<br>&nbsp;&nbsp;&nbsp;&nbsp;'x-amz-algorithm'&nbsp;=&gt;&nbsp;'AWS4-HMAC-SHA256',<br>&nbsp;&nbsp;&nbsp;&nbsp;'x-amz-credential'&nbsp;=&gt;&nbsp;credential,
    ‘x-amz-date’ => longDate,<br>&nbsp;&nbsp;&nbsp;&nbsp;'x-amz-signature'&nbsp;=&gt;&nbsp;signature,
    ‘file’ => new CurlFile(realpath(filePath),&nbsp;fileType, fileName)<br>]);<br>fileName)<br>]);<br>response = curl_exec(ch);<br><br><span id="span_a5da_28">//&nbsp;RESPONSE</span><br><span id="span_a5da_29">//&nbsp;If&nbsp;Amazon&nbsp;returns&nbsp;a&nbsp;response&nbsp;code&nbsp;of&nbsp;204,&nbsp;the&nbsp;request&nbsp;was</span><br><span id="span_a5da_30">//&nbsp;successful&nbsp;and&nbsp;the&nbsp;file&nbsp;should&nbsp;be&nbsp;sitting&nbsp;in&nbsp;your&nbsp;Amazon&nbsp;S3</span><br><span id="span_a5da_31">//&nbsp;bucket.&nbsp;If&nbsp;a&nbsp;code&nbsp;other&nbsp;than&nbsp;204&nbsp;is&nbsp;returned,&nbsp;there&nbsp;will&nbsp;be&nbsp;an</span><br><span id="span_a5da_32">//&nbsp;XML-formatted&nbsp;error&nbsp;code&nbsp;in&nbsp;the&nbsp;body.&nbsp;For&nbsp;simplicity,&nbsp;we&nbsp;use</span><br><span id="span_a5da_33">//&nbsp;substr&nbsp;to&nbsp;extract&nbsp;the&nbsp;error&nbsp;code&nbsp;and&nbsp;output&nbsp;it.</span><br>if&nbsp;(curl_getinfo(ch, CURLINFO_HTTP_CODE) == 204) {
    echo ‘Success!’;
} else {
    error&nbsp;=&nbsp;substr(response, strpos(response,&nbsp;'&lt;code&gt;')&nbsp;+&nbsp;6);<br>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;substr(error, 0, strpos(error,&nbsp;'&lt;/code&gt;'));<br>}</code></pre><pre><span id="span_a5da_34">If you aren't receiving any response at all, check if&nbsp;</span><samp id="samp_a5da_0">curl_exec(ch) is returning false. If it is, chances are it’s due to an SSL issue. You can check the error at curl_error(ch)</samp><spanid="spana5da36">.Fortestingpurposes,youcansetthesetwoadditionalcURLoptionsandseeifitworks:</span></pre><pre><codeid="codea5da1">curlsetopt(ch)</samp><span id="span_a5da_36">. For testing purposes, you can set these two additional cURL options and see if it works:</span></pre><pre><code id="code_a5da_1">curl_setopt(ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
But note that doing this is very insecure. Read this Stackoverflow post and this blog post.

Source: hex3d.com

PHP Amazon S3 File Upload Code AWS Signature Version 4

PermaLink: https://www.webmanajemen.com/2017/07/php-amazon-s3-file-upload-code-aws.html
Google Rich Snippets | Schema Markup Validator | Google Pagespeed Insight