CosService.php 2.9 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
李光春's avatar
李光春 已提交
14 15 16
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
李光春's avatar
李光春 已提交
17 18 19 20 21 22 23 24 25 26
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

namespace DtApp\ThinkLibrary\service\tencent;

use DtApp\ThinkLibrary\Service;
use Qcloud\Cos\Client;

/**
 * 腾讯云对象存储
李光春's avatar
李光春 已提交
27
 * https://cloud.tencent.com/product/cos
李光春's avatar
李光春 已提交
28 29 30 31 32
 * Class CosService
 * @package DtApp\ThinkLibrary\service\tencent
 */
class CosService extends Service
{
李光春's avatar
李光春 已提交
33 34 35
    /**
     * @var
     */
李光春's avatar
李光春 已提交
36
    private $secretId, $secretKey, $region, $bucket;
李光春's avatar
李光春 已提交
37

李光春's avatar
李光春 已提交
38 39 40 41 42
    /**
     * @param string $secretId
     * @return $this
     */
    public function secretId(string $secretId): self
李光春's avatar
李光春 已提交
43 44 45 46 47
    {
        $this->secretId = $secretId;
        return $this;
    }

李光春's avatar
李光春 已提交
48 49 50 51 52
    /**
     * @param string $secretKey
     * @return $this
     */
    public function secretKey(string $secretKey): self
李光春's avatar
李光春 已提交
53 54 55 56 57
    {
        $this->secretKey = $secretKey;
        return $this;
    }

李光春's avatar
李光春 已提交
58 59 60 61 62
    /**
     * @param string $region
     * @return $this
     */
    public function region(string $region): self
李光春's avatar
李光春 已提交
63 64 65 66 67
    {
        $this->region = $region;
        return $this;
    }

李光春's avatar
李光春 已提交
68 69 70 71 72
    /**
     * @param string $bucket
     * @return $this
     */
    public function bucket(string $bucket): self
李光春's avatar
李光春 已提交
73 74 75 76 77 78
    {
        $this->bucket = $bucket;
        return $this;
    }

    /**
李光春's avatar
李光春 已提交
79 80
     * @param string $object
     * @param string $filePath
李光春's avatar
李光春 已提交
81 82
     * @return bool
     */
李光春's avatar
李光春 已提交
83
    public function upload(string $object, string $filePath): bool
李光春's avatar
李光春 已提交
84
    {
李光春's avatar
李光春 已提交
85 86
        $cosClient = new Client([
            'region' => $this->region,
李光春's avatar
李光春 已提交
87
            'schema' => 'https',
李光春's avatar
李光春 已提交
88 89 90 91 92
            'credentials' => [
                'secretId' => $this->secretId,
                'secretKey' => $this->secretKey
            ]
        ]);
李光春's avatar
李光春 已提交
93 94
        $key = $object;
        $file = fopen($filePath, "rb");
李光春's avatar
李光春 已提交
95 96
        if ($file) {
            return $cosClient->putObject([
李光春's avatar
李光春 已提交
97 98 99 100
                'Bucket' => $this->bucket,
                'Key' => $key,
                'Body' => $file
            ]);
李光春's avatar
李光春 已提交
101
        }
李光春's avatar
李光春 已提交
102 103

        return false;
李光春's avatar
李光春 已提交
104 105
    }
}