|
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\baidu; |
|
21
|
|
|
|
|
22
|
|
|
use BaiduBce\Services\Bos\BosClient; |
|
23
|
|
|
use DtApp\ThinkLibrary\Service; |
|
24
|
|
|
use Exception; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* 百度云对象存储 |
|
28
|
|
|
* https://cloud.baidu.com/product/bos.html |
|
29
|
|
|
* Class BosService |
|
30
|
|
|
* @package DtApp\ThinkLibrary\service\baidu |
|
31
|
|
|
*/ |
|
32
|
|
|
class BosService extends Service |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var |
|
36
|
|
|
*/ |
|
37
|
|
|
private $accessKeyId, $secretAccessKey, $endpoint, $bucket; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $accessKeyId |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function accessKeyId(string $accessKeyId) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->accessKeyId = $accessKeyId; |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $secretAccessKey |
|
51
|
|
|
* @return $this |
|
52
|
|
|
*/ |
|
53
|
|
|
public function secretAccessKey(string $secretAccessKey) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->secretAccessKey = $secretAccessKey; |
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $endpoint |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function endpoint(string $endpoint) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->endpoint = $endpoint; |
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $bucket |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
|
public function bucket(string $bucket) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->bucket = $bucket; |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $object |
|
81
|
|
|
* @param string $filePath |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
* @throws Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
public function upload(string $object, string $filePath) |
|
86
|
|
|
{ |
|
87
|
|
|
// 设置BosClient的Access Key ID、Secret Access Key和ENDPOINT |
|
88
|
|
|
$BOS_TEST_CONFIG = array( |
|
89
|
|
|
'credentials' => array( |
|
90
|
|
|
'accessKeyId' => $this->accessKeyId, |
|
91
|
|
|
'secretAccessKey' => $this->secretAccessKey, |
|
92
|
|
|
), |
|
93
|
|
|
'endpoint' => $this->endpoint, |
|
94
|
|
|
); |
|
95
|
|
|
$client = new BosClient($BOS_TEST_CONFIG); |
|
96
|
|
|
// 从文件中直接上传Object |
|
97
|
|
|
return $client->putObjectFromFile($this->bucket, $object, $filePath); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|