BucketManager.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. namespace Qiniu\Storage;
  3. use Qiniu\Auth;
  4. use Qiniu\Config;
  5. use Qiniu\Zone;
  6. use Qiniu\Http\Client;
  7. use Qiniu\Http\Error;
  8. /**
  9. * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
  10. *
  11. * @link https://developer.qiniu.com/kodo/api/1274/rs
  12. */
  13. final class BucketManager
  14. {
  15. private $auth;
  16. private $config;
  17. public function __construct(Auth $auth, Config $config = null)
  18. {
  19. $this->auth = $auth;
  20. if ($config == null) {
  21. $this->config = new Config();
  22. } else {
  23. $this->config = $config;
  24. }
  25. }
  26. /**
  27. * 获取指定账号下所有的空间名。
  28. *
  29. * @return string[] 包含所有空间名
  30. */
  31. public function buckets($shared = true)
  32. {
  33. $includeShared = "false";
  34. if ($shared === true) {
  35. $includeShared = "true";
  36. }
  37. return $this->rsGet('/buckets?shared=' . $includeShared);
  38. }
  39. /**
  40. * 获取指定空间绑定的所有的域名
  41. *
  42. * @return string[] 包含所有空间域名
  43. */
  44. public function domains($bucket)
  45. {
  46. return $this->apiGet('/v6/domain/list?tbl=' . $bucket);
  47. }
  48. /**
  49. * 获取空间绑定的域名列表
  50. * @return string[] 包含空间绑定的所有域名
  51. */
  52. /**
  53. * 列取空间的文件列表
  54. *
  55. * @param $bucket 空间名
  56. * @param $prefix 列举前缀
  57. * @param $marker 列举标识符
  58. * @param $limit 单次列举个数限制
  59. * @param $delimiter 指定目录分隔符
  60. *
  61. * @return array 包含文件信息的数组,类似:[
  62. * {
  63. * "hash" => "<Hash string>",
  64. * "key" => "<Key string>",
  65. * "fsize" => "<file size>",
  66. * "putTime" => "<file modify time>"
  67. * },
  68. * ...
  69. * ]
  70. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
  71. */
  72. public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null)
  73. {
  74. $query = array('bucket' => $bucket);
  75. \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
  76. \Qiniu\setWithoutEmpty($query, 'marker', $marker);
  77. \Qiniu\setWithoutEmpty($query, 'limit', $limit);
  78. \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
  79. $url = $this->getRsfHost() . '/list?' . http_build_query($query);
  80. return $this->get($url);
  81. }
  82. /**
  83. * 获取资源的元信息,但不返回文件内容
  84. *
  85. * @param $bucket 待获取信息资源所在的空间
  86. * @param $key 待获取资源的文件名
  87. *
  88. * @return array 包含文件信息的数组,类似:
  89. * [
  90. * "hash" => "<Hash string>",
  91. * "key" => "<Key string>",
  92. * "fsize" => <file size>,
  93. * "putTime" => "<file modify time>"
  94. * "fileType" => <file type>
  95. * ]
  96. *
  97. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html
  98. */
  99. public function stat($bucket, $key)
  100. {
  101. $path = '/stat/' . \Qiniu\entry($bucket, $key);
  102. return $this->rsGet($path);
  103. }
  104. /**
  105. * 删除指定资源
  106. *
  107. * @param $bucket 待删除资源所在的空间
  108. * @param $key 待删除资源的文件名
  109. *
  110. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  111. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html
  112. */
  113. public function delete($bucket, $key)
  114. {
  115. $path = '/delete/' . \Qiniu\entry($bucket, $key);
  116. list(, $error) = $this->rsPost($path);
  117. return $error;
  118. }
  119. /**
  120. * 给资源进行重命名,本质为move操作。
  121. *
  122. * @param $bucket 待操作资源所在空间
  123. * @param $oldname 待操作资源文件名
  124. * @param $newname 目标资源文件名
  125. *
  126. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  127. */
  128. public function rename($bucket, $oldname, $newname)
  129. {
  130. return $this->move($bucket, $oldname, $bucket, $newname);
  131. }
  132. /**
  133. * 给资源进行重命名,本质为move操作。
  134. *
  135. * @param $from_bucket 待操作资源所在空间
  136. * @param $from_key 待操作资源文件名
  137. * @param $to_bucket 目标资源空间名
  138. * @param $to_key 目标资源文件名
  139. *
  140. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  141. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html
  142. */
  143. public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
  144. {
  145. $from = \Qiniu\entry($from_bucket, $from_key);
  146. $to = \Qiniu\entry($to_bucket, $to_key);
  147. $path = '/copy/' . $from . '/' . $to;
  148. if ($force === true) {
  149. $path .= '/force/true';
  150. }
  151. list(, $error) = $this->rsPost($path);
  152. return $error;
  153. }
  154. /**
  155. * 将资源从一个空间到另一个空间
  156. *
  157. * @param $from_bucket 待操作资源所在空间
  158. * @param $from_key 待操作资源文件名
  159. * @param $to_bucket 目标资源空间名
  160. * @param $to_key 目标资源文件名
  161. *
  162. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  163. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html
  164. */
  165. public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
  166. {
  167. $from = \Qiniu\entry($from_bucket, $from_key);
  168. $to = \Qiniu\entry($to_bucket, $to_key);
  169. $path = '/move/' . $from . '/' . $to;
  170. if ($force) {
  171. $path .= '/force/true';
  172. }
  173. list(, $error) = $this->rsPost($path);
  174. return $error;
  175. }
  176. /**
  177. * 主动修改指定资源的文件类型
  178. *
  179. * @param $bucket 待操作资源所在空间
  180. * @param $key 待操作资源文件名
  181. * @param $mime 待操作文件目标mimeType
  182. *
  183. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  184. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html
  185. */
  186. public function changeMime($bucket, $key, $mime)
  187. {
  188. $resource = \Qiniu\entry($bucket, $key);
  189. $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
  190. $path = '/chgm/' . $resource . '/mime/' . $encode_mime;
  191. list(, $error) = $this->rsPost($path);
  192. return $error;
  193. }
  194. /**
  195. * 修改指定资源的存储类型
  196. *
  197. * @param $bucket 待操作资源所在空间
  198. * @param $key 待操作资源文件名
  199. * @param $fileType 待操作文件目标文件类型
  200. *
  201. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  202. * @link https://developer.qiniu.com/kodo/api/3710/modify-the-file-type
  203. */
  204. public function changeType($bucket, $key, $fileType)
  205. {
  206. $resource = \Qiniu\entry($bucket, $key);
  207. $path = '/chtype/' . $resource . '/type/' . $fileType;
  208. list(, $error) = $this->rsPost($path);
  209. return $error;
  210. }
  211. /**
  212. * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换
  213. *
  214. * @param $bucket 待操作资源所在空间
  215. * @param $key 待操作资源文件名
  216. * @param $status 待操作文件目标文件类型
  217. *
  218. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  219. * @link https://developer.qiniu.com/kodo/api/4173/modify-the-file-status
  220. */
  221. public function changeStatus($bucket, $key, $status)
  222. {
  223. $resource = \Qiniu\entry($bucket, $key);
  224. $path = '/chstatus/' . $resource . '/status/' . $status;
  225. list(, $error) = $this->rsPost($path);
  226. return $error;
  227. }
  228. /**
  229. * 从指定URL抓取资源,并将该资源存储到指定空间中
  230. *
  231. * @param $url 指定的URL
  232. * @param $bucket 目标资源空间
  233. * @param $key 目标资源文件名
  234. *
  235. * @return array 包含已拉取的文件信息。
  236. * 成功时: [
  237. * [
  238. * "hash" => "<Hash string>",
  239. * "key" => "<Key string>"
  240. * ],
  241. * null
  242. * ]
  243. *
  244. * 失败时: [
  245. * null,
  246. * Qiniu/Http/Error
  247. * ]
  248. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html
  249. */
  250. public function fetch($url, $bucket, $key = null)
  251. {
  252. $resource = \Qiniu\base64_urlSafeEncode($url);
  253. $to = \Qiniu\entry($bucket, $key);
  254. $path = '/fetch/' . $resource . '/to/' . $to;
  255. $ak = $this->auth->getAccessKey();
  256. $ioHost = $this->config->getIovipHost($ak, $bucket);
  257. $url = $ioHost . $path;
  258. return $this->post($url, null);
  259. }
  260. /**
  261. * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
  262. *
  263. * @param $bucket 待获取资源所在的空间
  264. * @param $key 代获取资源文件名
  265. *
  266. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  267. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html
  268. */
  269. public function prefetch($bucket, $key)
  270. {
  271. $resource = \Qiniu\entry($bucket, $key);
  272. $path = '/prefetch/' . $resource;
  273. $ak = $this->auth->getAccessKey();
  274. $ioHost = $this->config->getIovipHost($ak, $bucket);
  275. $url = $ioHost . $path;
  276. list(, $error) = $this->post($url, null);
  277. return $error;
  278. }
  279. /**
  280. * 在单次请求中进行多个资源管理操作
  281. *
  282. * @param $operations 资源管理操作数组
  283. *
  284. * @return array 每个资源的处理情况,结果类似:
  285. * [
  286. * { "code" => <HttpCode int>, "data" => <Data> },
  287. * { "code" => <HttpCode int> },
  288. * { "code" => <HttpCode int> },
  289. * { "code" => <HttpCode int> },
  290. * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
  291. * ...
  292. * ]
  293. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
  294. */
  295. public function batch($operations)
  296. {
  297. $params = 'op=' . implode('&op=', $operations);
  298. return $this->rsPost('/batch', $params);
  299. }
  300. /**
  301. * 设置文件的生命周期
  302. *
  303. * @param $bucket 设置文件生命周期文件所在的空间
  304. * @param $key 设置文件生命周期文件的文件名
  305. * @param $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期
  306. *
  307. * @return Mixed
  308. * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle
  309. */
  310. public function deleteAfterDays($bucket, $key, $days)
  311. {
  312. $entry = \Qiniu\entry($bucket, $key);
  313. $path = "/deleteAfterDays/$entry/$days";
  314. list(, $error) = $this->rsPost($path);
  315. return $error;
  316. }
  317. private function getRsfHost()
  318. {
  319. $scheme = "http://";
  320. if ($this->config->useHTTPS == true) {
  321. $scheme = "https://";
  322. }
  323. return $scheme . Config::RSF_HOST;
  324. }
  325. private function getRsHost()
  326. {
  327. $scheme = "http://";
  328. if ($this->config->useHTTPS == true) {
  329. $scheme = "https://";
  330. }
  331. return $scheme . Config::RS_HOST;
  332. }
  333. private function getApiHost()
  334. {
  335. $scheme = "http://";
  336. if ($this->config->useHTTPS == true) {
  337. $scheme = "https://";
  338. }
  339. return $scheme . Config::API_HOST;
  340. }
  341. private function rsPost($path, $body = null)
  342. {
  343. $url = $this->getRsHost() . $path;
  344. return $this->post($url, $body);
  345. }
  346. private function apiGet($path)
  347. {
  348. $url = $this->getApiHost() . $path;
  349. return $this->get($url);
  350. }
  351. private function rsGet($path)
  352. {
  353. $url = $this->getRsHost() . $path;
  354. return $this->get($url);
  355. }
  356. private function get($url)
  357. {
  358. $headers = $this->auth->authorization($url);
  359. $ret = Client::get($url, $headers);
  360. if (!$ret->ok()) {
  361. return array(null, new Error($url, $ret));
  362. }
  363. return array($ret->json(), null);
  364. }
  365. private function post($url, $body)
  366. {
  367. $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
  368. $ret = Client::post($url, $body, $headers);
  369. if (!$ret->ok()) {
  370. return array(null, new Error($url, $ret));
  371. }
  372. $r = ($ret->body === null) ? array() : $ret->json();
  373. return array($r, null);
  374. }
  375. public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
  376. {
  377. return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
  378. }
  379. public static function buildBatchRename($bucket, $key_pairs, $force)
  380. {
  381. return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
  382. }
  383. public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
  384. {
  385. return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
  386. }
  387. public static function buildBatchDelete($bucket, $keys)
  388. {
  389. return self::oneKeyBatch('/delete', $bucket, $keys);
  390. }
  391. public static function buildBatchStat($bucket, $keys)
  392. {
  393. return self::oneKeyBatch('/stat', $bucket, $keys);
  394. }
  395. public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
  396. {
  397. $data = array();
  398. foreach ($key_day_pairs as $key => $day) {
  399. array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
  400. }
  401. return $data;
  402. }
  403. public static function buildBatchChangeMime($bucket, $key_mime_pairs)
  404. {
  405. $data = array();
  406. foreach ($key_mime_pairs as $key => $mime) {
  407. array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
  408. }
  409. return $data;
  410. }
  411. public static function buildBatchChangeType($bucket, $key_type_pairs)
  412. {
  413. $data = array();
  414. foreach ($key_type_pairs as $key => $type) {
  415. array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
  416. }
  417. return $data;
  418. }
  419. private static function oneKeyBatch($operation, $bucket, $keys)
  420. {
  421. $data = array();
  422. foreach ($keys as $key) {
  423. array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
  424. }
  425. return $data;
  426. }
  427. private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
  428. {
  429. if ($target_bucket === null) {
  430. $target_bucket = $source_bucket;
  431. }
  432. $data = array();
  433. $forceOp = "false";
  434. if ($force) {
  435. $forceOp = "true";
  436. }
  437. foreach ($key_pairs as $from_key => $to_key) {
  438. $from = \Qiniu\entry($source_bucket, $from_key);
  439. $to = \Qiniu\entry($target_bucket, $to_key);
  440. array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
  441. }
  442. return $data;
  443. }
  444. }