1-安装腾讯云SDK

composer require qcloud/cos-sdk-v5

2-腾讯云配置

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方法

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。