TencentCosService.php
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace App\Services;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
/**
* @remark :腾讯cos上传
* @class :TencentCosService.php
* @author :lyh
* @time :2023/7/18 16:46
*/
class TencentCosService extends BaseService
{
public $cos = [];//cos配置
public $method = '';//请求方式
public $time = 86400;//签名有效时间
public $config = [
'cosRegion' => 'COS_REGION', // 存储桶地域
'appId' => 'COS_APP_ID', // 腾讯云应用ID
'secretId' => 'COS_SECRET_ID', // 腾讯云API的SecretId
'secretKey' => 'COS_SECRET_KEY', // 腾讯云API的SecretKey
'bucket' => 'COS_BUCKET', // 存储桶名称
];
public function __construct(){
$this->cos = config('filesystems.disks.cos');
$this->config['cosRegion'] = $this->cos['region'];
$this->config['appId'] = $this->cos['credentials']['appId'];
$this->config['secretId'] = $this->cos['credentials']['secretId'];
$this->config['secretKey'] = $this->cos['credentials']['secretKey'];
$this->config['bucket'] = $this->cos['bucket'];
}
/**
* @remark :上传图片
* @name :upload
* @author :lyh
* @method :post
* @time :2023/7/18 16:56
*/
public function upload_image($path,$fileName,$image_type){
$contentType = 'image/'.$image_type;
$httpVerb = 'PUT';
$date = gmdate('D, d M Y H:i:s \G\M\T');
//生成 KeyTime
$keyTime = ( string )( time() - 60 ) . ';' . ( string )( strtotime( '+30 minutes' ) );
//生成 SignKey
$signKey = hash_hmac( 'sha1', $keyTime, trim($this->config['secretKey']) );
//生成 UrlParamList 和 HttpParameters
$queryParameters = \request()->query();
$sortedKeys = collect($queryParameters)->keys()->sort()->all();
$urlParamList = "";
$httpParameters = "";
foreach ($sortedKeys as $key) {
$encodedKey = Str::lower(rawurlencode($key));
$value = isset($queryParameters[$key]) ? rawurlencode($queryParameters[$key]) : "";
$urlParamList .= $encodedKey . ";";
$httpParameters .= $encodedKey . "=" . $value . "&";
}
$urlParamList = rtrim($urlParamList, ";");
$httpParameters = rtrim($httpParameters, "&");
// 生成 HeaderList 和 HttpHeaders
$headers = \request()->headers->all();
$sortedKeys = collect($headers)->keys()->sort()->all();
$headerList = "";
$httpHeaders = "";
foreach ($sortedKeys as $key) {
$encodedKey = Str::lower(rawurlencode($key));
$value = rawurlencode($headers[$key][0]);
$headerList .= $encodedKey . ";";
$httpHeaders .= $encodedKey . "=" . $value . "&";
}
$headerList = rtrim($headerList, ";");
$httpHeaders = rtrim($httpHeaders, "&");
//生成HttpString
$httpMethod = Str::lower($httpVerb);
$uriPathname = $path.'/'.$fileName;
$httpString = $httpMethod . "\n" . $uriPathname . "\n" . $httpParameters . "\n" . $httpHeaders . "\n";
//生成 StringToSign
$sha1String = sha1($httpString);
$stringToSign = "sha1\n" . $keyTime . "\n" . $sha1String . "\n";
$sign = hash_hmac( 'sha1', $stringToSign, $signKey );
$authorization =
"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}";
$url = "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com{$uriPathname}";
// 打开文件流
$filePathUrl = config('filesystems.disks.upload')['root'].$path.'/'.$fileName;
return $this->http_put($url,$contentType,$authorization,$date,$filePathUrl);
}
/**
* @param Request $request
* @remark :生成$urlParamList + $httpParameters
* @name :ceshi
* @author :lyh
* @method :post
* @time :2023/7/19 14:05
*/
public function queryParameters(Request $request){
}
/**
* @remark :上传文件到第三方
* @name : (http_put
* @author :lyh
* @method :post
* @time :2023/7/18 17:06
*/
public function http_put($url,$contentType,$authorization,$date,$filePath){
// 执行请求
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: ' . $contentType,
'Authorization: ' . $authorization,
'Date: ' . $date
));
curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'r'));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
$response = curl_exec($ch);
curl_close($ch);
// 检查响应结果
if ($response) {
return 1;
} else {
return 0;
}
}
}