七牛云存储PHP SDK的使用

这篇文章是对七牛云PHP SDK用法最基础的讲解,其实官方文档中已经讲得非常详细了,在此,只是演示如何将SDK引入到框架,并进行调用。

下载SDK

到 Thinkphp5 框架根目录通过Composer进行下载:

composer require qiniu/php-sdk

完成后,vendor下会多出qiniu目录,下载成功了。

使用SDK

前端模板上传文件,后台接受文件后可调用SDK上传到七牛云。通过删除的文件名,可删除文件。

<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{    
    const BUCKET = '空间名';
    const DOMAIN = '空间对应的域名';

    public function index()
    {
        return $this -> fetch();
    }

    //上传
    public function upload()
    {
        if(request() -> isPost()){
            $title = input('post.title');
            $file = request()->file('img');
            $file_info = $file -> getInfo();
            $qiniu = new UploadFile(self::DOMAIN,self::BUCKET);
            $res = $qiniu -> uploadOne($file_info);
            dump($res);
        }else{
            return $this -> fetch();
        }
    }

    //删除
    public function delete()
    {
        if(request() -> isPost()){
            $name = input('post.file_name');
            $isImage = preg_match('/.*(\.png|\.jpg|\.jpeg|\.gif)$/', $name);
            if(!$isImage){
                echo '不是图片格式';
                exit();
            }
            $qiniu = new DeleteFile(self::DOMAIN,self::BUCKET);
            $res = $qiniu -> delimage($name);
            if($res['err']){
                dump($res['msg']);
            }else{
                echo '删除成功!';
            }
        }
    }
}
<?php
namespace app\index\controller;

use think\Controller;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class UploadFile extends Controller
{
    protected $domain;
    protected $bucket;
    protected $token;

    const ACCESS_KEY = 'AK';
    const SECRET_KEY = 'SK';

    public function __construct($domain, $bucket) {
        $this->domain = 'http://'.$domain.'/';
        $this->bucket = $bucket;
        $auth = new Auth(self::ACCESS_KEY, self::SECRET_KEY);
        // 生成上传Token
        $this->token = $auth->uploadToken($bucket);
    }

    /**
     * 上传
     * @param array $file 图片参数
     * @return array
     */
    public function uploadOne($file) {
        // 构建 UploadManager 对象
        $uploadMgr = new UploadManager();
        list($ret, $err) = $uploadMgr->putFile($this->token, $file['name'], $file['tmp_name']);
        if ($err !== null) {
            return ['err' => 1, 'msg' => $err, 'data' => ''];
        } else {
            //返回图片的完整URL
            return ['err' => 0, 'msg' => '上传完成', 'data' => ($this->domain . $ret['key'])];
        }
    }
}
<?php
namespace app\index\controller;

use Qiniu\Auth;
use think\Controller;
use Qiniu\Storage\BucketManager;
use Qiniu\Config;

class DeleteFile extends Controller
{
    protected $domain;
    protected $bucket;

    const ACCESS_KEY = 'AK';
    const SECRET_KEY = 'SK';

    public function __construct($domain, $bucket) {
        $this->domain = 'http://'.$domain.'/';
        $this->bucket = $bucket;
    }

    /**
     * 删除图片
     * @param $delFileName 要删除的图片文件,与七牛云空间存在的文件名称相同
     * @return bool
     */
    public function delimage($delFileName)
    {
        $auth = new Auth(self::ACCESS_KEY, self::SECRET_KEY);
        $config = new Config();
        $bucketManager = new BucketManager($auth, $config);
        $err = $bucketManager->delete($this->bucket, $delFileName);
        if ($err) {
            return ['err' => 1, 'msg' => $err];
        }else{
            return ['err' => 0, 'msg' => '删除成功'];
        }
    }
}

由于文档中示例代码也很详细,这里就不多写了。上面演示的是上传图片,但是也可以上传视频,把图片的判断删除或修改就可以了,七牛会自动对大于4MB的文件进行分片上传。还有非常多的操作功能,可以参考文档试一试。

发表评论

发表评论

沙发空缺中,还不快抢~