1分钟部署网站📞AI智能客服,大模型训练自有数据,简单好用,有效降低客服成本 广告
[toc] # 腾讯cos插件后台操作工具类 > 此工具类的特色是同步的fastadmin腾讯cos插件的配置, 并且沿用的是插件自带的sdk,不需要composer再去下载一个sdk,物尽其用 代码片:https://gist.github.com/edk24/329b63df72774ad8b97f0664df0ff1a9 ## 代码 位置:admin/library/QcloudCosUtil.php ```php <?php namespace app\admin\library; use app\common\model\Attachment; use think\Loader; /** * fastadmin 腾讯云cos后台上传工具类 (依赖cos插件) * * @author 余小波 <1421926943@qq.com> * @desc SDK:https://packagist.org/packages/qcloud/cos-sdk-v5 */ class QcloudCosUtil { /** * cos上传配置 * * @var array */ protected $config = array(); /** * 插件目录,默认空 * * @var string */ protected $addons_path = ''; /** * cos客户端 * * @var \Qcloud\Cos\Client|null */ protected $cos_client = null; public function __construct() { if (!class_exists('\Qcloud\Cos\Client')) { Loader::addNamespace('Qcloud\Cos', $this->addons_path . str_replace('/', DS, 'library/Qcloud/Cos/')); } if (!class_exists('\GuzzleHttp\Command\Command')) { Loader::addNamespace('GuzzleHttp\Command', $this->addons_path . str_replace('/', DS, 'library/Guzzle/command/src/')); } if (!class_exists('\GuzzleHttp\Command\Guzzle\Description')) { Loader::addNamespace('GuzzleHttp\Command\Guzzle', $this->addons_path . str_replace('/', DS, 'library/Guzzle/guzzle-services/src/')); } $this->config = get_addon_config('cos'); $this->cos_client = new \Qcloud\Cos\Client(array( 'region' => $this->config['region'], 'credentials' => array( 'secretId' => $this->config['secretId'], 'secretKey' => $this->config['secretKey'], ) )); } /** * 上传文件 (最大5GB) * * @param string $key * @param string|resource $body 二进制 或者 fopen(<file>, 'rb') * @return GuzzleHttp\Command\Result|null * @throws \Exception */ public function putObject($key, $body) { $result = $this->cos_client->putObject(array( 'Bucket' => $this->config['bucket'], 'Key' => $key, 'Body' => $body)); return $result; } /** * 上传文件 [使用分块上传] (最大50T) * * @param string $key * @param string|resource $body 二进制 或者 fopen(<file>, 'rb') * @return \GuzzleHttp\Command\Result|null * @throws \Exception */ public function upload($key, $body) { $result = $this->cos_client->Upload($this->config['bucket'], $key, $body); return $result; } /** * fast文件上传 (遵循fast保存路径规则 最大文件限制50T) * * @param string $filename * @return \GuzzleHttp\Command\Result|null */ public function fastUpload($filename) { if (file_exists($filename) == false) { throw new \think\Exception('操作失败,文件不存在'); } $md5 = md5_file($filename); $size = filesize($filename); $name = pathinfo($filename, PATHINFO_FILENAME); $suffix = pathinfo($filename, PATHINFO_EXTENSION); $type = mime_content_type($filename); $imagewidth = 0; $imageheight = 0; if (substr($type, 0, 5) == 'image') { $imageinfo = getimagesize($filename); if ($imageinfo) { $imagewidth = $imageinfo[0]; $imageheight = $imageinfo[1]; } } // /uploads/{year}{mon}{day}/{filemd5}{.suffix} $url = str_replace('{year}', date('Y'), $this->config['savekey']); $url = str_replace('{mon}', date('m'), $url); $url = str_replace('{day}', date('d'), $url); $url = str_replace('{filemd5}', $md5, $url); $url = str_replace('{.suffix}', '.' . $suffix, $url); $result = $this->upload($url, fopen($filename, 'rb')); if ($result == null) { return false; } // 保存到附件中 $attachment = Attachment::where('url', $url)->where('storage', 'cos')->find(); if (!$attachment) { $params = array( 'admin_id' => (int)session('admin.id'), 'user_id' => (int)cookie('uid'), 'filesize' => $size, 'filename' => $name . '.' . $suffix, 'imagewidth' => $imagewidth, 'imageheight' => $imageheight, 'imagetype' => $suffix, 'imageframes' => 0, 'mimetype' => $type, 'url' => $url, 'uploadtime' => time(), 'storage' => 'cos', 'sha1' => $md5, ); Attachment::create($params); } return $result; } } ``` ## 使用方法 ```php $cos = new QcloudCosUtil(); $res = $cos->fastUpload('/home/yuxiaobo/Desktop/bd.zip'); var_dump($res); ```