问题描述:在开发项目的时候,需要用到七牛云用以保存附件
步骤:
1、执行下面命令下载七牛云所需要的文件
composer require qiniu/php-sdk
2、在七牛云上注册自己的账号,获取两个密匙AK、SK和空间名称
3、在config目录中新建一个配置文件qiniu.php
4、控制器代码
<?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();
}
/**
* 上传接口
* @param Request $request
* @return string|null
* @throws \Exception
*/
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();
}
}
5、Service代码
<?php
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 "<br/>".$item['key'];
}
// dump($ret['items']);
// if (array_key_exists('marker', $ret)) {
// echo "Marker:" . $ret["marker"] . "\n";
// }
// echo "\nList Iterms====>\n";
dump($ret['items']);
}
}
}