1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------- |
4
|
|
|
// | ThinkLibrary 6.0 for ThinkPhP 6.0 |
5
|
|
|
// +---------------------------------------------------------------------- |
6
|
|
|
// | 版权所有 2017~2020 [ https://www.dtapp.net ] |
7
|
|
|
// +---------------------------------------------------------------------- |
8
|
|
|
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
9
|
|
|
// +---------------------------------------------------------------------- |
10
|
|
|
// | 开源协议 ( https://mit-license.org ) |
11
|
|
|
// +---------------------------------------------------------------------- |
12
|
|
|
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
13
|
|
|
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
14
|
|
|
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary |
15
|
|
|
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary |
16
|
|
|
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git |
17
|
|
|
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
18
|
|
|
// +---------------------------------------------------------------------- |
19
|
|
|
|
20
|
|
|
namespace DtApp\ThinkLibrary\service\tencent; |
21
|
|
|
|
22
|
|
|
use DtApp\ThinkLibrary\Service; |
23
|
|
|
use Qcloud\Cos\Client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 腾讯云对象存储 |
27
|
|
|
* https://cloud.tencent.com/product/cos |
28
|
|
|
* Class CosService |
29
|
|
|
* @package DtApp\ThinkLibrary\service\tencent |
30
|
|
|
*/ |
31
|
|
|
class CosService extends Service |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var |
35
|
|
|
*/ |
36
|
|
|
private $secretId, $secretKey, $region, $bucket; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $secretId |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function secretId(string $secretId): self |
43
|
|
|
{ |
44
|
|
|
$this->secretId = $secretId; |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $secretKey |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function secretKey(string $secretKey): self |
53
|
|
|
{ |
54
|
|
|
$this->secretKey = $secretKey; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $region |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function region(string $region): self |
63
|
|
|
{ |
64
|
|
|
$this->region = $region; |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $bucket |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function bucket(string $bucket): self |
73
|
|
|
{ |
74
|
|
|
$this->bucket = $bucket; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $object |
80
|
|
|
* @param string $filePath |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function upload(string $object, string $filePath): bool |
84
|
|
|
{ |
85
|
|
|
$cosClient = new Client([ |
86
|
|
|
'region' => $this->region, |
87
|
|
|
'schema' => 'https', |
88
|
|
|
'credentials' => [ |
89
|
|
|
'secretId' => $this->secretId, |
90
|
|
|
'secretKey' => $this->secretKey |
91
|
|
|
] |
92
|
|
|
]); |
93
|
|
|
$key = $object; |
94
|
|
|
$file = fopen($filePath, "rb"); |
95
|
|
|
if ($file) { |
|
|
|
|
96
|
|
|
return $cosClient->putObject([ |
97
|
|
|
'Bucket' => $this->bucket, |
98
|
|
|
'Key' => $key, |
99
|
|
|
'Body' => $file |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|