PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/webroot/lib/vendor/qiniu/php-sdk/src/Qiniu/Storage/BucketManager.php

https://gitlab.com/tangsengjiu/Talk
PHP | 341 lines | 166 code | 32 blank | 143 comment | 4 complexity | 3074422f0258af415b82ea86e7e95843 MD5 | raw file
  1. <?php
  2. namespace Qiniu\Storage;
  3. use Qiniu\Auth;
  4. use Qiniu\Config;
  5. use Qiniu\Http\Client;
  6. use Qiniu\Http\Error;
  7. /**
  8. * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
  9. *
  10. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/
  11. */
  12. final class BucketManager
  13. {
  14. private $auth;
  15. public function __construct(Auth $auth)
  16. {
  17. $this->auth = $auth;
  18. }
  19. /**
  20. * 获取指定账号下所有的空间名。
  21. *
  22. * @return string[] 包含所有空间名
  23. */
  24. public function buckets()
  25. {
  26. return $this->rsGet('/buckets');
  27. }
  28. /**
  29. * 列取空间的文件列表
  30. *
  31. * @param $bucket 空间名
  32. * @param $prefix 列举前缀
  33. * @param $marker 列举标识符
  34. * @param $limit 单次列举个数限制
  35. * @param $delimiter 指定目录分隔符
  36. *
  37. * @return array 包含文件信息的数组,类似:[
  38. * {
  39. * "hash" => "<Hash string>",
  40. * "key" => "<Key string>",
  41. * "fsize" => "<file size>",
  42. * "putTime" => "<file modify time>"
  43. * },
  44. * ...
  45. * ]
  46. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
  47. */
  48. public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null)
  49. {
  50. $query = array('bucket' => $bucket);
  51. \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
  52. \Qiniu\setWithoutEmpty($query, 'marker', $marker);
  53. \Qiniu\setWithoutEmpty($query, 'limit', $limit);
  54. \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
  55. $url = Config::RSF_HOST . '/list?' . http_build_query($query);
  56. list($ret, $error) = $this->get($url);
  57. if ($ret === null) {
  58. return array(null, null, $error);
  59. }
  60. $marker = array_key_exists('marker', $ret) ? $ret['marker'] : null;
  61. return array($ret['items'], $marker, null);
  62. }
  63. /**
  64. * 获取资源的元信息,但不返回文件内容
  65. *
  66. * @param $bucket 待获取信息资源所在的空间
  67. * @param $key 待获取资源的文件名
  68. *
  69. * @return array 包含文件信息的数组,类似:
  70. * [
  71. * "hash" => "<Hash string>",
  72. * "key" => "<Key string>",
  73. * "fsize" => "<file size>",
  74. * "putTime" => "<file modify time>"
  75. * ]
  76. *
  77. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html
  78. */
  79. public function stat($bucket, $key)
  80. {
  81. $path = '/stat/' . \Qiniu\entry($bucket, $key);
  82. return $this->rsGet($path);
  83. }
  84. /**
  85. * 删除指定资源
  86. *
  87. * @param $bucket 待删除资源所在的空间
  88. * @param $key 待删除资源的文件名
  89. *
  90. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  91. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html
  92. */
  93. public function delete($bucket, $key)
  94. {
  95. $path = '/delete/' . \Qiniu\entry($bucket, $key);
  96. list(, $error) = $this->rsPost($path);
  97. return $error;
  98. }
  99. /**
  100. * 给资源进行重命名,本质为move操作。
  101. *
  102. * @param $bucket 待操作资源所在空间
  103. * @param $oldname 待操作资源文件名
  104. * @param $newname 目标资源文件名
  105. *
  106. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  107. */
  108. public function rename($bucket, $oldname, $newname)
  109. {
  110. return $this->move($bucket, $oldname, $bucket, $newname);
  111. }
  112. /**
  113. * 给资源进行重命名,本质为move操作。
  114. *
  115. * @param $from_bucket 待操作资源所在空间
  116. * @param $from_key 待操作资源文件名
  117. * @param $to_bucket 目标资源空间名
  118. * @param $to_key 目标资源文件名
  119. *
  120. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  121. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html
  122. */
  123. public function copy($from_bucket, $from_key, $to_bucket, $to_key)
  124. {
  125. $from = \Qiniu\entry($from_bucket, $from_key);
  126. $to = \Qiniu\entry($to_bucket, $to_key);
  127. $path = '/copy/' . $from . '/' . $to;
  128. list(, $error) = $this->rsPost($path);
  129. return $error;
  130. }
  131. /**
  132. * 将资源从一个空间到另一个空间
  133. *
  134. * @param $from_bucket 待操作资源所在空间
  135. * @param $from_key 待操作资源文件名
  136. * @param $to_bucket 目标资源空间名
  137. * @param $to_key 目标资源文件名
  138. *
  139. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  140. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html
  141. */
  142. public function move($from_bucket, $from_key, $to_bucket, $to_key)
  143. {
  144. $from = \Qiniu\entry($from_bucket, $from_key);
  145. $to = \Qiniu\entry($to_bucket, $to_key);
  146. $path = '/move/' . $from . '/' . $to;
  147. list(, $error) = $this->rsPost($path);
  148. return $error;
  149. }
  150. /**
  151. * 主动修改指定资源的文件类型
  152. *
  153. * @param $bucket 待操作资源所在空间
  154. * @param $key 待操作资源文件名
  155. * @param $mime 待操作文件目标mimeType
  156. *
  157. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  158. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html
  159. */
  160. public function changeMime($bucket, $key, $mime)
  161. {
  162. $resource = \Qiniu\entry($bucket, $key);
  163. $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
  164. $path = '/chgm/' . $resource . '/mime/' .$encode_mime;
  165. list(, $error) = $this->rsPost($path);
  166. return $error;
  167. }
  168. /**
  169. * 从指定URL抓取资源,并将该资源存储到指定空间中
  170. *
  171. * @param $url 指定的URL
  172. * @param $bucket 目标资源空间
  173. * @param $key 目标资源文件名
  174. *
  175. * @return array 包含已拉取的文件信息。
  176. * 成功时: [
  177. * [
  178. * "hash" => "<Hash string>",
  179. * "key" => "<Key string>"
  180. * ],
  181. * null
  182. * ]
  183. *
  184. * 失败时: [
  185. * null,
  186. * Qiniu/Http/Error
  187. * ]
  188. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html
  189. */
  190. public function fetch($url, $bucket, $key = null)
  191. {
  192. $resource = \Qiniu\base64_urlSafeEncode($url);
  193. $to = \Qiniu\entry($bucket, $key);
  194. $path = '/fetch/' . $resource . '/to/' . $to;
  195. return $this->ioPost($path);
  196. }
  197. /**
  198. * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
  199. *
  200. * @param $bucket 待获取资源所在的空间
  201. * @param $key 代获取资源文件名
  202. *
  203. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  204. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html
  205. */
  206. public function prefetch($bucket, $key)
  207. {
  208. $resource = \Qiniu\entry($bucket, $key);
  209. $path = '/prefetch/' . $resource;
  210. list(, $error) = $this->ioPost($path);
  211. return $error;
  212. }
  213. /**
  214. * 在单次请求中进行多个资源管理操作
  215. *
  216. * @param $operations 资源管理操作数组
  217. *
  218. * @return array 每个资源的处理情况,结果类似:
  219. * [
  220. * { "code" => <HttpCode int>, "data" => <Data> },
  221. * { "code" => <HttpCode int> },
  222. * { "code" => <HttpCode int> },
  223. * { "code" => <HttpCode int> },
  224. * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
  225. * ...
  226. * ]
  227. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
  228. */
  229. public function batch($operations)
  230. {
  231. $params = 'op=' . implode('&op=', $operations);
  232. return $this->rsPost('/batch', $params);
  233. }
  234. private function rsPost($path, $body = null)
  235. {
  236. $url = Config::RS_HOST . $path;
  237. return $this->post($url, $body);
  238. }
  239. private function rsGet($path)
  240. {
  241. $url = Config::RS_HOST . $path;
  242. return $this->get($url);
  243. }
  244. private function ioPost($path, $body = null)
  245. {
  246. $url = Config::IO_HOST . $path;
  247. return $this->post($url, $body);
  248. }
  249. private function get($url)
  250. {
  251. $headers = $this->auth->authorization($url);
  252. $ret = Client::get($url, $headers);
  253. if (!$ret->ok()) {
  254. return array(null, new Error($url, $ret));
  255. }
  256. return array($ret->json(), null);
  257. }
  258. private function post($url, $body)
  259. {
  260. $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
  261. $ret = Client::post($url, $body, $headers);
  262. if (!$ret->ok()) {
  263. return array(null, new Error($url, $ret));
  264. }
  265. $r = ($ret->body === null) ? array() : $ret->json();
  266. return array($r, null);
  267. }
  268. public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket)
  269. {
  270. return self::twoKeyBatch('copy', $source_bucket, $key_pairs, $target_bucket);
  271. }
  272. public static function buildBatchRename($bucket, $key_pairs)
  273. {
  274. return self::buildBatchMove($bucket, $key_pairs, $bucket);
  275. }
  276. public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket)
  277. {
  278. return self::twoKeyBatch('move', $source_bucket, $key_pairs, $target_bucket);
  279. }
  280. public static function buildBatchDelete($bucket, $keys)
  281. {
  282. return self::oneKeyBatch('delete', $bucket, $keys);
  283. }
  284. public static function buildBatchStat($bucket, $keys)
  285. {
  286. return self::oneKeyBatch('stat', $bucket, $keys);
  287. }
  288. private static function oneKeyBatch($operation, $bucket, $keys)
  289. {
  290. $data = array();
  291. foreach ($keys as $key) {
  292. array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
  293. }
  294. return $data;
  295. }
  296. private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket)
  297. {
  298. if ($target_bucket === null) {
  299. $target_bucket = $source_bucket;
  300. }
  301. $data = array();
  302. foreach ($key_pairs as $from_key => $to_key) {
  303. $from = \Qiniu\entry($source_bucket, $from_key);
  304. $to = \Qiniu\entry($target_bucket, $to_key);
  305. array_push($data, $operation . '/' . $from . '/' . $to);
  306. }
  307. return $data;
  308. }
  309. }