· 7 years ago · Jul 17, 2018, 06:30 PM
1<?php
2$access_key = "123456"; //Access Key
3$secret_key = "123456"; //Secret Key
4$my_bucket = "123456"; //bucket name
5$region = "us-east-2"; //bucket region
6$success_redirect = 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; //URL to which the client is redirected upon success (currently self)
7$allowd_file_size = "1048579"; //1 MB allowed Size
8
9//dates
10$short_date = gmdate('Ymd'); //short date
11$iso_date = gmdate("Ymd\THis\Z"); //iso format date
12$expiration_date = gmdate('Y-m-d\TG:i:s\Z', strtotime('+1 hours')); //policy expiration 1 hour from now
13
14//POST Policy required in order to control what is allowed in the request
15//For more info http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html
16$policy = utf8_encode(json_encode(array(
17 'expiration' => $expiration_date,
18 'conditions' => array(
19 array('acl' => 'public-read'),
20 array('bucket' => $my_bucket),
21 array('success_action_redirect' => $success_redirect),
22 array('starts-with', '$key', ''),
23 array('content-length-range', '1', $allowd_file_size),
24 array('x-amz-credential' => $access_key.'/'.$short_date.'/'.$region.'/s3/sasdasd/aws4_request'),
25 array('x-amz-algorithm' => 'AWS4-HMAC-SHA256'),
26 array('X-amz-date' => $iso_date)
27 ))));
28
29//Signature calculation (AWS Signature Version 4)
30//For more info http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html
31$kDate = hash_hmac('sha256', $short_date, 'AWS4' . $secret_key, true);
32$kRegion = hash_hmac('sha256', $region, $kDate, true);
33$kService = hash_hmac('sha256', "s3", $kRegion, true);
34$kSigning = hash_hmac('sha256', "aws4_request", $kService, true);
35$signature = hash_hmac('sha256', base64_encode($policy), $kSigning);
36?>
37<!DOCTYPE HTML>
38<html>
39<head>
40<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
41<title>Aws S3 Direct File Uploader</title>
42</head>
43<body>
44<form action="http://<?= $my_bucket ?>.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
45<input type="hidden" name="key" value="${filename}" />
46<input type="hidden" name="acl" value="public-read" />
47<input type="hidden" name="X-Amz-Credential" value="<?= $access_key; ?>/<?= $short_date; ?>/<?= $region; ?>/s3/aws4_request" />
48<input type="hidden" name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
49<input type="hidden" name="X-Amz-Date" value="<?=$iso_date ; ?>" />
50<input type="hidden" name="Policy" value="<?=base64_encode($policy); ?>" />
51<input type="hidden" name="X-Amz-Signature" value="<?=$signature ?>" />
52<input type="hidden" name="success_action_redirect" value="<?= $success_redirect ?>" />
53<input type="file" name="file" />
54<input type="submit" value="Upload File" />
55</form>
56<?php
57//After success redirection from AWS S3
58if(isset($_GET["key"]))
59{
60 $filename = $_GET["key"];
61 $ext = pathinfo($filename, PATHINFO_EXTENSION);
62 if(in_array($ext, array("jpg", "png", "gif", "jpeg"))){
63 echo '<hr />Image File Uploaded : '.$my_bucket.'.s3.amazonaws.com/'.$_GET["key"].' "<br /><img src="//'.$my_bucket.'.s3.amazonaws.com/'.$_GET["key"].'" style="width:100%;" />';
64 }else{
65 echo '<hr />File Uploaded : <br /><a href="http://'.$my_bucket.'.s3.amazonaws.com/'.$_GET["key"].'">'.$filename.'</a>';
66 }
67}
68?>
69</body>
70</html>