PHP
PHP生成腾讯云COS接口需要的请求签名并上传图片
PHP生成腾讯云COS接口需要的请求签名并上传图片
最近在弄腾讯云COS图片存储的时候,由于不想使用官方提供的SDK,打算通过http方式调取接口而不走官方SDK,博主也通过看官方文档以及自己研究,把自己最终对实现代码分享给大家。
主要难点是如何生成请求签名,以下是生成签名部分:
/** * 请求获取签名 * @param $method * @param $path * @param $bucketURL * @param $secretKey * @param $secretId * @return string */ static function RequestSign($method, $path, $bucketURL, $secretKey, $secretId) { $signTime = (string)(time() - 60) . ';' . (string)(time() + 1200); $host = parse_url($bucketURL); $httpString = sprintf("%s\n%s\n\nhost=%s\n", strtolower($method), $path, $host['host']); $stringToSign = sprintf("sha1\n%s\n%s\n", $signTime, sha1($httpString)); $signKey = hash_hmac('sha1', $signTime, $secretKey); $signature = hash_hmac('sha1', $stringToSign, $signKey); return sprintf('q-sign-algorithm=sha1&q-ak=%s&q-sign-time=%s&q-key-time=%s&q-header-list=host&q-url-param-list=&q-signature=%s', $secretId, $signTime, $signTime, $signature); }
以下是上传图片至腾讯云COS指定位置代码
/** * 上传图片至腾讯云COS * @param $srcPath * @param $key */ static function uploadImageToCos($srcPath, $key) { #获取基本配置 $secretId = SecretId; //"云 API 密钥 SecretId"; $secretKey =SecretKey; //"云 API 密钥 SecretKey"; $host =<BucketName-APPID>.cos.<Region>.myqcloud.com; //请求地址 #组合请求路由 $path = "/" . $key; #获取签名 $authorization = RequestSign('put', $path, $host, $secretKey, $secretId); #组合请求数据 $url = $host . $path; $data = file_get_contents($srcPath); $header = array( 'Authorization: ' . $authorization, 'Date: ' . gmdate('D, d M Y H:i:s T') ); #请求 $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $data = curl_exec($curl); $error = curl_error($curl); curl_close($curl); }
以下是删除腾讯云COS上指定图片代码
/** * 删除图片 * @param $key */ static function deleteImageToCos($key) { #获取基本配置 $secretId = SecretId; //"云 API 密钥 SecretId"; $secretKey =SecretKey; //"云 API 密钥 SecretKey"; $host =<BucketName-APPID>.cos.<Region>.myqcloud.com; //请求地址 #组合请求路由 $path = "/" . $key; #组合请求连接 $url = $host . $path; #获取签名 $authorization = RequestSign("delete", $path, $host, $secretKey, $secretId); #组合头部 $header = array( 'Authorization: ' . $authorization, 'Date: ' . gmdate('D, d M Y H:i:s T') ); #请求 $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); $data = curl_exec($curl); $error = curl_error($curl); curl_close($curl); }
调用方法代码
$srcPath = ‘www/wwwroot/test.jpg’; //本地图片目录 $key = "test/test.jpg"; //上传指定地址【test文件夹下命名为test.jpg】 uploadImageToCos($srcPath, $key); //上传图片 deleteImageToCos($key); //删除图片
这样我们便成功上传图片至腾讯云COS指定位置,成功删除腾讯云COS指定图片。
不错不错,刚好用上
回复