I\'m using the following php function to give temporary access to the public for a private file.
function get_s3_signed_url($bucket, $resource, $AWS_S3_KEY,
Format of your Content-Disposition is invalid, Specify disposition-type
.
Example:
Content-Disposition: attachment; filename=test.mp3;
Use response-content-disposition
in signature and params:
$disposition = "response-content-disposition=" . urlencode("attachment; filename={$filename}");
/* ... */
$string_to_sign .= "?{$disposition}";
/* ... */
$authentication_params.= "&{$disposition}";
The problem with your function is that the header values should be encoded in the final hyperlink, but not for signing. The following function corrects that:
function get_s3_signed_url($bucket, $resource, $AWS_S3_KEY, $AWS_s3_secret_key, $expire_seconds, $save_as)
{
$expires = time()+$expire_seconds;
// S3 Signed URL creation
$headers = array(
'response-content-disposition' => 'attachment; filename=' . $save_as,
);
$resource = str_replace(array('%2F', '%2B'), array('/', '+'), rawurlencode($resource));
$string_to_sign = "GET\n\n\n$expires\n/$bucket/$resource";
$final_url = "http://s3.amazonaws.com/$bucket/$resource?";
$append_char = '?';
foreach ($headers as $header => $value) {
$final_url .= $header . '=' . urlencode($value) . '&';
$string_to_sign .= $append_char . $header . '=' . $value;
$append_char = '&';
}
$signature = urlencode(base64_encode(hash_hmac('sha1', $string_to_sign, $AWS_s3_secret_key, true)));
return $final_url . "AWSAccessKeyId=$AWS_S3_KEY&Expires=$expires&Signature=$signature";
}