作者 lyh

gx

@@ -40,22 +40,22 @@ class TencentCosService extends BaseService @@ -40,22 +40,22 @@ class TencentCosService extends BaseService
40 * @method :post 40 * @method :post
41 * @time :2023/7/18 16:56 41 * @time :2023/7/18 16:56
42 */ 42 */
43 - public function upload_image($path,$image_type){ 43 + public function upload_image($path,$fileName,$image_type){
  44 + // 构建请求URL
  45 + $expiredTime = time() + 3600;
  46 + $pathname = '/'.$this->config['bucket'].'/'.$fileName;
  47 + $signature = $this->generateSignature($this->config['secretKey'], 'PUT', $pathname, $expiredTime);
  48 + $url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname.'?sign='.$signature;
44 // 构造请求方法、URL和头部 49 // 构造请求方法、URL和头部
45 $url = 'https://' . $this->config['bucket'] . '.cos.' . $this->config['cosRegion'] . '.myqcloud.com'.$path; 50 $url = 'https://' . $this->config['bucket'] . '.cos.' . $this->config['cosRegion'] . '.myqcloud.com'.$path;
46 - var_dump($url);  
47 $this->method = 'PUT'; 51 $this->method = 'PUT';
48 $headers = array( 52 $headers = array(
49 'Authorization: ' . $this->cosAuthorization($this->config['appId'], $this->config['secretId'], $this->config['secretKey'], $this->method, '/' . $this->config['bucket'] . $path), 53 'Authorization: ' . $this->cosAuthorization($this->config['appId'], $this->config['secretId'], $this->config['secretKey'], $this->method, '/' . $this->config['bucket'] . $path),
50 'Content-Type: image/'.$image_type // 根据上传的文件类型设置相应的Content-Type 54 'Content-Type: image/'.$image_type // 根据上传的文件类型设置相应的Content-Type
51 ); 55 );
52 - var_dump($headers);  
53 // 打开文件流 56 // 打开文件流
54 $url_path = config('filesystems.disks.upload')['root'].$path; 57 $url_path = config('filesystems.disks.upload')['root'].$path;
55 - var_dump($url_path);  
56 $file = fopen($url_path, 'r'); 58 $file = fopen($url_path, 'r');
57 - var_dump($file);  
58 - die();  
59 return $this->http_put($url,$headers,$file,$url_path); 59 return $this->http_put($url,$headers,$file,$url_path);
60 } 60 }
61 61
@@ -97,11 +97,12 @@ class TencentCosService extends BaseService @@ -97,11 +97,12 @@ class TencentCosService extends BaseService
97 * @method :post 97 * @method :post
98 * @time :2023/7/18 17:10 98 * @time :2023/7/18 17:10
99 */ 99 */
100 - function cosAuthorization($appId, $secretId, $secretKey, $method, $path)  
101 - {  
102 - $expired = time() + $this->time; // 签名有效期:当前时间戳 + 86400秒(1天)  
103 - $toSign = 'a=' . $appId . '&b=' . $secretId . '&k=' . $secretKey . '&e=' . $expired . '&t=' . time() . '&r=' . rand() . '&f=' . $path;  
104 - $sign = base64_encode(hash_hmac('SHA1', $toSign, $secretKey, true) . $toSign); 100 + public function generateSignature($secretKey, $method, $pathname, $expiredTime) {
  101 + $signTime = time();
  102 + $plainText = "a=".$secretId."&k=".$secretKey."&e=".$expiredTime."&t=".$signTime."&r=".$pathname."&f=";
  103 + $bin = hash_hmac("SHA1", $plainText, $secretKey, true);
  104 + $bin = $bin . $plainText;
  105 + $sign = base64_encode($bin);
105 return $sign; 106 return $sign;
106 } 107 }
107 } 108 }