CosHelper.php 1.19 KB
<?php


namespace App\Helper;


use Illuminate\Support\Str;
use Qcloud\Cos\Client;

class CosHelper
{
    protected $client;
    protected $bucket;

    public function __construct($bucket = null)
    {
        $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 = $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";
    }

}