|
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\qiniu; |
|
21
|
|
|
|
|
22
|
|
|
use DtApp\ThinkLibrary\Service; |
|
23
|
|
|
use Exception; |
|
24
|
|
|
use Qiniu\Auth; |
|
25
|
|
|
use Qiniu\Storage\UploadManager; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* 七牛云对象存储 |
|
29
|
|
|
* https://www.qiniu.com/products/kodo |
|
30
|
|
|
* Class KodoService |
|
31
|
|
|
* @package DtApp\ThinkLibrary\service\qiniu |
|
32
|
|
|
*/ |
|
33
|
|
|
class KodoService extends Service |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var |
|
37
|
|
|
*/ |
|
38
|
|
|
private $accessKey, $secretKey, $bucket; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $accessKey |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function accessKey(string $accessKey): self |
|
45
|
|
|
{ |
|
46
|
|
|
$this->accessKey = $accessKey; |
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $secretKey |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
|
|
public function secretKey(string $secretKey): self |
|
55
|
|
|
{ |
|
56
|
|
|
$this->secretKey = $secretKey; |
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $bucket |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
|
|
public function bucket(string $bucket): self |
|
65
|
|
|
{ |
|
66
|
|
|
$this->bucket = $bucket; |
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $object |
|
72
|
|
|
* @param string $filePath |
|
73
|
|
|
* @return bool|mixed |
|
74
|
|
|
* @throws Exception |
|
75
|
|
|
*/ |
|
76
|
|
|
public function upload(string $object, string $filePath) |
|
77
|
|
|
{ |
|
78
|
|
|
// 初始化签权对象 |
|
79
|
|
|
$auth = new Auth($this->accessKey, $this->secretKey); |
|
80
|
|
|
// 生成上传Token |
|
81
|
|
|
$token = $auth->uploadToken($this->bucket); |
|
82
|
|
|
// 构建 UploadManager 对象 |
|
83
|
|
|
$uploadMgr = new UploadManager(); |
|
84
|
|
|
// 调用 UploadManager 的 putFile 方法进行文件的上传。 |
|
85
|
|
|
[$ret, $err] = $uploadMgr->putFile($token, $object, $filePath); |
|
86
|
|
|
if ($err !== null) { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $ret; |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|