123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- /**
- * 七牛接口调用
- * 1、图片文件上传
- *
- * 参考:http://developer.qiniu.com/docs/v6/sdk/php-sdk.html
- *
- * @author: dogstar 2015-03-17
- */
- require_once dirname(__FILE__) . '/qiniu/io.php';
- require_once dirname(__FILE__) . '/qiniu/rs.php';
- class Qiniu_Lite {
- protected $config;
- /**
- * @param string $config['accessKey'] 统一的key
- * @param string $config['secretKey']
- * @param string $config['space_bucket'] 自定义配置的空间
- * @param string $config['space_host']
- */
- public function __construct($config = NULL) {
- $this->config = $config;
- if ($this->config === NULL) {
- $this->config = DI()->config->get('app.Qiniu');
- }
- }
- /**
- * 文件上传
- * @param string $filePath 待上传文件的绝对路径
- * @return string 上传成功后的URL,失败时返回空
- */
- /*public function uploadFile($filePath){
- $fileUrl = '';
- if (!file_exists($filePath)) {
- return $fileUrl;
- }
- $config = $this->config;
- $fileName = date('YmdHis_', $_SERVER['REQUEST_TIME'])
- . md5(PhalApi_Tool::createRandStr(8) . microtime(true));
- Qiniu_SetKeys($config['accessKey'], $config['secretKey']);
- $putPolicy = new Qiniu_RS_PutPolicy($config['space_bucket']);
- $upToken = $putPolicy->Token(null);
- $putExtra = new Qiniu_PutExtra();
- $putExtra->Crc32 = 1;
- list($ret, $err) = Qiniu_PutFile($upToken, $fileName, $filePath, $putExtra);
- if ($err !== null) {
- DI()->logger->debug('failed to upload file to qiniu',
- array('Err' => $err->Err, 'Reqid' => $err->Reqid, 'Details' => $err->Details, 'Code' => $err->Code));
- } else {
- $fileUrl = $config['space_host'] . '/' . $fileName;
- DI()->logger->debug('succeed to upload file to qiniu', $ret);
- }
- return $fileUrl;
- }*/
- /**
- * 文件上传(重写)
- * @param string $filePath 待上传文件的绝对路径
- * @return string 上传成功后的URL,失败时返回空
- */
- public function uploadFile($filePath,$accesskey,$secretkey,$bucket,$spacehost){
- $fileUrl = '';
- if (!file_exists($filePath)) {
- return $fileUrl;
- }
-
- $fileName = date('YmdHis_', $_SERVER['REQUEST_TIME'])
- . md5(PhalApi_Tool::createRandStr(8) . microtime(true));
- Qiniu_SetKeys($accesskey, $secretkey);
- $putPolicy = new Qiniu_RS_PutPolicy($bucket);
- $upToken = $putPolicy->Token(null);
- $putExtra = new Qiniu_PutExtra();
- $putExtra->Crc32 = 1;
- list($ret, $err) = Qiniu_PutFile($upToken, $fileName, $filePath, $putExtra);
- if ($err !== null) {
- DI()->logger->debug('failed to upload file to qiniu',
- array('Err' => $err->Err, 'Reqid' => $err->Reqid, 'Details' => $err->Details, 'Code' => $err->Code));
- } else {
- $fileUrl = $spacehost.$fileName;
- DI()->logger->debug('succeed to upload file to qiniu', $ret);
- }
- return $fileUrl;
- }
-
- /**
- * 获取七牛Token
- * @return string 七牛Token
- */
- public function getQiniuToken(){
- $config = $this->config;
- Qiniu_SetKeys($config['accessKey'], $config['secretKey']);
- $putPolicy = new Qiniu_RS_PutPolicy($config['space_bucket']);
- $upToken = $putPolicy->Token(null);
- return $upToken;
- }
- /**
- * 获取七牛Token(方法重写)
- * @return string 七牛Token
- */
- public function getQiniuToken1($accesskey,$secretkey,$bucket){
- Qiniu_SetKeys($accesskey, $secretkey);
- $putPolicy = new Qiniu_RS_PutPolicy($bucket);
- $upToken = $putPolicy->Token(null);
- return $upToken;
- }
- }
|