'secret' => '<aws secret key>',
'region' => '<aws region>',
'bucket' => '<aws bucket name>'
header('Content-Type: application/json');
if(isset($_POST["filename"]) && isset($_FILES)) {
$result = upload_file($S3_CONFIG, $_FILES['recording']['tmp_name'], $_POST["filename"].".wav");
echo(json_encode($result));
echo json_encode(array("status"=>"failure"));
function upload_file($config, $filepath, $new_name) {
$content_type = 'audio/wav';
$file = file_get_contents($filepath);
$host = $config['bucket'] . ".$service." . $config['region'] . '.amazonaws.com';
$timestamp = gmDate("Ymd\THis\Z"); // Amazon web stamp format
// HTTP request headers as key & value
$request_headers['Content-Type'] = $content_type;
$request_headers['Date'] = $timestamp;
$request_headers['Host'] = $host;
$request_headers['x-amz-content-sha256'] = hash('sha256', $file);
// Sort it in ascending order
foreach($request_headers as $key => $value) {
$canonical_headers[] = strtolower($key) . ":" . $value;
$canonical_headers = implode("\n", $canonical_headers);
foreach($request_headers as $key => $value) {
$signed_headers[] = strtolower($key);
$signed_headers = implode(";", $signed_headers);
$canonical_request[] = "PUT";
$canonical_request[] = "/" . $filename;
$canonical_request[] = "";
$canonical_request[] = $canonical_headers;
$canonical_request[] = "";
$canonical_request[] = $signed_headers;
$canonical_request[] = hash('sha256', $file);
$canonical_request = implode("\n", $canonical_request);
$hashed_canonical_request = hash('sha256', $canonical_request);
$scope[] = $config['region'];
$scope[] = "aws4_request";
$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);
$kSecret = 'AWS4' . $config['secret'];
$kDate = hash_hmac('sha256', $date, $kSecret, true);
$kRegion = hash_hmac('sha256', $config['region'], $kDate, true);
$kService = hash_hmac('sha256', $service, $kRegion, true);
$kSigning = hash_hmac('sha256', 'aws4_request', $kService, true);
$signature = hash_hmac('sha256', $string_to_sign, $kSigning);
'Credential=' . $config['key'] . '/' . implode('/', $scope),
'SignedHeaders=' . $signed_headers,
'Signature=' . $signature,
$authorization = 'AWS4-HMAC-SHA256' . ' ' . implode( ',', $authorization);
$curl_headers = [ 'Authorization: ' . $authorization ];
foreach($request_headers as $key => $value) {
$curl_headers[] = $key . ": " . $value;
$url = 'http://' . $host . '/' . $filename;
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$retval['status'] = 'success';
$retval['status'] = 'fail';
$retval['message'] = $ex->getMessage();