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 | // | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
||
15 | // +---------------------------------------------------------------------- |
||
16 | |||
17 | namespace DtApp\ThinkLibrary\service\ksyun; |
||
18 | |||
19 | use DtApp\ThinkLibrary\Service; |
||
20 | use Ks3Client; |
||
21 | use Ks3ServiceException; |
||
22 | |||
23 | /** |
||
24 | * 金山云对象存储 |
||
25 | * https://www.ksyun.com/nv/product/KS3.html |
||
26 | * Class Ks3Service |
||
27 | * @package DtApp\ThinkLibrary\service\ksyun |
||
28 | */ |
||
29 | class Ks3Service extends Service |
||
30 | { |
||
31 | /** |
||
32 | * @var |
||
33 | */ |
||
34 | private $accessKeyID, $accessKeySecret, $endpoint, $bucket; |
||
35 | |||
36 | /** |
||
37 | * @param string $accessKeyID |
||
38 | * @return $this |
||
39 | */ |
||
40 | public function accessKeyID(string $accessKeyID) |
||
41 | { |
||
42 | $this->accessKeyID = $accessKeyID; |
||
43 | return $this; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param string $accessKeySecret |
||
48 | * @return $this |
||
49 | */ |
||
50 | public function accessKeySecret(string $accessKeySecret) |
||
51 | { |
||
52 | $this->accessKeySecret = $accessKeySecret; |
||
53 | return $this; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param string $endpoint |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function endpoint(string $endpoint): self |
||
61 | { |
||
62 | $this->endpoint = $endpoint; |
||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param string $bucket |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function bucket(string $bucket): self |
||
71 | { |
||
72 | $this->bucket = $bucket; |
||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string $object |
||
78 | * @param string $filePath |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function upload(string $object, string $filePath): ?bool |
||
82 | { |
||
83 | require_once(__DIR__ . "/bin/Ks3Client.class.php"); |
||
84 | $client = new Ks3Client($this->accessKeyID, $this->accessKeySecret, $this->endpoint); |
||
85 | $content = fopen($filePath, 'rb'); |
||
86 | $args = [ |
||
87 | "Bucket" => $this->bucket, |
||
88 | "Key" => $object, |
||
89 | "Content" => [ |
||
90 | //要上传的内容 |
||
91 | "content" => $content,//可以是文件路径或者resource,如果文件大于2G,请提供文件路径 |
||
92 | "seek_position" => 0//跳过文件开头?个字节 |
||
93 | ], |
||
94 | "ACL" => "public-read",//可以设置访问权限,合法值,private、public-read |
||
95 | "ObjectMeta" => [ |
||
96 | //设置object的元数据,可以设置"Cache-Control","Content-Disposition","Content-Encoding","Content-Length","Content-MD5","Content-Type","Expires"。当设置了Content-Length时,最后上传的为从seek_position开始向后Content-Length个字节的内容。当设置了Content-MD5时,系统会在服务端进行md5校验。 |
||
97 | "Content-Type" => "binay/ocet-stream" |
||
98 | //"Content-Length"=>4 |
||
99 | ], |
||
100 | "UserMeta" => [ |
||
101 | //可以设置object的用户元数据,需要以x-kss-meta-开头 |
||
102 | "x-kss-meta-test" => "test" |
||
103 | ] |
||
104 | ]; |
||
105 | try { |
||
106 | $client->putObjectByFile($args); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
107 | return true; |
||
108 | } catch (Ks3ServiceException $e) { |
||
109 | return false; |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 |