Home  >  Article  >  php教程  >  七牛云存贮demo

七牛云存贮demo

WBOY
WBOYOriginal
2016-06-07 11:35:471069browse

七牛的开发文档真能把人看的稀里糊涂的,搜demo又搜不到,没法,自己研究了一下,共享出部分代码,权当抛砖引玉了,不好您也别喷(使用的TP3.1.X的框架,不支持命名空间,所以把api整合到一个文件里了,省得引入不正确,见附件)
<?php <br /> <br> <br>     class QiniuAction extends Action {<br> <br>         public $accessKey = '******WqG8S6_d0z81ctXRe9q**********';//请自行去七牛申请<br>         public $secretKey = '*************Y6Hi7nsdaqsI**********';//请自行去七牛申请<br>         public $bucket    = 'test12345';    //你的七牛管理后台的某个空间名<br>         public $domain    = 'http://xxxxxx.clouddn.com/';//你的七牛管理后台的分配给你的域名,位于 空间设置->域名设置->七牛域名<br>         public $returnUrl = 'http://yourdomain.com/index.php';//上传成功后的回调地址<br>         public $QiniuAuth;<br> <br> <br>         protected function _initialize(){<br>             parent::_initialize();<br> <br>             import('@.Common.Qiniu');<br>             $this->QiniuAuth = new Auth($this->accessKey, $this->secretKey);<br>         }<br> <br> <br>         //列表页<br>         public function index(){<br>             $auth = $this->QiniuAuth;<br>             $bucketMgr = new BucketManager($auth);<br> <br>             $bucket = $this->bucket;<br>             $prefix = '';<br>             $marker = '';<br>             $limit  = 100;//显示数量<br> <br>             list($iterms, $marker, $err) = $bucketMgr->listFiles($bucket, $prefix, $marker, $limit);<br>             if ($err !== null) {<br>                 dump($err);<br>                 $this->error('发生错误,请联系管理员。');<br>             } else {<br>                 foreach ($iterms as $key => $val) {<br>                     $expire = time() + 3600;    //过期时间(秒)<br>                     $url    = $this->domain . $val['key'] . '?e=' . $expire;//构造URL<br>                     $sign   = $auth->sign($url);    //进行签名加密<br>                     $token  ='&token=' . $sign;     //组装签名得到的token<br>                     $val['url'] = $url . $token;    //生成最终url<br>                     $iterms[$key] = $val;<br>                 }<br> <br>                 $this->assign('list', $iterms);<br>             }<br> <br>             $this->display();<br>         }<br> <br> <br>         //上传(模板文件见附件)<br>         public function add(){<br>             $auth   = $this->QiniuAuth;<br>             $bucket = $this->bucket;            // 要上传的空间<br>             $key    = time() . '.jpg';//自定义的名字,如果不设置,就跟hash相同<br> <br>             $policy = array(<br>                 'returnUrl' => $this->returnUrl,<br>                 'returnBody'=> '{"key": $(key), "hash": $(etag), "w": $(imageInfo.width), "h": $(imageInfo.height)}'<br>             );<br> <br>             $token = $auth->uploadToken($bucket, $key, 3600, $policy); // 生成上传 Token<br> <br>             $this->assign('token', $token);<br>             $this->assign('key', $key);<br>             $this->display();<br>         }<br> <br> <br>         public function del(){<br>             $key = I('get.key');<br>             if($key !== ''){<br>                 $auth      = $this->QiniuAuth;<br>                 $bucketMgr = new BucketManager($auth);<br>                 if($err = $bucketMgr->delete($this->bucket, $key) == NULL){<br>                     redirect($_SERVER['HTTP_REFERER']);<br>                 }else{<br>                     $this->error('删除失败');<br>                 }<br> <br>             }else{<br>                 redirect($_SERVER['HTTP_REFERER']);<br>             }<br>         }<br> <br> <br>     }

附件 qiniu.7z ( 9.01 KB 下载:118 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn