Issues (1482)

src/service/tencent/CosService.php (1 issue)

Severity
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\tencent;
18
19
use DtApp\ThinkLibrary\Service;
20
use Qcloud\Cos\Client;
21
22
/**
23
 * 腾讯云对象存储
24
 * https://cloud.tencent.com/product/cos
25
 * Class CosService
26
 * @package DtApp\ThinkLibrary\service\tencent
27
 */
28
class CosService extends Service
29
{
30
    /**
31
     * @var
32
     */
33
    private $secretId, $secretKey, $region, $bucket;
34
35
    /**
36
     * @param string $secretId
37
     * @return $this
38
     */
39
    public function secretId(string $secretId): self
40
    {
41
        $this->secretId = $secretId;
42
        return $this;
43
    }
44
45
    /**
46
     * @param string $secretKey
47
     * @return $this
48
     */
49
    public function secretKey(string $secretKey): self
50
    {
51
        $this->secretKey = $secretKey;
52
        return $this;
53
    }
54
55
    /**
56
     * @param string $region
57
     * @return $this
58
     */
59
    public function region(string $region): self
60
    {
61
        $this->region = $region;
62
        return $this;
63
    }
64
65
    /**
66
     * @param string $bucket
67
     * @return $this
68
     */
69
    public function bucket(string $bucket): self
70
    {
71
        $this->bucket = $bucket;
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $object
77
     * @param string $filePath
78
     * @return bool
79
     */
80
    public function upload(string $object, string $filePath): bool
81
    {
82
        $cosClient = new Client([
83
            'region' => $this->region,
84
            'schema' => 'https',
85
            'credentials' => [
86
                'secretId' => $this->secretId,
87
                'secretKey' => $this->secretKey
88
            ]
89
        ]);
90
        $key = $object;
91
        $file = fopen($filePath, "rb");
92
        if ($file) {
0 ignored issues
show
$file is of type resource, thus it always evaluated to false.
Loading history...
93
            $cosClient->putObject([
94
                'Bucket' => $this->bucket,
95
                'Key' => $key,
96
                'Body' => $file
97
            ]);
98
            return true;
99
        }
100
101
        return false;
102
    }
103
}
104