无法覆盖s3中的内容处置标头

时间:2012-09-24 02:01:11

标签: php amazon-s3 amazon-web-services

我正在使用以下php函数为私有文件提供临时访问权限。

function get_s3_signed_url($bucket, $resource, $AWS_S3_KEY, $AWS_s3_secret_key, $expire_seconds) {
    $expires = time()+$expire_seconds;
    // S3 Signed URL creation
    $string_to_sign = "GET\n\n\n{$expires}\n/".str_replace(".s3.amazonAWS.com","", $bucket)."/$resource";
    $signature = urlencode(base64_encode((hash_hmac("sha1", utf8_encode($string_to_sign), $AWS_s3_secret_key, TRUE))));

    $authentication_params = "AWSAccessKeyId=".$AWS_S3_KEY;
    $authentication_params.= "&Expires={$expires}";
    $authentication_params.= "&Signature={$signature}";

    return $link = "http://s3.amazonAWS.com/{$bucket}/{$resource}?{$authentication_params}";
}       

我想添加内容处置标题,以便在用户尝试访问此网址时将文件名更改为test.mp3,默认文件名为982jdjd2p3.mp3

$privateUrl = array('privateUrl' => get_s3_signed_url('testbucket', '982jdjd2p3.mp3', $my_aws_key, $my_aws_secret_key, 60));

我尝试将以下代码行添加到函数

$file_name = 'test.mp3';       
$authentication_params.= "&Content-Disposition={$file_name}";

但是,当我点击网址时

http://s3.amazonAWS.com/testbucket/982jdjd2p3.mp3?AWSAccessKeyId=***&Expires=***&Signature=***&Content-Disposition=test.mp3

建议的文件名保存为982jdjd2p3.mp3

如何使用此功能覆盖s3 GET请求的内容处置标头?

另见

Amazon S3 Change file download name

修改

以下是使用此函数使用get请求重命名文件的最新尝试。

function get_s3_signed_url($bucket, $resource, $AWS_S3_KEY, $AWS_s3_secret_key, $expire_seconds) {
    $expires = time()+$expire_seconds;
    // S3 Signed URL creation
    $filename='moot.mp3';
    $disposition = "response-content-disposition=" . urlencode("attachment; filename={$filename}");            

    $string_to_sign = "GET\n\n\n{$expires}\n/".str_replace(".s3.amazonAWS.com","", $bucket)."/$resource";
    $string_to_sign .= "?{$disposition}";
    $signature = urlencode(base64_encode((hash_hmac("sha1", utf8_encode($string_to_sign), $AWS_s3_secret_key, TRUE))));

    $authentication_params = "AWSAccessKeyId=".$AWS_S3_KEY;  
    $authentication_params.= "&Expires={$expires}";
    $authentication_params.= "&Signature={$signature}";
    $authentication_params.= "&{$disposition}";

    return $link = "http://s3.amazonAWS.com/{$bucket}/{$resource}?{$authentication_params}";
}       

2 个答案:

答案 0 :(得分:10)

您的函数的问题是标题值应该在最终超链接中编码,但不能用于签名。以下功能纠正了:

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";
}

答案 1 :(得分:1)

Content-Disposition的格式无效,请指定disposition-type

实施例: Content-Disposition: attachment; filename=test.mp3;

在签名和参数中使用response-content-disposition

$disposition = "response-content-disposition=" . urlencode("attachment; filename={$filename}");
/* ... */
$string_to_sign .= "?{$disposition}";
/* ... */
$authentication_params.= "&{$disposition}";