<?php
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class qiniu
{
public $_accesskey = null;
public $_secretKey =null;
public $_bucket =null;
/**
* 构造函数
*
* @access public
* @param string $tpl
* @return void
*/
function __construct($accesskey=null,$secretKey=null,$bucket =null)
{
$this->_accesskey=$accesskey;
$this->_secretKey=$secretKey;
$this->_bucket=$bucket;
}
function gettoken(){

$auth = new Auth($this->_accessKey, $this->_secretKey);
$bucket = $this->_bucket;
$token = $auth->uploadToken($bucket);
return $token;
}
function uploadstring($string){
//$string是字符串

$auth = new Auth($this->_accesskey, $this->_secretKey);
$bucket = $this->_bucket;
// 设置put policy的其他参数, 上传回调
//$opts = array(
// 'callbackUrl' => 'http://www.callback.com/',
// 'callbackBody' => 'name=$(fname)&hash=$(etag)'
// );
//$token = $auth->uploadToken($bucket, null, 3600, $opts);

$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();

list($ret, $err) = $uploadMgr->put($token, null, $string);
echo "\n====> put result: \n";
if ($err !== null) {
return $err;
} else {
return $ret;
}
}
function uploadfile($file=null){
//$file是文件路径

$auth = new Auth($this->_accesskey, $this->_secretKey);
$bucket = $this->_bucket;
// 设置put policy的其他参数, 上传回调
//$opts = array(
// 'callbackUrl' => 'http://www.callback.com/',
// 'callbackBody' => 'name=$(fname)&hash=$(etag)'
// );
//$token = $auth->uploadToken($bucket, null, 3600, $opts);

$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();
list($ret, $err) = $uploadMgr->putFile($token, null, $file);
if ($err !== null) {
return $err;
} else {
return $ret;
}
}
function download($resource,$filecode,$tosource=null,filename="file"){

//$resource是bucket的网址,$filecode是文件的码,$tosource是要下载的文件夹路径
$auth = new Auth($this->_accesskey, $this->_secretKey);
$baseUrl = $resource.'/'.$filecode;
$authUrl = $auth->privateDownloadUrl($baseUrl);
$this->download_remote_file_with_curl($authUrl, $tosource.time().$filename);
return $authUrl;
}
function download_remote_file_with_curl($file_url, $save_to)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_URL,$file_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file_content = curl_exec($ch);
curl_close($ch);

$downloaded_file = fopen($save_to, 'w');
fwrite($downloaded_file, $file_content);
fclose($downloaded_file);
}
}

?>

最新文章

  1. NOIP 2015 游记
  2. http get/post解决乱码问题
  3. 使用odoo价格表[pricelist]对价格进行特别处理,如 .99
  4. 我的Linux对拍脚本
  5. WEB前端介绍
  6. 【全面解析DeepZoom 之三】建立DeepZoom应用
  7. laravel5.2学习资源
  8. Django之上传文件
  9. 你真的会 snapshot 吗? - 每天5分钟玩转 OpenStack(163)
  10. [BZOJ 2500]幸福的道路 树形dp+单调队列+二分答案
  11. RNN探索(2)之手写数字识别
  12. Android--Service之基础
  13. 省选前的JOI
  14. 初学CSS-4-文字颜色属性
  15. MATLAB线性方程组的迭代求解法
  16. GitHub私有代码库将免费开放
  17. javascript创建节点的事件绑定
  18. 二叉树 c++
  19. Java如何处理已检查异常?
  20. 生成word附件和word域动态赋值

热门文章

  1. mounted钩子问题
  2. Split()函数
  3. 在iOS项目中嵌入RN代码
  4. B.1 接口
  5. PHP websocket之聊天室实现
  6. 单例模式的python实现
  7. git 的简单使用(5)
  8. Django REST framework - 认证
  9. ubuntu 14.04 gcc/g++版本降低
  10. 洛谷 P2827 BZOJ 4721 UOJ #264 蚯蚓