PHP上传文件到腾讯cos上源码,腾讯云cos通过签名方式生成下载链接

腾讯云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;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是PHP腾讯云COS签名生成的示例代码: ```php <?php // 计算签名 function getAuthorization($secretId, $secretKey, $method, $pathname, $headers, $query = null, $expired = 86400) { // 整理参数 $headers = array_change_key_case($headers); $headersStr = ""; ksort($headers); foreach ($headers as $k => $v) { if (preg_match("/^x-cos-/", $k)) { $headersStr .= strtolower($k) . ':' . trim($v) . "\n"; } } $qSignTime = [ time(), time() + $expired ]; $qSignString = sprintf("q-sign-algorithm=sha1&q-ak=%s&q-sign-time=%d;%d&q-key-time=%d;%d", $secretId, $qSignTime[0], $qSignTime[1], $qSignTime[0], $qSignTime[1]); $urlParamList = []; if (isset($query)) { $urlParamList = array_merge($urlParamList, $query); } $urlParamList[] = $qSignString; sort($urlParamList); $urlParamString = implode("&", $urlParamList); $httpString = strtolower($method) . "\n" . $pathname . "\n" . $urlParamString . "\n" . $headersStr; $signKey = hash_hmac("sha1", implode("\n", $qSignTime) . "\n" . sha1($httpString) . "\n", $secretKey); $qHeaderList = []; foreach ($headers as $k => $v) { if (preg_match("/^x-cos-/", $k)) { array_push($qHeaderList, strtolower($k)); } } sort($qHeaderList); $qHeaderString = implode(";", $qHeaderList); $qSign = "q-sign-algorithm=sha1&q-ak=" . $secretId . "&q-sign-time=" . $qSignTime[0] . ";" . $qSignTime[1] . "&q-key-time=" . $qSignTime[0] . ";" . $qSignTime[1] . "&q-header-list=" . $qHeaderString . "&q-url-param-list=" . $urlParamString . "&q-signature=" . hash_hmac("sha1", $httpString, $signKey); return $qSign; } // 示例:上传文件 $secretId = 'your_secret_id'; $secretKey = 'your_secret_key'; $region = 'ap-guangzhou'; $bucket = 'your_bucket_name'; $key = 'test.txt'; $file = '/path/to/test.txt'; // 生成签名 $method = 'PUT'; $pathname = '/' . $key; $headers = [ 'Content-Type' => 'text/plain', 'Cache-Control' => 'no-cache', 'Content-Length' => filesize($file), 'Host' => $bucket . '.cos.' . $region . '.myqcloud.com', ]; $auth = getAuthorization($secretId, $secretKey, $method, $pathname, $headers); // 发送请求 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://' . $bucket . '.cos.' . $region . '.myqcloud.com/' . $key); curl_setopt($ch, CURLOPT_PUT, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: ' . $auth, 'Content-Type: text/plain', 'Cache-Control: no-cache', 'Content-Length: ' . filesize($file), 'Host: ' . $bucket . '.cos.' . $region . '.myqcloud.com', ]); $fh = fopen($file, 'r'); curl_setopt($ch, CURLOPT_INFILE, $fh); curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file)); curl_exec($ch); fclose($fh); curl_close($ch); echo '上传成功'; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值