CosHelper.php
1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace App\Helper;
use Illuminate\Support\Str;
use Qcloud\Cos\Client;
class CosHelper
{
    protected $client;
    protected $bucket;
    public function __construct()
    {
        $secretId = env('COS_SECRET_ID'); //"云 API 密钥 SecretId";
        $secretKey = env('COS_SECRET_KEY'); //"云 API 密钥 SecretKey";
        $region = env('COS_REGION'); //设置一个默认的存储桶地域
        $cosClient = new Client(
            array(
                'region' => $region,
                'schema' => 'https', //协议头部,默认为http
                'credentials'=> array(
                    'secretId'  => $secretId ,
                    'secretKey' => $secretKey)));
        $this->client = $cosClient;
        $this->bucket = env('COS_BUCKET');
    }
    /**
     * 预览文档 ———— 同步转换为html
     * @param string $key
     * @param int $expire
     * @return string
     */
    public function getPreviewUrl(string $key, int $expire = 10) : string
    {
        $url = $this->client->getObjectUrl($this->bucket, $key, "+{$expire}minutes");
        return $url;
//        return $url . "ci-process=doc-preview&dstType=html";
    }
}