一、安装扩展包
composer require "overtrue/laravel-filesystem-cos"
二、 修改配置文件 config/filesystems.php
把cos相关配置追加到disks中去:
<?php return [ /* |-------------------------------------------------------------------------- | Default Filesystem Disk |-------------------------------------------------------------------------- | | Here you may specify the default filesystem disk that should be used | by the framework. A "local" driver, as well as a variety of cloud | based drivers are available for your choosing. Just store away! | | Supported: "local", "ftp", "s3", "rackspace" | */ 'default' => env('STORAGE','local'), /* |-------------------------------------------------------------------------- | Default Cloud Filesystem Disk |-------------------------------------------------------------------------- | | Many applications store files both locally and in the cloud. For this | reason, you may specify a default "cloud" driver here. This driver | will be bound as the Cloud disk implementation in the container. | */ 'cloud' => 's3', /* |-------------------------------------------------------------------------- | Filesystem Disks |-------------------------------------------------------------------------- | | Here you may configure as many filesystem "disks" as you wish, and you | may even configure multiple disks of the same driver. Defaults have | been setup for each driver as an example of the required options. | */ 'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('uploads'), ], 'ftp' => [ 'driver' => 'ftp', 'host' => 'ftp.example.com', 'username' => 'your-username', 'password' => 'your-password', ], 'qiniu' => [ 'driver' => 'qiniu', 'domain' => env('QINIU_DOMAIM',''), 'domains' => [ 'default' => env('QINIU_DOMAIM',''), //你的七牛域名 'https' => 'dn-yourdomain.qbox.me', //你的HTTPS域名 'custom' => 'static.abc.com', //你的自定义域名 ], 'access_key'=> '', //AccessKey 'secret_key'=> '', //SecretKey 'bucket' => env('BUCKET',''), //Bucket名字 'notify_url'=> '', //持久化处理回调地址 ], 'cos' => [ 'driver' => 'cos', 'region' => env('COS_REGION'), 'credentials' => [ 'appId' => env('COS_APP_ID'), 'secretId' => env('COS_SECRET_ID'), 'secretKey' => env('COS_SECRET_KEY'), ], 'timeout' => env('COS_TIMEOUT', 60), 'connect_timeout' => env('COS_CONNECT_TIMEOUT', 60), 'bucket' => env('COS_BUCKET'), 'cdn' => env('RESOURCE_URL'), 'scheme' => env('COS_SCHEME', 'https'), 'read_from_cdn' => env('COS_READ_FROM_CDN', false), ], ], ];
三、在.env文件中添加cos腾讯云连接配置。
COS_REGION="您的REGION"
COS_APPID="您的APPID"
COS_SECRET_ID="您的SECRET_ID"
COS_SECRET_KEY="您的SECRET_KEY"
COS_BUCKET="您的BUCKET"
RESOURCE_URL="您的访问域名"
注意:如果缓存过配置文件,记得清除一下缓存再重新生成
php artisan config:cache
四、 上传案例(文件流或者图片路径两种方式上传)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UploadImgController extends \Illuminate\Routing\Controller
{
/**
* 文件流或文件路径上传图片
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function uploadImg(Request $request){
//type: 1:文件流上传方式。2:文件路径上传方式。
$type = $request->input('type');
$file = $request->file('file'); //文件流
$path = $request->input('path'); //文件路径
if(!$type){
return response()->json(['status' => 1, 'msg' => '缺少参数', 'data' => (object)[]]);
}
if($type == 1){
if(!$file){
return response()->json(['status' => 1, 'msg' => '缺少参数', 'data' => (object)[]]);
}
$extension = $file->getClientOriginalExtension(); //获取文件扩展名
if(!in_array($extension,["png", "jpg", "gif", 'jpeg'])){
return response()->json(['status' => 1, 'message' => '图片上传类型需要是png,jpg,jpeg或者是gif','data'=> (object)[]]);
}
$path = $file->getRealPath();
$sc = '/'.rand(11111,99999).'/'.time();
}else{
if(!$path){
return response()->json(['status' => 1, 'msg' => '缺少参数', 'data' => (object)[]]);
}
$sc = '/'.rand(11111,99999).'/'.time();
}
$fileName = config('filesystems.disks.cos.storage_url').$sc.'.'.$extension;
$cloudConfig = config('filesystems.disks.cos');
//腾讯云Cos存储
$cosClient = new \Qcloud\Cos\Client([
'region' => $cloudConfig['region'],
'credentials' => [
'appId' => $cloudConfig['credentials']['appId'],
'secretId' => $cloudConfig['credentials']['secretId'],
'secretKey' => $cloudConfig['credentials']['secretKey']
]
]);
try {
$result = $cosClient->putObject([
'Bucket' => $cloudConfig['bucket'], // 存储桶
'Key' => $fileName, //文件名
'Body' => fopen($path, 'rb'), // 图片
]);
return response()->json(['status' => 0, 'msg' => '图片上传成功','data'=> ['url'=>config('filesystems.disks.cos.url').$fileName,'path'=>$fileName]]);
} catch (\Exception $e) {
//日志记录
LogRecord('logs/image.log','uploadImg',['msg'=>$e->getMessage()]);
return response()->json(['status' => 1, 'msg' => '图片上传失败','data'=> (object)[]]);
}
}
}