作者 lyh

gx

@@ -43,8 +43,16 @@ class TencentCosService extends BaseService @@ -43,8 +43,16 @@ class TencentCosService extends BaseService
43 public function upload_image($path,$fileName){ 43 public function upload_image($path,$fileName){
44 // 构建请求URL 44 // 构建请求URL
45 $pathname = '/'.$this->config['bucket'].$path.'/'.$fileName; 45 $pathname = '/'.$this->config['bucket'].$path.'/'.$fileName;
46 - $signature = $this->generateSignature($path.'/'.$fileName);  
47 - $url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname.'?sign='.$signature; 46 + $url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname;
  47 + $date = gmdate('D, d M Y H:i:s T');
  48 + $signKey = hash_hmac('sha1', $date, $this->config['secretKey']);
  49 + $method = 'PUT';
  50 + $signature = base64_encode(hash_hmac('sha1', "{$method}\n\n\n\n", $signKey, true));
  51 + $headers = array(
  52 + 'Authorization: QCloud ' . $this->config['secretId'] . ':' . $signature,
  53 + 'Date: ' . $date,
  54 + 'Host: ' . $this->config['bucket'] . '.cos.' . $this->config['cosRegion'] . '.myqcloud.com'
  55 + );
48 // 打开文件流 56 // 打开文件流
49 $url_path = config('filesystems.disks.upload')['root'].$path.'/'.$fileName; 57 $url_path = config('filesystems.disks.upload')['root'].$path.'/'.$fileName;
50 $fileContent = file_get_contents($url_path); 58 $fileContent = file_get_contents($url_path);
@@ -58,37 +66,25 @@ class TencentCosService extends BaseService @@ -58,37 +66,25 @@ class TencentCosService extends BaseService
58 * @method :post 66 * @method :post
59 * @time :2023/7/18 17:06 67 * @time :2023/7/18 17:06
60 */ 68 */
61 - public function http_put($url,$fileContent){ 69 + public function http_put($url,$filePath,$headers){
  70 + // 执行请求
62 $ch = curl_init(); 71 $ch = curl_init();
63 curl_setopt($ch, CURLOPT_URL, $url); 72 curl_setopt($ch, CURLOPT_URL, $url);
64 - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 73 + curl_setopt($ch, CURLOPT_PUT, true);
65 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 74 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
66 - curl_setopt($ch, CURLOPT_POSTFIELDS, $fileContent); 75 + curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  76 + curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'rb'));
  77 + curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
  78 + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
67 $response = curl_exec($ch); 79 $response = curl_exec($ch);
68 curl_close($ch); 80 curl_close($ch);
69 - // 检查上传结果  
70 - if ($response === false) {  
71 - echo '图片上传失败';  
72 - } else { 81 + // 检查响应结果
  82 + if ($response) {
73 var_dump($response); 83 var_dump($response);
74 - die();  
75 echo '图片上传成功'; 84 echo '图片上传成功';
  85 + } else {
  86 + echo '图片上传失败';
76 } 87 }
77 } 88 }
78 89
79 - /**  
80 - * @remark : 构造腾讯云 COS 授权签名  
81 - * @name :cosAuthorization  
82 - * @author :lyh  
83 - * @method :post  
84 - * @time :2023/7/18 17:10  
85 - */  
86 - public function generateSignature($path) {  
87 - $signTime = time();  
88 - $expiredTime = 0;  
89 - $plainText =  
90 - 'a='.$this->config['appId'].'&b='.$this->config['bucket']. '&k='.$this->config['secretId'].'&e='.$expiredTime.'&t='.$signTime.'&r='.rand().'&f='.$path;  
91 - $sign = base64_encode(hash_hmac('SHA1', $plainText, $this->config['secretKey'], true).$plainText);  
92 - return $sign;  
93 - }  
94 } 90 }