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

Ks3Service::accessKeyID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
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\ksyun;
21
22
use DtApp\ThinkLibrary\Service;
23
use Ks3Client;
24
use Ks3ServiceException;
25
26
/**
27
 * 金山云对象存储
28
 * https://www.ksyun.com/nv/product/KS3.html
29
 * Class Ks3Service
30
 * @package DtApp\ThinkLibrary\service\ksyun
31
 */
32
class Ks3Service extends Service
33
{
34
    /**
35
     * @var
36
     */
37
    private $accessKeyID, $accessKeySecret, $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 $accessKeySecret
51
     * @return $this
52
     */
53
    public function accessKeySecret(string $accessKeySecret)
54
    {
55
        $this->accessKeySecret = $accessKeySecret;
56
        return $this;
57
    }
58
59
    /**
60
     * @param string $endpoint
61
     * @return $this
62
     */
63
    public function endpoint(string $endpoint): self
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): self
74
    {
75
        $this->bucket = $bucket;
76
        return $this;
77
    }
78
79
    /**
80
     * @param string $object
81
     * @param string $filePath
82
     * @return bool
83
     */
84
    public function upload(string $object, string $filePath): ?bool
85
    {
86
        //是否使用VHOST
87
        define("KS3_API_VHOST", FALSE);
88
        //是否开启日志(写入日志文件)
89
        define("KS3_API_LOG", FALSE);
90
        //是否显示日志(直接输出日志)
91
        define("KS3_API_DISPLAY_LOG", FALSE);
92
        //定义日志目录(默认是该项目log下)
93
        define("KS3_API_LOG_PATH", "");
94
        //是否使用HTTPS
95
        define("KS3_API_USE_HTTPS", FALSE);
96
        //是否开启curl debug模式
97
        define("KS3_API_DEBUG_MODE", FALSE);
98
        require_once(__DIR__ . "/bin/Ks3Client.class.php");
99
        $client = new Ks3Client($this->accessKeyID, $this->accessKeySecret, $this->endpoint);
100
        $content = fopen($filePath, 'rb');
101
        $args = [
102
            "Bucket" => $this->bucket,
103
            "Key" => $object,
104
            "Content" => [
105
                //要上传的内容
106
                "content" => $content,//可以是文件路径或者resource,如果文件大于2G,请提供文件路径
107
                "seek_position" => 0//跳过文件开头?个字节
108
            ],
109
            "ACL" => "public-read",//可以设置访问权限,合法值,private、public-read
110
            "ObjectMeta" => [
111
                //设置object的元数据,可以设置"Cache-Control","Content-Disposition","Content-Encoding","Content-Length","Content-MD5","Content-Type","Expires"。当设置了Content-Length时,最后上传的为从seek_position开始向后Content-Length个字节的内容。当设置了Content-MD5时,系统会在服务端进行md5校验。
112
                "Content-Type" => "binay/ocet-stream"
113
                //"Content-Length"=>4
114
            ],
115
            "UserMeta" => [
116
                //可以设置object的用户元数据,需要以x-kss-meta-开头
117
                "x-kss-meta-test" => "test"
118
            ]
119
        ];
120
        try {
121
            return $client->putObjectByFile($args);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $client->putObjectByFile($args) could return the type string which is incompatible with the type-hinted return boolean|null. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
The method putObjectByFile() does not exist on Ks3Client. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
            return $client->/** @scrutinizer ignore-call */ putObjectByFile($args);
Loading history...
122
        } catch (Ks3ServiceException $e) {
123
            return false;
124
        }
125
    }
126
}
127