作者 lyh

gx

@@ -36,94 +36,30 @@ class TencentCosService extends BaseService @@ -36,94 +36,30 @@ class TencentCosService extends BaseService
36 $this->config['bucket'] = $this->cos['bucket']; 36 $this->config['bucket'] = $this->cos['bucket'];
37 } 37 }
38 38
39 - /**  
40 - * @remark :上传图片  
41 - * @name :upload  
42 - * @author :lyh  
43 - * @method :post  
44 - * @time :2023/7/18 16:56  
45 - */  
46 - public function upload_image($path,$fileName,$image_type){  
47 - $contentType = 'image/'.$image_type;  
48 - $httpVerb = 'PUT';  
49 - $date = gmdate('D, d M Y H:i:s \G\M\T');  
50 - //生成 KeyTime  
51 - $keyTime = ( string )( time() - 60 ) . ';' . ( string )( strtotime( '+30 minutes' ) );  
52 - //生成 SignKey  
53 - $signKey = hash_hmac( 'sha1', $keyTime, trim($this->config['secretKey']) );  
54 - //生成 UrlParamList 和 HttpParameters  
55 - $queryParameters = \request()->query();  
56 - $sortedKeys = collect($queryParameters)->keys()->sort()->all();  
57 - $urlParamList = "";  
58 - $httpParameters = "";  
59 - foreach ($sortedKeys as $key) {  
60 - $encodedKey = Str::lower(rawurlencode($key));  
61 - $value = isset($queryParameters[$key]) ? rawurlencode($queryParameters[$key]) : "";  
62 - $urlParamList .= $encodedKey . ";";  
63 - $httpParameters .= $encodedKey . "=" . $value . "&"; 39 + public function uploadToCos($file,$path,$filename)
  40 + {
  41 + $objectKey = $path.'/',$filename;
  42 + // 上传到腾讯云 COS
  43 + $httpClient = new Client();
  44 + $response = $httpClient->request('PUT', "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com/{$objectKey}", [
  45 + 'body' => file_get_contents($file->getRealPath()),
  46 + 'headers' => $this->generateHeaders($this->config['secretId'], $this->config['secretKey']),
  47 + ]);
  48 + // 若上传成功,则返回上传后的 URL
  49 + if ($response->getStatusCode() === 200) {
  50 + return "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com/{$objectKey}";
64 } 51 }
65 - $urlParamList = rtrim($urlParamList, ";");  
66 - $httpParameters = rtrim($httpParameters, "&");  
67 - // 生成 HeaderList 和 HttpHeaders  
68 - $headers = \request()->headers->all();  
69 - $sortedKeys = collect($headers)->keys()->sort()->all();  
70 - $headerList = "";  
71 - $httpHeaders = "";  
72 - foreach ($sortedKeys as $key) {  
73 - $encodedKey = Str::lower(rawurlencode($key));  
74 - $value = rawurlencode($headers[$key][0]);  
75 - $headerList .= $encodedKey . ";";  
76 - $httpHeaders .= $encodedKey . "=" . $value . "&";  
77 - }  
78 - $headerList = rtrim($headerList, ";");  
79 - $httpHeaders = rtrim($httpHeaders, "&");  
80 - //生成HttpString  
81 - $httpMethod = Str::lower($httpVerb);  
82 - $uriPathname = \request()->getPathInfo();  
83 - $httpString = $httpMethod . "\n" . $uriPathname . "\n" . $httpParameters . "\n" . $httpHeaders . "\n";  
84 - //生成 StringToSign  
85 - $sha1String = sha1($httpString);  
86 - $stringToSign = "sha1\n" . $keyTime . "\n" . $sha1String . "\n";  
87 - $filePath = $path.'/'.$fileName;  
88 - $sign = hash_hmac( 'sha1', $stringToSign, $signKey );  
89 - $authorization =  
90 - "q-sign-algorithm=sha1&q-ak={$this->config['secretId']}&q-sign-time={$keyTime}&q-key-time={$keyTime}&q-header-list=&q-url-param-list=&q-signature={$sign}";  
91 - $url = "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com{$filePath}";  
92 - // 打开文件流  
93 - $filePathUrl = config('filesystems.disks.upload')['root'].$path.'/'.$fileName;  
94 - return $this->http_put($url,$contentType,$authorization,$date,$filePathUrl); 52 + return null;
95 } 53 }
96 54
97 - /**  
98 - * @remark :上传文件到第三方  
99 - * @name : (http_put  
100 - * @author :lyh  
101 - * @method :post  
102 - * @time :2023/7/18 17:06  
103 - */  
104 - public function http_put($url,$contentType,$authorization,$date,$filePath){  
105 - // 执行请求  
106 - $ch = curl_init($url);  
107 - curl_setopt($ch, CURLOPT_PUT, true);  
108 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
109 - curl_setopt($ch, CURLOPT_HTTPHEADER, array(  
110 - 'Content-Type: ' . $contentType,  
111 - 'Authorization: ' . $authorization,  
112 - 'Date: ' . $date  
113 - ));  
114 - curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'r'));  
115 - curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));  
116 -  
117 - $response = curl_exec($ch);  
118 - curl_close($ch);  
119 - // 检查响应结果  
120 - if ($response) {  
121 - var_dump($response);  
122 - die();  
123 - return 1;  
124 - } else {  
125 - return 0;  
126 - } 55 + private function generateHeaders($secretId, $secretKey)
  56 + {
  57 + $keyTime = time() - 1 . ';' . (time() + 300);
  58 + $signKey = hash_hmac('sha1', $keyTime, $secretKey);
  59 + $signature = hash_hmac('sha1', "sha1\n{$keyTime}\n\n", $signKey);
  60 + return [
  61 + 'Authorization' => 'q-sign-algorithm=sha1&q-ak=' . $secretId . '&q-sign-time=' . $keyTime
  62 + . '&q-key-time=' . $keyTime . '&q-signature=' . $signature,
  63 + ];
127 } 64 }
128 -  
129 } 65 }