fastadmin 上传图片或视频至七牛云
在 app/config 文件中的最后新增 七牛配置
'qiniu' => [ "use" => 0, "domain" => 'http://cdn.wqw168.com/', "bucket" => 'ghyl', "accessKey" => 'kizAtNRPdAv26H4dNCJacScMa3D2PZ57y167W3I6', "secretKey" => 'npjZ_PcN4DaoWKiv7KUA7v0CbI81OtSh2xQqPifx', ],
在 app/common/controller 下新建 Qiniu.php 文件
<?php namespace app\common\controller; use think\Config; use Qiniu\Auth; use Qiniu\Storage\UploadManager; /** * 七牛基类 */ class Qiniu extends Backend { public function initialize() { } /** * 上传 * @param array $file 图片参数 * @return array */ public function uploadOne() { $data = $this->request->file(); $info = $data['file']->getInfo(); $domain = config("qiniu.domain"); $bucket = config("qiniu.bucket"); $auth = new Auth(config("qiniu.accessKey"), config("qiniu.secretKey")); // 生成上传Token $token = $auth->uploadToken($bucket); // 构建 UploadManager 对象 $uploadMgr = new UploadManager(); list($ret, $err) = $uploadMgr->putFile($token, $info['name'], $info['tmp_name']); if ($err !== null) { return ['code' => 0, 'msg' => '上传失败']; } else { //返回图片的完整URL return ['code' => 1, 'msg' => '上传完成', 'data' => ($domain . $ret['key'])]; } } }
然后修改上传文件接口的地方:api/common/upload,正常情况下只需修改 else 里的内容
if ($chunkid) {//这里的地方不用修改,因为这里是分片上传的 } else { if (config("qiniu.use")) { //默认普通上传文件 $qiniu = new \app\common\controller\Qiniu; $rs = $qiniu->uploadOne(); if ($rs["code"] == 0) { $this->error($rs["msg"]); } $this->success(__('Uploaded successful'), '', ['url' => $rs["data"], 'fullurl' => $rs["data"]]); } else { $attachment = null; //默认普通上传文件 $file = $this->request->file('file'); try { $upload = new Upload($file); $attachment = $upload->upload(); } catch (UploadException $e) { $this->error($e->getMessage()); } $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]); } }