CosService.php 2.7 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?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
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

namespace DtApp\ThinkLibrary\service\tencent;

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

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

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

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

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

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

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

        return false;
李光春's avatar
李光春 已提交
102 103
    }
}