<?php
namespace Qcloud_cos;
date_default_timezone_set('PRC');
class Cosapi
{
const EXPIRED_SECONDS = 180;
const SLICE_SIZE_512K = 524288;
const SLICE_SIZE_1M = 1048576;
const SLICE_SIZE_2M = 2097152;
const SLICE_SIZE_3M = 3145728;
const MAX_UNSLICE_FILE_SIZE = 20971520;
const MAX_RETRY_TIMES = 3;
const COSAPI_PARAMS_ERROR = -1;
const COSAPI_NETWORK_ERROR = -2;
private static $timeout = 60;
public static function setTimeout($timeout = 60) {
if (!is_int($timeout) || $timeout < 0) {
return false;
}
self::$timeout = $timeout;
return true;
}
public static function upload($bucketName, $srcPath, $dstPath,
$bizAttr = null, $slicesize = null, $insertOnly = null)
{
if (!file_exists($srcPath)) {
return array(
'code' => self::COSAPI_PARAMS_ERROR,
'message' => 'file '.$srcPath.' not exists',
'data' => array());
}
if (filesize($srcPath) < self::MAX_UNSLICE_FILE_SIZE ) {
return self::uploadfile($bucketName, $srcPath, $dstPath, $bizAttr, $insertOnly);
} else {
$sliceSize = self::getSliceSize($slicesize);
return self::upload_slice($bucketName, $srcPath, $dstPath, $bizAttr, $sliceSize, $insertOnly);
}
}
public static function createFolder($bucketName, $path, $bizAttr = null) {
$path = self::normalizerPath($path, True);
$path = self::cosUrlEncode($path);
$expired = time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucketName, $path);
$sign = Auth::appSign($expired, $bucketName);
$data = array(
'op' => 'create',
'biz_attr' => (isset($bizAttr) ? $bizAttr : ''),
);
$data = json_encode($data);
$req = array(
'url' => $url,
'method' => 'post',
'timeout' => self::$timeout,
'data' => $data,
'header' => array(
'Authorization:'.$sign,
'Content-Type: application/json',
),
);
return self::sendRequest($req);
}
public static function listFolder(
$bucketName, $path, $num = 20,
$pattern = 'eListBoth', $order = 0,
$context = null) {
$path = self::normalizerPath($path,True);
return self::listBase($bucketName, $path, $num,
$pattern, $order, $context);
}
public static function prefixSearch(
$bucketName, $prefix, $num = 20,
$pattern = 'eListBoth', $order = 0,
$context = null) {
$path = self::normalizerPath($prefix);
return self::listBase($bucketName, $prefix, $num,
$pattern, $order, $context);
}
public static function updateFolder($bucketName, $path, $bizAttr = null) {
$path = self::normalizerPath($path, True);
return self::updateBase($bucketName, $path, $bizAttr);
}
public static function statFolder($bucketName, $path) {
$path = self::normalizerPath($path, True);
return self::statBase($bucketName, $path);
}
public static function delFolder($bucketName, $path) {
$path = self::normalizerPath($path, True);
return self::delBase($bucketName, $path);
}
public static function update($bucketName, $path,
$bizAttr = null, $authority=null,$customer_headers_array=null) {
$path = self::normalizerPath($path);
return self::updateBase($bucketName, $path, $bizAttr, $authority, $customer_headers_array);
}
public static function move($bucketName, $srcPath, $dstPath, $toOverWrite = 0)
{
$srcPath = self::cosUrlEncode($srcPath);
$url = self::generateResUrl($bucketName,$srcPath);
$sign = Auth::appSign_once($srcPath, $bucketName);
$expired = time() + self::EXPIRED_SECONDS;
$data = array(
'op' => 'move',
'dest_fileid' => $dstPath,
'to_over_write' => $toOverWrite,
);
$data = json_encode($data);
$req = array(
'url' => $url,
'method' => 'post',
'timeout' => self::$timeout,
'data' => $data,
'header' => array(
'Authorization: '.$sign,
'Content-Type: application/json',
),
);
return self::sendRequest($req);
}
public static function stat($bucketName, $path) {
$path = self::normalizerPath($path);
return self::statBase($bucketName, $path);
}
public static function delFile($bucketName, $path) {
$path = self::normalizerPath($path);
return self::delBase($bucketName, $path);
}
private static function uploadfile($bucketName, $srcPath, $dstPath, $bizAttr = null, $insertOnly = null)
{
$srcPath = realpath($srcPath);
$dstPath = self::cosUrlEncode($dstPath);
if (filesize($srcPath) >= self::MAX_UNSLICE_FILE_SIZE )
{
return array(
'code' => self::COSAPI_PARAMS_ERROR,
'message' => 'file '.$srcPath.' larger then 20M, please use upload_slice interface',
'data' => array());
}
$expired = time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucketName, $dstPath);
$sign = Auth::appSign($expired, $bucketName);
$sha1 = hash_file('sha1', $srcPath);
$data = array(
'op' => 'upload',
'sha' => $sha1,
'biz_attr' => (isset($bizAttr) ? $bizAttr : ''),
);
if (function_exists('curl_file_create')) {
$data['filecontent'] = curl_file_create($srcPath);
} else {
$data['filecontent'] = '@'.$srcPath;
}
if (isset($insertOnly) && strlen($insertOnly) > 0)
$data['insertOnly'] = (($insertOnly == 0 || $insertOnly == '0' ) ? 0 : 1);
$req = array(
'url' => $url,
'method' => 'post',
'timeout' => self::$timeout,
'data' => $data,
'header' => array(
'Authorization:'.$sign,
),
);
return self::sendRequest($req);
}
private static function upload_slice(
$bucketName, $srcPath, $dstPath,
$bizAttr = null, $sliceSize = null, $insertOnly=null) {
$srcPath = realpath($srcPath);
$fileSize = filesize($srcPath);
$dstPath = self::cosUrlEncode($dstPath);
$expired = time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucketName, $dstPath);
$sign = Auth::appSign($expired, $bucketName);
$sha1 = hash_file('sha1', $srcPath);
$ret = self::upload_prepare(
$fileSize, $sha1, $sliceSize,
$sign, $url, $bizAttr, $insertOnly);
if($ret['code'] != 0) {
return $ret;
}
if(isset($ret['data'])
&& isset($ret['data']['url'])) {
return $ret;
}
$sliceSize = $ret['data']['slice_size'];
if ($sliceSize > self::SLICE_SIZE_3M ||
$sliceSize <= 0) {
$ret['code'] = self::COSAPI_PARAMS_ERROR;
$ret['message'] = 'illegal slice size';
return $ret;
}
$session = $ret['data']['session'];
$offset = $ret['data']['offset'];
$sliceCnt = ceil($fileSize / $sliceSize);
$expired = time() + (self::EXPIRED_SECONDS * $sliceCnt);
$sign = Auth::appSign($expired, $bucketName);
$ret = self::upload_data(
$fileSize, $sha1, $sliceSize,
$sign, $url, $srcPath,
$offset, $session);
return $ret;
}
private static function upload_prepare(
$fileSize, $sha1, $sliceSize,
$sign, $url, $bizAttr = null, $insertOnly = null) {
$data = array(
'op' => 'upload_slice',
'filesize' => $fileSize,
'sha' => $sha1,
);
if (isset($bizAttr) && strlen($bizAttr))
$data['biz_attr'] = $bizAttr;
if (isset($insertOnly))
$data['insertOnly'] = (($insertOnly == 0) ? 0 : 1);
if ($sliceSize <= self::SLICE_SIZE_3M) {
$data['slice_size'] = $sliceSize;
} else {
$data['slice_size'] = self::SLICE_SIZE_3M;
}
$req = array(
'url' => $url,
'method' => 'post',
'timeout' => self::$timeout,
'data' => $data,
'header' => array(
'Authorization:'.$sign,
),
);
$ret = self::sendRequest($req);
return $ret;
}
private static function upload_data(
$fileSize, $sha1, $sliceSize,
$sign, $url, $srcPath,
$offset, $session) {
while ($fileSize > $offset) {
$filecontent = file_get_contents(
$srcPath, false, null,
$offset, $sliceSize);
if ($filecontent === false) {
return array(
'code' => self::COSAPI_PARAMS_ERROR,
'message' => 'read file '.$srcPath.' error',
'data' => array(),
);
}
$boundary = '---------------------------' . substr(md5(mt_rand()), 0, 10);
$data = self::generateSliceBody(
$filecontent, $offset, $sha1,
$session, basename($srcPath), $boundary);
$req = array(
'url' => $url,
'method' => 'post',
'timeout' => self::$timeout,
'data' => $data,
'header' => array(
'Authorization:'.$sign,
'Content-Type: multipart/form-data; boundary=' . $boundary,
),
);
$retry_times = 0;
do {
$ret = self::sendRequest($req);
if ($ret['code'] == 0) {
break;
}
$retry_times++;
} while($retry_times < self::MAX_RETRY_TIMES);
if($ret['code'] != 0) {
return $ret;
}
if ($ret['data']['session']) {
$session = $ret['data']['session'];
}
$offset += $sliceSize;
}
return $ret;
}
private static function generateSliceBody(
$fileContent, $offset, $sha,
$session, $fileName, $boundary) {
$formdata = '';
$formdata .= '--' . $boundary . "\r\n";
$formdata .= "content-disposition: form-data; name=\"op\"\r\n\r\nupload_slice\r\n";
$formdata .= '--' . $boundary . "\r\n";
$formdata .= "content-disposition: form-data; name=\"offset\"\r\n\r\n" . $offset. "\r\n";
$formdata .= '--' . $boundary . "\r\n";
$formdata .= "content-disposition: form-data; name=\"session\"\r\n\r\n" . $session . "\r\n";
$formdata .= '--' . $boundary . "\r\n";
$formdata .= "content-disposition: form-data; name=\"fileContent\"; filename=\"" . $fileName . "\"\r\n";
$formdata .= "content-type: application/octet-stream\r\n\r\n";
$data = $formdata . $fileContent . "\r\n--" . $boundary . "--\r\n";
return $data;
}
private static function listBase(
$bucketName, $path, $num = 20,
$pattern = 'eListBoth', $order = 0, $context = null) {
$path = self::cosUrlEncode($path);
$expired = time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucketName, $path);
$sign = Auth::appSign($expired, $bucketName);
$data = array(
'op' => 'list',
);
if (self::isPatternValid($pattern) == false)
{
return array(
'code' => self::COSAPI_PARAMS_ERROR,
'message' => 'parameter pattern invalid',
);
}
$data['pattern'] = $pattern;
if ($order != 0 && $order != 1)
{
return array(
'code' => self::COSAPI_PARAMS_ERROR,
'message' => 'parameter order invalid',
);
}
$data['order'] = $order;
if ($num < 0 || $num > 199)
{
return array(
'code' => self::COSAPI_PARAMS_ERROR,
'message' => 'parameter num invalid, num need less then 200',
);
}
$data['num'] = $num;
if (isset($context))
{
$data['context'] = $context;
}
$url = $url . '?' . http_build_query($data);
$req = array(
'url' => $url,
'method' => 'get',
'timeout' => self::$timeout,
'header' => array(
'Authorization:'.$sign,
),
);
return self::sendRequest($req);
}
private static function updateBase($bucketName, $path,
$bizAttr = null, $authority = null, $custom_headers_array = null) {
$path = self::cosUrlEncode($path);
$expired = time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucketName, $path);
$sign = Auth::appSign_once(
$path, $bucketName);
$data = array(
'op' => 'update',
);
$flag = 0;
if (isset($bizAttr))
{
$data['biz_attr'] = $bizAttr;
$flag = $flag | 0x01;
}
if (isset($authority) && strlen($authority) > 0)
{
if(self::isAuthorityValid($authority) == false)
{
return array(
'code' => self::COSAPI_PARAMS_ERROR,
'message' => 'parameter authority invalid',
);
}
$data['authority'] = $authority;
$flag = $flag | 0x80;
}
if (isset($custom_headers_array))
{
$data['custom_headers'] = array();
self::add_customer_header($data['custom_headers'], $custom_headers_array);
$flag = $flag | 0x40;
}
if ($flag != 0 && $flag != 1)
{
$data['flag'] = $flag;
}
$data = json_encode($data);
$req = array(
'url' => $url,
'method' => 'post',
'timeout' => self::$timeout,
'data' => $data,
'header' => array(
'Authorization:'.$sign,
'Content-Type: application/json',
),
);
return self::sendRequest($req);
}
private static function statBase($bucketName, $path) {
$path = self::cosUrlEncode($path);
$expired = time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucketName, $path);
$sign = Auth::appSign($expired, $bucketName);
$data = array(
'op' => 'stat',
);
$url = $url . '?' . http_build_query($data);
$req = array(
'url' => $url,
'method' => 'get',
'timeout' => self::$timeout,
'header' => array(
'Authorization:'.$sign,
),
);
return self::sendRequest($req);
}
private static function delBase($bucketName, $path) {
if ($path == "/") {
return array(
'code' => self::COSAPI_PARAMS_ERROR,
'message' => 'can not delete bucket using api! go to http://console.qcloud.com/cos to operate bucket',
);
}
$path = self::cosUrlEncode($path);
$expired = time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucketName, $path);
$sign = Auth::appSign_once(
$path, $bucketName);
$data = array(
'op' => 'delete',
);
$data = json_encode($data);
$req = array(
'url' => $url,
'method' => 'post',
'timeout' => self::$timeout,
'data' => $data,
'header' => array(
'Authorization:'.$sign,
'Content-Type: application/json',
),
);
return self::sendRequest($req);
}
private static function cosUrlEncode($path) {
return str_replace('%2F', '/', rawurlencode($path));
}
private static function generateResUrl($bucketName, $dstPath) {
return Conf::API_COSAPI_END_POINT . Conf::APPID . '/' . $bucketName . $dstPath;
}
private static function sendRequest($req) {
$rsp = Http::send($req);
$info = Http::info();
$ret = json_decode($rsp, true);
if ($ret) {
if (0 === $ret['code']) {
return $ret;
} else {
return array(
'code' => $ret['code'],
'message' => $ret['message'],
'data' => array()
);
}
} else {
return array(
'code' => self::COSAPI_NETWORK_ERROR,
'message' => $rsp,
'data' => array()
);
}
}
private static function getSliceSize($sliceSize)
{
$size = self::SLICE_SIZE_1M;
if (!isset($sliceSize))
{
return $size;
}
if ($sliceSize <= self::SLICE_SIZE_512K)
{
$size = self::SLICE_SIZE_512K;
}
else if ($sliceSize <= self::SLICE_SIZE_1M)
{
$size = self::SLICE_SIZE_1M;
}
else if ($sliceSize <= self::SLICE_SIZE_2M)
{
$size = self::SLICE_SIZE_2M;
}
else
{
$size = self::SLICE_SIZE_3M;
}
return $size;
}
private static function normalizerPath($path, $isfolder = False) {
if (preg_match('/^ $path = '/' . $path;
}
if ($isfolder == True)
{
if (preg_match('/\/$/', $path) == 0) {
$path = $path . '/';
}
}
return $path;
}
private static function isAuthorityValid($authority)
{
if ($authority == 'eInvalid'
|| $authority == 'eWRPrivate'
|| $authority == 'eWPrivateRPublic')
{
return true;
}
return false;
}
private static function isPatternValid($pattern)
{
if ($pattern == 'eListBoth'
|| $pattern == 'eListDirOnly'
|| $pattern == 'eListFileOnly')
{
return true;
}
return false;
}
private static function isCustomer_header($key)
{
if ($key == 'Cache-Control'
|| $key == 'Content-Type'
|| $key == 'Content-Disposition'
|| $key == 'Content-Language'
|| substr($key,0,strlen('x-cos-meta-')) == 'x-cos-meta-')
{
return true;
}
return false;
}
private static function add_customer_header(&$data, &$customer_headers_array)
{
if (count($customer_headers_array) < 1) return;
foreach($customer_headers_array as $key=>$value)
{
if(self::isCustomer_header($key))
{
$data[$key] = $value;
}
}
}
}