TencentCosService.php
3.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
<?php
namespace App\Services;
/**
* @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){
// 构建请求URL
$expiredTime = time() + 3600;
$pathname = '/'.$this->config['bucket'].'/'.$fileName;
$signature = $this->generateSignature($this->config['secretKey'], 'PUT', $pathname, $expiredTime);
$url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname.'?sign='.$signature;
// 构造请求方法、URL和头部
$url = 'https://' . $this->config['bucket'] . '.cos.' . $this->config['cosRegion'] . '.myqcloud.com'.$path;
$this->method = 'PUT';
$headers = array(
'Authorization: ' . $this->cosAuthorization($this->config['appId'], $this->config['secretId'], $this->config['secretKey'], $this->method, '/' . $this->config['bucket'] . $path),
'Content-Type: image/'.$image_type // 根据上传的文件类型设置相应的Content-Type
);
// 打开文件流
$url_path = config('filesystems.disks.upload')['root'].$path;
$file = fopen($url_path, 'r');
return $this->http_put($url,$headers,$file,$url_path);
}
/**
* @remark :上传文件到第三方
* @name : (http_put
* @author :lyh
* @method :post
* @time :2023/7/18 17:06
*/
public function http_put($url,$headers,$file,$url_path){
// 创建CURL句柄
$curl = curl_init();
// 设置请求方法、URL、头部和数据流
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_INFILE, $file);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($url_path));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// 发起请求
curl_exec($curl);
// 关闭文件流和CURL句柄
fclose($file);
curl_close($curl);
// 检查响应状态码
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($httpCode == 200) {
return 1;
} else {
return 2;
}
}
/**
* @remark : 构造腾讯云 COS 授权签名
* @name :cosAuthorization
* @author :lyh
* @method :post
* @time :2023/7/18 17:10
*/
public function generateSignature($secretKey, $method, $pathname, $expiredTime) {
$signTime = time();
$plainText = "a=".$secretId."&k=".$secretKey."&e=".$expiredTime."&t=".$signTime."&r=".$pathname."&f=";
$bin = hash_hmac("SHA1", $plainText, $secretKey, true);
$bin = $bin . $plainText;
$sign = base64_encode($bin);
return $sign;
}
}