123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107 |
- <?php
- namespace Qiniu\Storage;
- use Qiniu\Auth;
- use Qiniu\Config;
- use Qiniu\Zone;
- use Qiniu\Http\Client;
- use Qiniu\Http\Error;
- final class BucketManager
- {
- private $auth;
- private $config;
- public function __construct(Auth $auth, Config $config = null)
- {
- $this->auth = $auth;
- if ($config == null) {
- $this->config = new Config();
- } else {
- $this->config = $config;
- }
- }
-
- public function buckets($shared = true)
- {
- $includeShared = "false";
- if ($shared === true) {
- $includeShared = "true";
- }
- return $this->rsGet('/buckets?shared=' . $includeShared);
- }
-
- public function listbuckets(
- $region = null,
- $line = 'false',
- $shared = 'false'
- ) {
- $path = '/v3/buckets?region=' . $region . '&line=' . $line . '&shared=' . $shared;
- $info = $this->ucPost($path);
- return $info;
- }
-
- public function createBucket($name, $region = 'z0')
- {
- $path = '/mkbucketv2/'.$name.'/region/' . $region;
- return $this->rsPost($path, null);
- }
-
- public function deleteBucket($name)
- {
- $path = '/drop/'.$name;
- return $this->rsPost($path, null);
- }
-
- public function domains($bucket)
- {
- return $this->apiGet('/v6/domain/list?tbl=' . $bucket);
- }
-
- public function bucketInfo($bucket)
- {
- $path = '/v2/bucketInfo?bucket=' . $bucket;
- $info = $this->ucPost($path);
- return $info;
- }
-
- public function bucketInfos($region = null, $shared = 'false', $fs = 'false')
- {
- $path = '/v2/bucketInfos?region=' . $region . '&shared=' . $shared . '&fs=' . $fs;
- $info = $this->ucPost($path);
- return $info;
- }
-
-
- public function listFiles(
- $bucket,
- $prefix = null,
- $marker = null,
- $limit = 1000,
- $delimiter = null
- ) {
- $query = array('bucket' => $bucket);
- \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
- \Qiniu\setWithoutEmpty($query, 'marker', $marker);
- \Qiniu\setWithoutEmpty($query, 'limit', $limit);
- \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
- $url = $this->getRsfHost() . '/list?' . http_build_query($query);
- return $this->get($url);
- }
-
- public function listFilesv2(
- $bucket,
- $prefix = null,
- $marker = null,
- $limit = 1000,
- $delimiter = null,
- $skipconfirm = true
- ) {
- $query = array('bucket' => $bucket);
- \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
- \Qiniu\setWithoutEmpty($query, 'marker', $marker);
- \Qiniu\setWithoutEmpty($query, 'limit', $limit);
- \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
- \Qiniu\setWithoutEmpty($query, 'skipconfirm', $skipconfirm);
- $path = '/v2/list?' . http_build_query($query);
- $url = $this->getRsfHost() . $path;
- $headers = $this->auth->authorization($url, null, 'application/x-www-form-urlencoded');
- $ret = Client::post($url, null, $headers);
- if (!$ret->ok()) {
- return array(null, new Error($url, $ret));
- }
- $r = explode("\n", $ret->body);
- $pop = array_pop($r);
- return array($r, null);
- }
-
-
-
-
- public function bucketLifecycleRule(
- $bucket,
- $name,
- $prefix,
- $delete_after_days,
- $to_line_after_days
- ) {
- $path = '/rules/add';
- if ($bucket) {
- $params['bucket'] = $bucket;
- }
- if ($name) {
- $params['name'] = $name;
- }
- if ($prefix) {
- $params['prefix'] = $prefix;
- }
- if ($delete_after_days) {
- $params['delete_after_days'] = $delete_after_days;
- }
- if ($to_line_after_days) {
- $params['to_line_after_days'] = $to_line_after_days;
- }
- $data = http_build_query($params);
- $info = $this->ucPost($path, $data);
- return $info;
- }
-
- public function updateBucketLifecycleRule(
- $bucket,
- $name,
- $prefix,
- $delete_after_days,
- $to_line_after_days
- ) {
- $path = '/rules/update';
- if ($bucket) {
- $params['bucket'] = $bucket;
- }
- if ($name) {
- $params['name'] = $name;
- }
- if ($prefix) {
- $params['prefix'] = $prefix;
- }
- if ($delete_after_days) {
- $params['delete_after_days'] = $delete_after_days;
- }
- if ($to_line_after_days) {
- $params['to_line_after_days'] = $to_line_after_days;
- }
- $data = http_build_query($params);
- $info = $this->ucPost($path, $data);
- return $info;
- }
-
- public function getBucketLifecycleRules($bucket)
- {
- $path = '/rules/get?bucket=' . $bucket;
- $info = $this->ucGet($path);
- return $info;
- }
-
- public function deleteBucketLifecycleRule($bucket, $name)
- {
- $path = '/rules/delete';
- if ($bucket) {
- $params['bucket'] = $bucket;
- }
- if ($name) {
- $params['name'] = $name;
- }
- $data = http_build_query($params);
- $info = $this->ucPost($path, $data);
- return $info;
- }
-
- public function putBucketEvent(
- $bucket,
- $name,
- $prefix,
- $suffix,
- $event,
- $callbackURL,
- $access_key = null,
- $host = null
- ) {
- $path = '/events/add';
- if ($bucket) {
- $params['bucket'] = $bucket;
- }
- if ($name) {
- $params['name'] = $name;
- }
- if ($prefix) {
- $params['prefix'] = $prefix;
- }
- if ($suffix) {
- $params['suffix'] = $suffix;
- }
- if ($callbackURL) {
- $params['callbackURL'] = $callbackURL;
- }
- if ($access_key) {
- $params['access_key'] = $access_key;
- }
- if ($host) {
- $params['host'] = $host;
- }
- $data = http_build_query($params);
- if ($event) {
- $eventpath = "";
- foreach ($event as $key => $value) {
- $eventpath .= "&event=$value";
- }
- $data .= $eventpath;
- }
- $info = $this->ucPost($path, $data);
- return $info;
- }
-
- public function updateBucketEvent(
- $bucket,
- $name,
- $prefix,
- $suffix,
- $event,
- $callbackURL,
- $access_key = null,
- $host = null
- ) {
- $path = '/events/update';
- if ($bucket) {
- $params['bucket'] = $bucket;
- }
- if ($name) {
- $params['name'] = $name;
- }
- if ($prefix) {
- $params['prefix'] = $prefix;
- }
- if ($suffix) {
- $params['suffix'] = $suffix;
- }
- if ($event) {
- $params['event'] = $event;
- }
- if ($callbackURL) {
- $params['callbackURL'] = $callbackURL;
- }
- if ($access_key) {
- $params['access_key'] = $access_key;
- }
- if ($host) {
- $params['host'] = $host;
- }
- $data = http_build_query($params);
- $info = $this->ucPost($path, $data);
- return $info;
- }
-
- public function getBucketEvents($bucket)
- {
- $path = '/events/get?bucket=' . $bucket;
- $info = $this->ucGet($path);
- return $info;
- }
-
- public function deleteBucketEvent($bucket, $name)
- {
- $path = '/events/delete';
- if ($bucket) {
- $params['bucket'] = $bucket;
- }
- if ($name) {
- $params['name'] = $name;
- }
- $data = http_build_query($params);
- $info = $this->ucPost($path, $data);
- return $info;
- }
-
-
-
-
-
-
-
-
-
- public function getCorsRules($bucket)
- {
- $path = '/corsRules/get/' . $bucket;
- $info = $this->ucGet($path);
- return $info;
- }
-
-
-
-
-
-
-
-
-
- public function getBucktSourceConfig(array $params)
- {
- $path = '/mirrorConfig/get';
- $data = json_encode($params);
- $info = $this->ucPostV2($path, $data);
- return $info;
- }
-
- public function putBucketAccessStyleMode($bucket, $mode)
- {
- $path = '/accessMode/' . $bucket . '/mode/' . $mode;
- $info = $this->ucPost($path, null);
- return $info;
- }
-
- public function putBucketAccessMode($bucket, $private)
- {
- $path = '/bucket/' . $bucket . '/private/' . $private;
- $info = $this->ucPost($path, null);
- return $info;
- }
-
- public function putReferAntiLeech($bucket, $mode, $norefer, $pattern, $enabled = 1)
- {
- $path = "/referAntiLeech?bucket=$bucket&mode=$mode&norefer=$norefer&pattern=$pattern&source_enabled=$enabled";
- $info = $this->ucPost($path, null);
- return $info;
- }
-
- public function putBucketMaxAge($bucket, $maxAge)
- {
- $path = '/maxAge?bucket=' . $bucket . '&maxAge=' . $maxAge;
- $info = $this->ucPost($path, null);
- return $info;
- }
-
- public function putBucketQuota($bucket, $size, $count)
- {
- $path = '/setbucketquota/' . $bucket . '/size/' . $size . '/count/' . $count;
- $info = $this->apiPost($path, null);
- return $info;
- }
-
- public function getBucketQuota($bucket)
- {
- $path = '/getbucketquota/' . $bucket;
- $info = $this->apiPost($path, null);
- return $info;
- }
-
- public function stat($bucket, $key)
- {
- $path = '/stat/' . \Qiniu\entry($bucket, $key);
- return $this->rsGet($path);
- }
-
- public function delete($bucket, $key)
- {
- $path = '/delete/' . \Qiniu\entry($bucket, $key);
- list(, $error) = $this->rsPost($path);
- return $error;
- }
-
- public function rename($bucket, $oldname, $newname)
- {
- return $this->move($bucket, $oldname, $bucket, $newname);
- }
-
- public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
- {
- $from = \Qiniu\entry($from_bucket, $from_key);
- $to = \Qiniu\entry($to_bucket, $to_key);
- $path = '/copy/' . $from . '/' . $to;
- if ($force === true) {
- $path .= '/force/true';
- }
- list(, $error) = $this->rsPost($path);
- return $error;
- }
-
- public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
- {
- $from = \Qiniu\entry($from_bucket, $from_key);
- $to = \Qiniu\entry($to_bucket, $to_key);
- $path = '/move/' . $from . '/' . $to;
- if ($force) {
- $path .= '/force/true';
- }
- list(, $error) = $this->rsPost($path);
- return $error;
- }
-
- public function changeMime($bucket, $key, $mime)
- {
- $resource = \Qiniu\entry($bucket, $key);
- $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
- $path = '/chgm/' . $resource . '/mime/' . $encode_mime;
- list(, $error) = $this->rsPost($path);
- return $error;
- }
-
- public function changeType($bucket, $key, $fileType)
- {
- $resource = \Qiniu\entry($bucket, $key);
- $path = '/chtype/' . $resource . '/type/' . $fileType;
- list(, $error) = $this->rsPost($path);
- return $error;
- }
-
- public function changeStatus($bucket, $key, $status)
- {
- $resource = \Qiniu\entry($bucket, $key);
- $path = '/chstatus/' . $resource . '/status/' . $status;
- list(, $error) = $this->rsPost($path);
- return $error;
- }
-
- public function fetch($url, $bucket, $key = null)
- {
- $resource = \Qiniu\base64_urlSafeEncode($url);
- $to = \Qiniu\entry($bucket, $key);
- $path = '/fetch/' . $resource . '/to/' . $to;
- $ak = $this->auth->getAccessKey();
- $ioHost = $this->config->getIovipHost($ak, $bucket);
- $url = $ioHost . $path;
- return $this->post($url, null);
- }
-
- public function prefetch($bucket, $key)
- {
- $resource = \Qiniu\entry($bucket, $key);
- $path = '/prefetch/' . $resource;
- $ak = $this->auth->getAccessKey();
- $ioHost = $this->config->getIovipHost($ak, $bucket);
- $url = $ioHost . $path;
- list(, $error) = $this->post($url, null);
- return $error;
- }
-
- public function batch($operations)
- {
- $params = 'op=' . implode('&op=', $operations);
- return $this->rsPost('/batch', $params);
- }
-
- public function deleteAfterDays($bucket, $key, $days)
- {
- $entry = \Qiniu\entry($bucket, $key);
- $path = "/deleteAfterDays/$entry/$days";
- list(, $error) = $this->rsPost($path);
- return $error;
- }
- private function getRsfHost()
- {
- $scheme = "http://";
- if ($this->config->useHTTPS == true) {
- $scheme = "https://";
- }
- return $scheme . Config::RSF_HOST;
- }
- private function getRsHost()
- {
- $scheme = "http://";
- if ($this->config->useHTTPS == true) {
- $scheme = "https://";
- }
- return $scheme . Config::RS_HOST;
- }
- private function getApiHost()
- {
- $scheme = "http://";
- if ($this->config->useHTTPS == true) {
- $scheme = "https://";
- }
- return $scheme . Config::API_HOST;
- }
- private function getUcHost()
- {
- $scheme = "http://";
- if ($this->config->useHTTPS == true) {
- $scheme = "https://";
- }
- return $scheme . Config::UC_HOST;
- }
- private function rsPost($path, $body = null)
- {
- $url = $this->getRsHost() . $path;
- return $this->post($url, $body);
- }
- private function apiPost($path, $body = null)
- {
- $url = $this->getApiHost() . $path;
- return $this->post($url, $body);
- }
- private function ucPost($path, $body = null)
- {
- $url = $this->getUcHost() . $path;
- return $this->post($url, $body);
- }
- private function ucGet($path)
- {
- $url = $this->getUcHost() . $path;
- return $this->get($url);
- }
- private function apiGet($path)
- {
- $url = $this->getApiHost() . $path;
- return $this->get($url);
- }
- private function rsGet($path)
- {
- $url = $this->getRsHost() . $path;
- return $this->get($url);
- }
- private function get($url)
- {
- $headers = $this->auth->authorization($url);
- $ret = Client::get($url, $headers);
- if (!$ret->ok()) {
- return array(null, new Error($url, $ret));
- }
- return array($ret->json(), null);
- }
- private function post($url, $body)
- {
- $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
- $ret = Client::post($url, $body, $headers);
- if (!$ret->ok()) {
- return array(null, new Error($url, $ret));
- }
- $r = ($ret->body === null) ? array() : $ret->json();
- return array($r, null);
- }
- private function ucPostV2($path, $body)
- {
- $url = $this->getUcHost() . $path;
- return $this->postV2($url, $body);
- }
- private function postV2($url, $body)
- {
- $headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/json');
- $headers["Content-Type"] = 'application/json';
- $ret = Client::post($url, $body, $headers);
- if (!$ret->ok()) {
- return array(null, new Error($url, $ret));
- }
- $r = ($ret->body === null) ? array() : $ret->json();
- return array($r, null);
- }
- public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
- {
- return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
- }
- public static function buildBatchRename($bucket, $key_pairs, $force)
- {
- return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
- }
- public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
- {
- return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
- }
- public static function buildBatchDelete($bucket, $keys)
- {
- return self::oneKeyBatch('/delete', $bucket, $keys);
- }
- public static function buildBatchStat($bucket, $keys)
- {
- return self::oneKeyBatch('/stat', $bucket, $keys);
- }
- public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
- {
- $data = array();
- foreach ($key_day_pairs as $key => $day) {
- array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
- }
- return $data;
- }
- public static function buildBatchChangeMime($bucket, $key_mime_pairs)
- {
- $data = array();
- foreach ($key_mime_pairs as $key => $mime) {
- array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
- }
- return $data;
- }
- public static function buildBatchChangeType($bucket, $key_type_pairs)
- {
- $data = array();
- foreach ($key_type_pairs as $key => $type) {
- array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
- }
- return $data;
- }
- private static function oneKeyBatch($operation, $bucket, $keys)
- {
- $data = array();
- foreach ($keys as $key) {
- array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
- }
- return $data;
- }
- private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
- {
- if ($target_bucket === null) {
- $target_bucket = $source_bucket;
- }
- $data = array();
- $forceOp = "false";
- if ($force) {
- $forceOp = "true";
- }
- foreach ($key_pairs as $from_key => $to_key) {
- $from = \Qiniu\entry($source_bucket, $from_key);
- $to = \Qiniu\entry($target_bucket, $to_key);
- array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
- }
- return $data;
- }
- }
|