作者 lyh

gx

@@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
2 2
3 namespace App\Services; 3 namespace App\Services;
4 4
  5 +use Illuminate\Http\Request;
  6 +
5 /** 7 /**
6 * @remark :腾讯cos上传 8 * @remark :腾讯cos上传
7 * @class :TencentCosService.php 9 * @class :TencentCosService.php
@@ -41,19 +43,21 @@ class TencentCosService extends BaseService @@ -41,19 +43,21 @@ class TencentCosService extends BaseService
41 * @time :2023/7/18 16:56 43 * @time :2023/7/18 16:56
42 */ 44 */
43 public function upload_image($path,$fileName,$image_type){ 45 public function upload_image($path,$fileName,$image_type){
44 - // 构建请求URL  
45 $httpVerb = 'PUT'; 46 $httpVerb = 'PUT';
46 - $contentMd5 = '';  
47 - $contentType = 'image/'.$image_type;  
48 - date_default_timezone_set('Asia/Shanghai');  
49 - $date = date('D, d M Y H:i:s \G\M\T');  
50 - $time = time();  
51 - $expiredTime = time() + 3600000; 47 + $date = gmdate('D, d M Y H:i:s \G\M\T');
  48 + //生成 KeyTime
  49 + $signTime = ( string )( time() - 60 ) . ';' . ( string )( strtotime( '+30 minutes' ) );
  50 + //生成 SignKey
  51 + $signKey = hash_hmac( 'sha1', $signTime, trim($this->config['secretKey']) );
  52 + //生成 UrlParamList 和 HttpParameters
  53 +
52 $filePath = $path.'/'.$fileName; 54 $filePath = $path.'/'.$fileName;
53 - $stringToSign = "{$httpVerb}\n{$contentMd5}\n{$contentType}\n{$date}\n/{$this->config['bucket']}/{$filePath}";  
54 - $sign = base64_encode(hash_hmac('sha1', $stringToSign, $this->config['secretKey'], true)); 55 + $stringToSign = "{$httpVerb}\n\n\n{$date}\n/{$this->config['bucket']}/{$filePath}";
  56 + $sign = hash_hmac( 'sha1', $stringToSign, $signKey );
  57 + var_dump($sign);
  58 + die();
55 $authorization = 59 $authorization =
56 - "q-sign-algorithm=sha1&q-ak={$this->config['secretId']}&q-sign-time={$time};{$expiredTime}&q-key-time={$time};{$expiredTime}&q-header-list=&q-url-param-list=&q-signature={$sign}"; 60 + "q-sign-algorithm=sha1&q-ak={$this->config['secretId']}&q-sign-time={$signTime}&q-key-time={$signTime}&q-header-list=&q-url-param-list=&q-signature={$sign}";
57 $url = "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com{$filePath}"; 61 $url = "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com{$filePath}";
58 // 打开文件流 62 // 打开文件流
59 $filePathUrl = config('filesystems.disks.upload')['root'].$path.'/'.$fileName; 63 $filePathUrl = config('filesystems.disks.upload')['root'].$path.'/'.$fileName;