Thinkphp6使用七牛云
短信预约 -IT技能 免费直播动态提醒
问题描述:在开发项目的时候,需要用到七牛云用以保存附件
步骤:
- 下载七牛云所需要的文件
composer require qiniu/php-sdk
-
在七牛云上注册自己的账号,获取两个密匙AK、SK和空间名称
-
新建一个配置文件qiniu.php
-
控制器代码
declare (strict_types = 1);namespace app\controller;use app\BaseController;use app\Request;use think\App;use Qiniu\Storage\UploadManager;use app\service\Qiniu as QiniuService;use think\facade\Db;use think\facade\View;class QiniuUpload extends BaseController{ protected $service; public function __construct(App $app, QiniuService $service) { parent::__construct($app); $this->service = $service; } public function index() { View::assign( [ 'token'=>$this->service->getUpToken() ] ); return view(); } public function uploadFile(Request $request) { $fileName = $request->post('file_name'); $token = $request->post('upload_token'); $image = $request->file('file');// 获取临时路径 $filePath = $image->getRealPath();// 获取文件后缀 $ext = $image->getOriginalExtension();// 上传到七牛后保存的新文件名 $newImageName = $fileName.rand(00000,99999).'.'.$ext;// 初始化UploadManager $uploadMgr = new UploadManager(); // 调用 UploadManager 的 putFile 方法进行文件的上传。 list($ret, $err) = $uploadMgr->putFile($token,$newImageName,$filePath); if ($err !== null){ dump($err); return null; }else{// 图片上传成功 $dataTpl = [ 'fname' => $fileName, 'key' => $ret['key'] ]; Db::name('qiniu')->save($dataTpl); return '上传成功'; } } public function downloadFile() { $dataList = Db::name('qiniu')->select()->toArray(); $finalArr = array(); foreach ($dataList as $data) { $tpl['name'] = $data['fname']; $tpl['key'] = $data['key']; $tpl['url'] = $this->service->getSignedUrl($data['key']); $finalArr[] = $tpl; } View::assign( ['dataList'=>$finalArr] ); return view(); } public function del(Request $request) { $key = $request->get('key'); $res = Db::name('qiniu')->where('key',$key)->delete(); if($res) { if($this->service->delete($key)) { return '都删除成功了!'; }else{ return '数据库删除成功了,但是七牛上没删除成功!'; } }else{ return '删除失败!'; } } public function allFile() { $this->service->getFileList(); }}
- Service代码
namespace app\service;use think\facade\Config;use Qiniu\Auth;use Qiniu\Config as QiniuConfig;use Qiniu\Storage\BucketManager;use const true;class Qiniu{ public function getUpToken() { $accessKey = Config::get('qiniu.access_key'); $secretKey = Config::get('qiniu.secret_key'); $bucket = Config::get('qiniu.bucket_name'); $auth = new Auth($accessKey, $secretKey); return $auth->uploadToken($bucket); } public function getSignedUrl($key) { $accessKey = Config::get('qiniu.access_key'); $secretKey = Config::get('qiniu.secret_key'); $auth = new Auth($accessKey, $secretKey); $bucket_domain = Config::get('qiniu.bucket_domain'); $baseUrl = $bucket_domain.$key; return $auth->privateDownloadUrl($baseUrl); } public function delete($key) { $accessKey = Config::get('qiniu.access_key'); $secretKey = Config::get('qiniu.secret_key'); $bucket = Config::get('qiniu.bucket_name'); $auth = new Auth($accessKey, $secretKey); $conf = new QiniuConfig(); $bucketManager = new BucketManager($auth,$conf); $err = $bucketManager->delete($bucket,$key); if ($err) { print_r($err); return 1; }else{ return 1; } } public function getFileList() { $accessKey = Config::get('qiniu.access_key'); $secretKey = Config::get('qiniu.secret_key'); $bucket = Config::get('qiniu.bucket_name'); $auth = new Auth($accessKey, $secretKey); $bucketManager = new BucketManager($auth);// 要列取文件的公共前缀 $prefix = '';// 上次列举返回的位置标记,作为本次列举的起点信息。 $marker = '';// 本次列举的条目数 $limit = 100; $delimiter = '/';// 列举文件 list($ret, $err) = $bucketManager->listFiles($bucket, $prefix, $marker, $limit, $delimiter); if ($err !== null) { echo "\n====> list file err: \n"; var_dump($err); } else { foreach ($ret['items'] as $item) { echo "
".$item['key']; }// dump($ret['items']);// if (array_key_exists('marker', $ret)) {// echo "Marker:" . $ret["marker"] . "\n";// }// echo "\nList Iterms====>\n"; dump($ret['items']); } }}
来源地址:https://blog.csdn.net/weixin_44981396/article/details/127975921
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341