admin管理员组文章数量:1530085
目录
前言
一、安装SDK
二、项目配置
1.创建存储桶
2.密钥管理
三、项目操作代码
1:操作
总结
前言
工具:腾讯cos
框架:Think PHP6
一、安装SDK
在右上角这里点击控制台/登录账号
腾讯云 - 产业智变 云启未来
选择对象存储
选择SDK下载 对象存储 快速入门 - SDK 文档 - 文档中心 - 腾讯云
选择安装方式
vendor文件里面包含 SDK 的依赖库和一个 autoload.php 脚本
二、项目配置
1.创建存储桶
我创建的时候key未填
2.密钥管理
记录下 id 和 key
三、项目操作代码
用户在上传视频或者图片时的流程:
我们先将文件保存在本地,本地作为中转站再上传到COS
COS上传成功之后返回文件地址给我们,我们拿到地址后删除本地文件并将返回地址存入数据库
1:操作
控制器前端访问的上传接口:
<?php
declare(strict_types=1);
namespace app\index\controller\UpVideo;
use app\index\model\video; //引入数据存储表
use app\index\Token\Token; //TOKEN
use think\facade\Request; //门面类
class Index extends Token
{
/**
* remarks:上传视频
*
* @return \think\Response
*/
public function index()
{
// 获取表单上传文件
$files = request()->file();
try {
validate(['image' =>'fileSize:10240|fileExt:jpg,png,mp4|image:200,200,jpg'])
->check($files);
$path = [];
foreach ($files as $file) {
//接收文件存入public文件中的topic中生成以日期为目录名的文件
$path = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
//转义文件格式
$srcPath = \think\facade\Filesystem::getDiskConfig('public', 'root') . '/' . str_replace('\\', '/', $path);
if (!empty($srcPath)) {
//获取上传文件的后缀(这里会将文件分成地址、后缀的一维数组,取后缀的下标1)
$url = parse_url($srcPath);
$str = explode('.', $url['path']);
$ext = $str[1];
//实例化COS上传层
$Service = new Service();
//这将本地地址和后缀两个参数带过去,这里的地址是完整的本地路径
$data = $Service->index(str_replace('\\', '/', $srcPath), $ext);
//接收COS返回路径值存入数据库
if ($data != "" || $data != null) {
$content = request::only(['content']);
$videoModel = new video();
$videoModel->save([
'userid' => $this->uid,
'content' => $content['content'],
'url' => $data
]);
if ($videoModel == true) {
unlink($path); //删除本地文件
return json(['code' => 1, 'data' => $data, 'msg' => '上传成功']); //返回给前端
} else {
return $data;
}
}
}
}
} catch (\think\exception\ValidateException $e) {
return json(['code' => 0, 'msg' => $e->getMessage()]);
}
}
}
完整的路径参数是绝对路径
获取本地绝对路径:
<?php
return [
// 默认磁盘
'default' => env('filesystem.driver', 'local'),
// 磁盘列表
'disks' => [
'local' => [
'type' => 'local',
'root' => app()->getRuntimePath() . '',
],
'public' => [
// 磁盘类型
'type' => 'local',
// 磁盘路径
'root' => app()->getRootPath() . 'public',
// 磁盘路径对应的外部URL路径
'url' => 'http://' . $_SERVER['HTTP_HOST'],
// 可见性
'visibility' => 'public',
],
// 更多的磁盘配置信息
],
];
控制器Index访问的Service上传接口:
<?php
declare(strict_types=1);
namespace app\index\controller\UpVideo;
use Qcloud\Cos\Client;
class Service
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index($srcPath, $ext)
{
//引入 autoload.php 文件:根据自己的文件结构变换路径
require dirname(__FILE__) . '../../../../../vendor/autoload.php';
//配置密钥
$secretId = "SECRETID"; //替换成自己的id
$secretKey = "SECRETKEY"; //替换成自己的key
$region = "ap-chengdu"; //替换成创建储存桶时选择的区域
$cosClient = new Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'credentials' => array(
'secretId' => $secretId,
'secretKey' => $secretKey
)
)
);
### 上传文件流
try {
$bucket = 'name-**********'; //存储桶名称 格式:BucketName-APPID
$key = mt_rand(1, 100) . '.' . $ext; //此处的 key 为对象键,对象键是对象在存储桶中的唯一标识
$file = fopen($srcPath, "rb");
if ($file) {
$result = $cosClient->putObject(array(
'Bucket' => $bucket,
'Key' => $key,
'Body' => $file
));
//将路径返回到前端访问的上传接口中
return 'https://...这里放储存桶的请求域名' . $key;
print_r($result);
}
} catch (\Exception $e) {
return json_encode(["code" => 0, "msg" => $e->getMessage()]);
}
}
}
COS返回的路径格式
上传成功后的储存桶
总结
分享是温故知新
版权声明:本文标题:PHP 实现腾讯COS存储桶 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1726542812a1074886.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论