[php-cos]ThinkPHP项目集成腾讯云储存对象COS

Cos技术文档
在这里插入图片描述

1、安装phpSdk
通过composer的方式安装。
在这里插入图片描述
1.1 在composer.json中添加 qcloud/cos-sdk-v5: >=2.0

  "require": {
        "php": ">=7.2.5",
        "topthink/framework": "^6.1.0",
        "topthink/think-orm": "^2.0",
        "topthink/think-filesystem": "^1.0",
        "zoujingli/wechat-developer": "^1.2",
        "qcloud/cos-sdk-v5": ">=2.0"
    },

将composer.lock删除,然后运行composer install 就安装成功了。
查看vendor中的是否存在qcloud\cos-sdk-v5,安装包中有使用的例子:sample文件夹中。
在这里插入图片描述
2、添加配置文件
在这里插入图片描述
3、编写cos工具类

<?php

namespace app\common;

use think\facade\Config;
use Qcloud\Cos\Client;

class CosClient
{




    public static function uploadFile()
    {
        ### 上传文件流
        try {
            $qcloudConfig = Config::get("cosClient");
            $bucket = $qcloudConfig["bucket"]; //存储桶名称 格式:BucketName-APPID
            $key = "exampleobject"; //此处的 key 为对象键,对象键是对象在存储桶中的唯一标识
            $srcPath = "C:/Users/nima/Desktop/Snipaste_2023-07-26_14-59-39.png"; //本地文件绝对路径
            $file = fopen($srcPath, 'rb');
            if ($file) {
                $result = CosClient::cosClient()->Upload(
                    $bucket = $bucket,
                    $key = $key,
                    $body = $file
                );
            }
            print_r($result);
        } catch (\Exception $e) {
            echo "$e\n";
        }
    }

    public static function cosClient()
    {
        $qcloudConfig = Config::get("cosClient");

        $secretId = $qcloudConfig["secretId"];
        $secretKey = $qcloudConfig["secretKey"];
        $region = $qcloudConfig["region"];


        $cosClient = new Client(
            array(
                'region' => $region,
                'schema' => 'https', //协议头部,默认为http
                'credentials' => array(
                    'secretId'  => $secretId,
                    'secretKey' => $secretKey
                )
            )
        );
        return $cosClient;
    }
}

测试:

<?php

namespace app\controller;

use app\BaseController;
use think\Request;


use app\model\User;
use think\facade\Config;
use app\common\CosClient;



class CosController extends BaseController
{

    /**
     * 上传文件cos
     */
    public function cosUploadFile()
    {
        CosClient::uploadFile();
    }

}

postman请求:

在这里插入图片描述
成功报错:

GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem: self signed certificate in certificate
chain (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for
https://shushan-1259593927.cos.ap-nanjing.myqcloud.com/exampleobject in
F:\shushan-server\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:211
Stack trace:
#0 F:\shushan-server\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(158):
GuzzleHttp\Handler\CurlFactory::createRejection()
#1 F:\shushan-server\vendor\guzzlehttp\guzzle\src\Handler\Curl

按照报错指示,查看https://curl.haxx.se/libcurl/c/libcurl-errors.html找到解决办法:
下载pem证书,配置证书。

curl pem证书下载地址

在这里插入图片描述
将证书保存在:D:/phpstudy_pro/Extensions/php/php8.0.2nts/extras/ssl/cacert.pem
在这里插入图片描述
打开PHP.ini配置文件:配置证书:curl.cainfo
在这里插入图片描述
重启小皮
在这里插入图片描述
再次postman访问:成功了!

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科学熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值