composer下载:
"require": {
"qcloud/cos-sdk-v5": ">=2.0"
}
composer安装
php composer.phar install
修改 config/filesystem.php
'disks' => [
'local' => [
'type' => 'local',
'root' => app()->getRootPath() . 'storage'
]
public function fileupload(Request $request)
{
$file=$request->file("file");
$savename = \think\facade\Filesystem::putFile( 'topic', $file);
$data=Cos::getCos($savename);
return json(['error_code'=>0,'data'=>$data,'msg'=>'ok']);
}
server/Cos.php
<?php
namespace app\api\server;
use app\Request;
use Qcloud\Cos\Client;
class Cos
{
public static function getCos($savename)
{
//腾讯云cos
$secretId = ""; //"云 API 密钥 SecretId";
$secretKey = ""; //"云 API 密钥 SecretKey";
$region = "ap-shanghai"; //设置一个默认的存储桶地域
$cosClient = new Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'credentials' => array(
'secretId' => $secretId,
'secretKey' => $secretKey)
)
);
//设置一个对象键(尽量不包含特殊符号)
$keyv = rand(0,99999999);
# 上传文件
### 上传文件流
try {
$bucket = ""; //存储桶名称 格式:BucketName-APPID
$key = $keyv; //此处的 key 为对象键,对象键是对象在存储桶中的唯一标识
$srcPath = 'D:\phpstudy_pro\WWW\picurlstorage/'.$savename;//本地文件绝对路径
$file = fopen($srcPath,'rb');
if ($file) {
$cosClient->Upload(
$bucket = $bucket,
$key = $key,
$body = $file
);
}
} catch (\Exception $e) {
return $e;
}
//拼接存储路径
$path = '网址'.$key;
return $path;
}
全程粘就好