七牛雲存儲demo

字號+ 編輯: 国内TP粉 修訂: 种花家 來源: ThinkPHP 2023-09-21 我要說兩句(0)

Thinkphp3.1.x基礎上的七牛雲存儲接口操作類代碼demo案例。

转自Thinkphp官方的一个代码演示案例。

使用的TP3.1.X的框架,不支持命名空间,所以把api整合到一个文件里了,省得引入不正确

<?php

class QiniuAction extends Action
{

    public $accessKey = '******WqG8S6_d0z81ctXRe9q**********'; //请自行去七牛申请
    public $secretKey = '*************Y6Hi7nsdaqsI**********'; //请自行去七牛申请
    public $bucket    = 'test12345'; //你的七牛管理后台的某个空间名
    public $domain    = 'http://xxxxxx.clouddn.com/'; //你的七牛管理后台的分配给你的域名,位于 空间设置->域名设置->七牛域名
    public $returnUrl = 'http://yourdomain.com/index.php'; //上传成功后的回调地址
    public $QiniuAuth;

    protected function _initialize()
    {
        parent::_initialize();

        import('@.Common.Qiniu');
        $this->QiniuAuth = new Auth($this->accessKey, $this->secretKey);
    }

    // 列表页
    public function index()
    {
        $auth      = $this->QiniuAuth;
        $bucketMgr = new BucketManager($auth);

        $bucket = $this->bucket;
        $prefix = '';
        $marker = '';
        $limit  = 100; //显示数量

        list($iterms, $marker, $err) = $bucketMgr->listFiles($bucket, $prefix, $marker, $limit);
        if ($err !== null) {
            dump($err);
            $this->error('发生错误,请联系管理员。');
        } else {
            foreach ($iterms as $key => $val) {
                $expire       = time() + 3600; //过期时间(秒)
                $url          = $this->domain . $val['key'] . '?e=' . $expire; //构造URL
                $sign         = $auth->sign($url); //进行签名加密
                $token        = '&token=' . $sign; //组装签名得到的token
                $val['url']   = $url . $token; //生成最终url
                $iterms[$key] = $val;
            }

            $this->assign('list', $iterms);
        }

        $this->display();
    }

    //上传(模板文件见附件)
    public function add()
    {
        $auth   = $this->QiniuAuth;
        $bucket = $this->bucket; // 要上传的空间
        $key    = time() . '.jpg'; //自定义的名字,如果不设置,就跟hash相同

        $policy = array(
            'returnUrl'  => $this->returnUrl,
            'returnBody' => '{"key": $(key), "hash": $(etag), "w": $(imageInfo.width), "h": $(imageInfo.height)}',
        );

        $token = $auth->uploadToken($bucket, $key, 3600, $policy); // 生成上传 Token

        $this->assign('token', $token);
        $this->assign('key', $key);
        $this->display();
    }

    public function del()
    {
        $key = I('get.key');
        if ($key !== '') {
            $auth      = $this->QiniuAuth;
            $bucketMgr = new BucketManager($auth);
            if ($err = $bucketMgr->delete($this->bucket, $key) == null) {
                redirect($_SERVER['HTTP_REFERER']);
            } else {
                $this->error('删除失败');
            }

        } else {
            redirect($_SERVER['HTTP_REFERER']);
        }
    }

}


閲完此文,您的感想如何?
  • 有用

    1

  • 沒用

    1

  • 開心

    1

  • 憤怒

    1

  • 可憐

    1

1.如文章侵犯了您的版權,請發郵件通知本站,該文章將在24小時内刪除;
2.本站標注原創的文章,轉發時煩請注明來源;
3.交流群: 2702237 13835667

相關課文
  • mac開發接入微信公衆號接口返回報錯 cURL error 56: SSLRead() return error -9806

  • PHP的換行符是什麽

  • pecl安裝程序時報錯Array and string offset access syntax with curly braces is no longer supported

  • 由於商家傳入的H5交易參數有誤,該筆交易暫時無法完成,請聯繫商家解決

我要說說
網上賓友點評