腾讯云cos通过签名方式生成下载链接,让你的腾讯云图片在浏览器访问不是预览,而是下载。(mp4等视频格式同样有效)
class qcloud
{
private $_region = 'ap-shanghai';
private $_bucket = '';
private $_secret_id = ""; //"云 API 密钥 SecretId";
private $_secret_key = ""; //"云 API 密钥 SecretKey";
//获取下载链接
public function get_down_load_url($url)
{
$qSignStart = time();
$qSignEnd = bcadd($qSignStart,3600,0);
$fileUri = parse_url($url);
$ext_arr = explode('/',$url);
$ext = $ext_arr[count($ext_arr)-1];
$authorization = $this->get_authorization_down_url($this->_secret_id, $this->_secret_key, $qSignStart, $qSignEnd, $fileUri['path'], '');
$down_load = '&response-content-type=application%2Foctet-stream&response-cache-control=max-age%3D86400&response-content-disposition=attachment%3B%20filename%3D'.$ext;
return "https://{$this->_bucket}.cos.{$this->_region}.myqcloud.com{$fileUri['path']}?".$authorization."".$down_load;
}
/**
* 按COS要求从数组中获取 Signature 中 [HttpString] 内容
* 标准格式 key=value&key=value&...
* 数组元素按键字典排序 *
* key转换为小写
* value进行UrlEncode转换
* 转换为key=value格式
* 多对key=value之间用连接符连接
*
*/
public function get_http_header_string($headers){
if(!is_array($headers)){
return false;
}
try{
$tmpArray = array();
foreach($headers as $key => $value){
$tmpKey = strtolower($key);
$tmpArray[$tmpKey] = urlencode($value);
}
ksort($tmpArray);
$headerArray = array();
foreach( $tmpArray as $key => $value){
array_push($headerArray, "$key=$value");
}
return implode('&', $headerArray);
}
catch(Exception $error){
return false;
}
}
/**
* 按COS要求对header_list内容进行转换
* 提取所有key
* 字典排序
* key转换为小写
* 多对key=value之间用连接符连接
*
*/
public function get_q_header_list($headers){
if(!is_array($headers)){
return false;
}
try{
$tmpArray = array();
foreach( $headers as $key=>$value){
array_push($tmpArray, strtolower($key));
}
sort($tmpArray);
return implode(';', $tmpArray);
}
catch(Exception $error){
return false;
}
}
/**
* 计算签名
* secretId、secretKey 为必需参数,qSignStart、qSignEnd为调试需要,测试通过后应取消,改为方法内自动创建
*/
public function get_authorization_down_url( $secretId, $secretKey, $qSignStart, $qSignEnd, $fileUri, $headers ){
$qSignTime = "$qSignStart;$qSignEnd"; //unix_timestamp;unix_timestamp
$qKeyTime = $qSignTime;
$header_list = $this->get_q_header_list($headers);
//如果 Uri 中带有 ?的请求参数,该处应为数组排序后的字符串组合
$url_param_list = '';
//compute signature
$httpMethod = 'get';
$httpUri = $fileUri;
//与 q-url-param-list 相同
$httpParameters = $url_param_list;
//将自定义请求头分解为 & 连接的字符串
$headerString = $this->get_http_header_string( $headers );
// 计算签名中的 signature 部分
$signTime = $qSignTime;
$signKey = hash_hmac('sha1', $signTime, $secretKey);
$httpString = "$httpMethod\n$httpUri\n$httpParameters\n$headerString\n";
$sha1edHttpString = sha1($httpString);
$stringToSign = "sha1\n$signTime\n$sha1edHttpString\n";
$signature = hash_hmac('sha1', $stringToSign, $signKey);
//组合结果
$authorization = "q-sign-algorithm=sha1&q-ak=$secretId&q-sign-time=$qSignTime&q-key-time=$qKeyTime&q-header-list=$header_list&q-url-param-list=$url_param_list&q-signature=$signature";
return $authorization;
}
运行代码
$upload_success_url = 'https://img.domain.com/test.mp4';
$upload_success_url = 'https://img.domain.com/test.jpg';
$qcloud_class = new qcloud();
$get_down_load_url = $qcloud_class->get_down_load_url($upload_success_url);
echo $get_down_load_url;