1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
$aws_access_key_id = 'ZZIAXB6XXPMGHXM3AJBXX'; $aws_secret_access_key = 'X/4BDguGDzRSvuxl5tHq3ZA9NvbQvURXs2vtiDDD'; // Bucket $bucket_name = 'bucketname'; // AWS region and Host Name (Host names are different for each AWS region) // As an example these are set to us-east-1 (US Standard) $aws_region = 'us-east-2'; $host_name = $bucket_name . '.s3.amazonaws.com'; // Server path where content is present. This is just an example $content_path = 'sourcepath.png'; $content = file_get_contents($content_path); // AWS file permissions $content_acl = 'public-read'; // MIME type of file. Very important to set if you later plan to load the file from a S3 url in the browser (images, for example) $content_type = 'image/png'; // Name of content on S3 $content_title = 's3folder/s3image.png' // Service name for S3 $aws_service_name = 's3'; // UTC timestamp and date $timestamp = gmdate('Ymd\THis\Z'); $date = gmdate('Ymd'); // HTTP request headers as key & value $request_headers = array(); $request_headers['Content-Type'] = $content_type; $request_headers['Date'] = $timestamp; $request_headers['Host'] = $host_name; $request_headers['x-amz-acl'] = $content_acl; $request_headers['x-amz-content-sha256'] = hash('sha256', $content); // Sort it in ascending order ksort($request_headers); // Canonical headers $canonical_headers = []; foreach ($request_headers as $key => $value) { $canonical_headers[] = strtolower($key) . ":" . $value; } $canonical_headers = implode("\n", $canonical_headers); // Signed headers $signed_headers = []; foreach ($request_headers as $key => $value) { $signed_headers[] = strtolower($key); } $signed_headers = implode(";", $signed_headers); // Cannonical request $canonical_request = []; $canonical_request[] = "PUT"; $canonical_request[] = "/" . $content_title; $canonical_request[] = ""; $canonical_request[] = $canonical_headers; $canonical_request[] = ""; $canonical_request[] = $signed_headers; $canonical_request[] = hash('sha256', $content); $canonical_request = implode("\n", $canonical_request); $hashed_canonical_request = hash('sha256', $canonical_request); // AWS Scope $scope = []; $scope[] = $date; $scope[] = $aws_region; $scope[] = $aws_service_name; $scope[] = "aws4_request"; // String to sign $string_to_sign = []; $string_to_sign[] = "AWS4-HMAC-SHA256"; $string_to_sign[] = $timestamp; $string_to_sign[] = implode('/', $scope); $string_to_sign[] = $hashed_canonical_request; $string_to_sign = implode("\n", $string_to_sign); // Signing key $kSecret = 'AWS4' . $aws_secret_access_key; $kDate = hash_hmac('sha256', $date, $kSecret, true); $kRegion = hash_hmac('sha256', $aws_region, $kDate, true); $kService = hash_hmac('sha256', $aws_service_name, $kRegion, true); $kSigning = hash_hmac('sha256', 'aws4_request', $kService, true); // Signature $signature = hash_hmac('sha256', $string_to_sign, $kSigning); // Authorization $authorization = [ 'Credential=' . $aws_access_key_id . '/' . implode('/', $scope), 'SignedHeaders=' . $signed_headers, 'Signature=' . $signature, ]; $authorization = 'AWS4-HMAC-SHA256' . ' ' . implode(',', $authorization); // Curl headers $curl_headers = ['Authorization: ' . $authorization]; foreach ($request_headers as $key => $value) { $curl_headers[] = $key . ": " . $value; } $url = 'https://' . $host_name . '/' . $content_title; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $content); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code != 200) { exit('Error : Failed to upload'); } |
More Stories
CPU & Memory usage in PHP
Install PHP mcrypt extension on Ubuntu
Text to speech