我正在使用以下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}";
}
我想添加内容分布头,这样当用户尝试访问此URL时,我可以将文件名更改为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}";
然而,当我点击以下URL时:
http://s3.amazonAWS.com/testbucket/982jdjd2p3.mp3?AWSAccessKeyId=***&Expires=***&Signature=***&Content-Disposition=test.mp3
建议的文件名应保存为
982jdjd2p3.mp3
。使用此函数如何覆盖S3 GET请求的内容分发头?
另请参见:Amazon S3更改文件下载名称
编辑:
这是最近尝试使用此功能重命名文件的方法。
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}";
}
$save_as='hello world';
,则文件将保存为hello
。有什么解决方法吗? - user784637filename="hello world.txt"
。 - Ja͢ck