Passed
Push — v6 ( 0ec70f...32f4f3 )
by 光春
03:48
created

ObsService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 70
rs 10
c 1
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A bucket() 0 4 1
A secret() 0 4 1
A endpoint() 0 4 1
A key() 0 4 1
A upload() 0 18 2
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\huaweicloud;
21
22
use DtApp\ThinkLibrary\Service;
23
use Obs\ObsClient;
24
25
/**
26
 * 华为云对象存储
27
 * https://www.huaweicloud.com/product/obs.html
28
 * Class ObsService
29
 * @package DtApp\ThinkLibrary\service\huaweicloud
30
 */
31
class ObsService extends Service
32
{
33
    /**
34
     * @var
35
     */
36
    private $key, $secret, $endpoint, $bucket;
37
38
    /**
39
     * @param string $key
40
     * @return $this
41
     */
42
    public function key(string $key): self
43
    {
44
        $this->key = $key;
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $secret
50
     * @return $this
51
     */
52
    public function secret(string $secret): self
53
    {
54
        $this->secret = $secret;
55
        return $this;
56
    }
57
58
    /**
59
     * @param string $endpoint
60
     * @return $this
61
     */
62
    public function endpoint(string $endpoint): self
63
    {
64
        $this->endpoint = $endpoint;
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|\Obs\Internal\Common\Model
82
     */
83
    public function upload(string $object, string $filePath)
84
    {
85
        // 创建ObsClient实例
86
        $obsClient = new ObsClient([
87
            'key' => $this->key,
88
            'secret' => $this->secret,
89
            'endpoint' => $this->endpoint
90
        ]);
91
        $resp = $obsClient->putObject([
92
            'Bucket' => $this->bucket,
93
            'Key' => $object,
94
            'SourceFile' => $filePath  // localfile为待上传的本地文件路径,需要指定到具体的文件名
95
        ]);
96
        if (isset($resp['RequestId'])) {
97
            return $resp;
98
        }
99
100
        return false;
101
    }
102
}
103