1-安装腾讯云SDK

composer require qcloud/cos-sdk-v5

2-腾讯云配置

<?php

namespace app\common\controller;

use Qcloud\Cos\Client;
use think\Controller;
use think\Db;

class Tencent extends Controller
{
    /**
     * 上传文件
     * @param $config
     * @param $key
     * @return array
     */
    public function uploadToTencentCloud( $config,$key = null) {
        $secretId = $config['tencent_secretid'];
        $secretKey = $config['tencent_secretkey'];
        $region = $config['tencent_area']; // 例如 ap-beijing, ap-guangzhou
        $bucket = $config['tencent_bucket'];
        $data = $this->request->file();
        $info = $data['file']->getInfo();
        $parts = explode('.', $info['name']);
        $extension = end($parts);
        $filename=hash('md5', uniqid()).mt_rand(1,99).'.'.$extension;
        $cosClient = new Client([
            'region' => $region,
            'credentials' => [
                'secretId'  => $secretId,
                'secretKey' => $secretKey
            ],
        ]);
        try {
            $result = $cosClient->putObject([
                'Bucket' => $bucket,
                'Key' => $key ?: $filename,
                'Body' => fopen($info['tmp_name'], 'rb')
            ]);
            if ($result!==false) {
                $domain=$config['tencent_domain'];
                Db::name('attachment')->insert([
                    'filesize'    => $info['size'],
                    'imagetype'   => $info['type'],
                    'imageframes' => 0,
                    'mimetype'    => $info['type'],
                    'filename'    => $filename,
                    'url'         => $result['Key'],
                    'createtime'  => time(),
                    'updatetime'  => time(),
                    'uploadtime'  => time(),
                    'storage'     => 'tencent',
                    'sha1'        => '',
                    'type'        => 3,
                    'type_url'    => $domain,
                    'extparam'    => '',
                ]);
                return ['code' => 1, 'msg' => '上传完成', 'data' => ($domain . $result['Key'])];
            }
            return ['code' => 0, 'msg' => '上传失败'];
        } catch (\Exception $e) {
            // 异常处理
            return ['code' => 0, 'msg' => $e->getMessage()];
        }
    }

    public function deleteOne($config,$key = null)
    {
        $region = $config['tencent_area']; // 例如 ap-beijing, ap-guangzhou
        $secretId = $config['tencent_secretid'];
        $secretKey = $config['tencent_secretkey'];
        $bucket = $config['tencent_bucket'];
        // 初始化COS客户端
        $cosClient = new Client([
            'region' => $region,
            'credentials' => [
                'secretId'  => $secretId,
                'secretKey' => $secretKey
            ],
        ]);

        try {
            // 删除文件
            $result = $cosClient->deleteObject(array(
                'Bucket' => $bucket,
                'Key' => $key // 要删除的文件对象键,例如:'exampleobject.txt'
            ));
            Checking::writeLog(json_encode($result),'删除成功','tencent.log');
            // 输出结果
        } catch (\Exception $e) {
            // 输出错误信息
            Checking::writeLog($e->getMessage(),'删除出错','tencent.log');
        }
    }
}

接下来修改fastadmin 上传文件  api/controller/Common.php 文件下的 upload 方法

然后修改附件选择器 admin/controller/general/Attachment.php 下的index方法

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部