当前位置: 首页 > news >正文

[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工具类

<?phpnamespace 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;}
}

测试:

<?phpnamespace 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访问:成功了!

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

http://www.lryc.cn/news/97738.html

相关文章:

  • DuckDB全面挑战SQLite
  • Elasticsearch查询裁剪
  • Hadoop——Hive运行环境搭建
  • (vue)vue项目中引入外部字体
  • ChatGPT在语义理解和信息提取中的应用如何?
  • Mysql-主从复制与读写分离
  • 算法练习(3):牛客在线编程04 堆/栈/队列
  • mac下安装vue cli脚手架并搭建一个简易项目
  • 尝试-InsCode Stable Diffusion 美图活动一期
  • 【OpenGL学习】之着色器GLSL基础
  • Python爬虫基础知识点有哪些
  • 【CSS】 vh、rem 和 px 的区别
  • 如何设置板子从emmc启动-针对imx6ull
  • 使用Newtonsoft直接读取Json格式文本(Linq to Json)
  • 服务器用友数据库中了locked勒索病毒后怎么解锁数据恢复
  • Linux-MariaDB数据库的备份与初始化
  • springboot-redis使用fastjson2
  • SOC FPGA之HPS模型设计(二)
  • Go基础—反射,性能和灵活性的双刃剑
  • MATLAB与ROS联合仿真(慕羽☆)全套开源资料索引
  • 三、深入浅出WPF之控件与布局
  • 社群积分运营策略:增加用户忠诚度
  • 推荐用于学习RN原生模块开发的开源库—react-native-ble-manager
  • MySQL中锁的简介——全局锁
  • RocketMQ集群4.9.2升级4.9.6版本
  • 具身智能controller---RT-1(Robotics Transformer)(上---方法介绍)
  • 视频内存过大如何压缩变小?这个压缩方法了解一下
  • 【Ansible】自动化部署工具-----Ansible
  • Ubuntu下安装Node.js;npm
  • 设计模式-模版方法模式