腾讯云Cos云存储Thinkphp使用方法

1、首先在项目当中利用Composer下载Cos的包

composer require qcloud/cos-sdk-v5

2、然后查看以下案例

<?php

namespace app\qiugong\controller;

use think\facade\Db;
use think\facade\Request;

/**
 * Class Cos
 * 腾讯云Cos云存储
 * Developers:ZhouQiugong
 * @package app\home\controller
 */
class Cos extends Base
{
    // SecretId
    protected $SecretId = "xxxxx";
    // SecretKey
    protected $SecretKey = "xxxxxx";
    // Bucket
    protected $bucket = "xxxxxxx";


    /**
     * 初始化 Cos
     * Developers:ZhouQiugong
     * @return \Qcloud\Cos\Client
     */
    public function initCos()
    {
        $cosClient = new \Qcloud\Cos\Client([
            'region' => 'ap-guangzhou', //设置一个默认的存储桶地域
            'schema' => 'http', //协议头部,默认为http
            'credentials' => ['secretId' => $this->SecretId, 'secretKey' => $this->SecretKey]
        ]);
        return $cosClient;
    }


    /**
     * Base64上传cos
     * Developers:ZhouQiugong
     * @return \think\response\Json
     */
    public function upload()
    {
        // 上传Base64图片
        $imgBase64 = input('base64');

        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $imgBase64, $res)) {

            // 图片大小
            $size = file_get_contents($imgBase64);

            // 获取图片类型
            $type = $res[2];

            // 文件
            $fileName = '/' . date('Ymd') . '/' . md5(time() . rand(1000, 9999)) . '.' . $type;

            //截取data:image/png;base64, 这个逗号后的字符
            $base64_string = explode(',', $imgBase64);

            //对截取后的字符使用base64_decode进行解码
            $tmp_name = base64_decode($base64_string[1]);

            // 初始化Cos
            $cosClient = $this->initCos();

            // 拼接COS请求数据包
            $data = ['Bucket' => $this->bucket, 'Key' => $fileName, 'Body' => $tmp_name];

            // 执行Cos上传操作
            $result = $cosClient->Upload($data['Bucket'], $data['Key'], $data['Body']);

            // 将保存在Cos的图片记录保存到数据库
            Db::name('cos_files')->insert([
                'file_name' => $result['Key'],
                'url' => $result['Location'],
                'size' => strlen($size) / 1024,
                'status' => 1,
                'time' => time()
            ]);

            // 拼接地址返回前端
            $url = "https://xxx.xxx.com/" . $result['Key'];

            // 输出到前端
            return json(['status' => 200, 'msg' => '上传成功', 'url' => $url]);
        }
    }

    /**
     * Cos 删除单个文件
     * Developers:ZhouQiugong
     * @param $key
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delFile($key)
    {
        $bucket = $this->bucket;
        $cosClient = $this->initCos();
        $result = $cosClient->deleteObject(['Bucket' => $bucket, 'Key' => $key]);
        if ($result) {
            Db::name('cos_files')->where(['file_name' => $key, 'status' => 1])->delete();
            return json(['status' => 200, 'msg' => '删除成功']);
        } else {
            return json(['status' => 500, 'msg' => '删除失败']);
        }
    }


}

 

可以直接复制方法到控制器当中测试!

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。