BucketManager.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. * 从指定URL抓取资源,并将该资源存储到指定空间中
  213. *
  214. * @param $url 指定的URL
  215. * @param $bucket 目标资源空间
  216. * @param $key 目标资源文件名
  217. *
  218. * @return array 包含已拉取的文件信息。
  219. * 成功时: [
  220. * [
  221. * "hash" => "<Hash string>",
  222. * "key" => "<Key string>"
  223. * ],
  224. * null
  225. * ]
  226. *
  227. * 失败时: [
  228. * null,
  229. * Qiniu/Http/Error
  230. * ]
  231. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html
  232. */
  233. public function fetch($url, $bucket, $key = null)
  234. {
  235. $resource = \Qiniu\base64_urlSafeEncode($url);
  236. $to = \Qiniu\entry($bucket, $key);
  237. $path = '/fetch/' . $resource . '/to/' . $to;
  238. $ak = $this->auth->getAccessKey();
  239. $ioHost = $this->config->getIovipHost($ak, $bucket);
  240. $url = $ioHost . $path;
  241. return $this->post($url, null);
  242. }
  243. /**
  244. * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
  245. *
  246. * @param $bucket 待获取资源所在的空间
  247. * @param $key 代获取资源文件名
  248. *
  249. * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error
  250. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html
  251. */
  252. public function prefetch($bucket, $key)
  253. {
  254. $resource = \Qiniu\entry($bucket, $key);
  255. $path = '/prefetch/' . $resource;
  256. $ak = $this->auth->getAccessKey();
  257. $ioHost = $this->config->getIovipHost($ak, $bucket);
  258. $url = $ioHost . $path;
  259. list(, $error) = $this->post($url, null);
  260. return $error;
  261. }
  262. /**
  263. * 在单次请求中进行多个资源管理操作
  264. *
  265. * @param $operations 资源管理操作数组
  266. *
  267. * @return array 每个资源的处理情况,结果类似:
  268. * [
  269. * { "code" => <HttpCode int>, "data" => <Data> },
  270. * { "code" => <HttpCode int> },
  271. * { "code" => <HttpCode int> },
  272. * { "code" => <HttpCode int> },
  273. * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
  274. * ...
  275. * ]
  276. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
  277. */
  278. public function batch($operations)
  279. {
  280. $params = 'op=' . implode('&op=', $operations);
  281. return $this->rsPost('/batch', $params);
  282. }
  283. /**
  284. * 设置文件的生命周期
  285. *
  286. * @param $bucket 设置文件生命周期文件所在的空间
  287. * @param $key 设置文件生命周期文件的文件名
  288. * @param $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期
  289. *
  290. * @return Mixed
  291. * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle
  292. */
  293. public function deleteAfterDays($bucket, $key, $days)
  294. {
  295. $entry = \Qiniu\entry($bucket, $key);
  296. $path = "/deleteAfterDays/$entry/$days";
  297. list(, $error) = $this->rsPost($path);
  298. return $error;
  299. }
  300. private function getRsfHost()
  301. {
  302. $scheme = "http://";
  303. if ($this->config->useHTTPS == true) {
  304. $scheme = "https://";
  305. }
  306. return $scheme . Config::RSF_HOST;
  307. }
  308. private function getRsHost()
  309. {
  310. $scheme = "http://";
  311. if ($this->config->useHTTPS == true) {
  312. $scheme = "https://";
  313. }
  314. return $scheme . Config::RS_HOST;
  315. }
  316. private function getApiHost()
  317. {
  318. $scheme = "http://";
  319. if ($this->config->useHTTPS == true) {
  320. $scheme = "https://";
  321. }
  322. return $scheme . Config::API_HOST;
  323. }
  324. private function rsPost($path, $body = null)
  325. {
  326. $url = $this->getRsHost() . $path;
  327. return $this->post($url, $body);
  328. }
  329. private function apiGet($path)
  330. {
  331. $url = $this->getApiHost() . $path;
  332. return $this->get($url);
  333. }
  334. private function rsGet($path)
  335. {
  336. $url = $this->getRsHost() . $path;
  337. return $this->get($url);
  338. }
  339. private function get($url)
  340. {
  341. $headers = $this->auth->authorization($url);
  342. $ret = Client::get($url, $headers);
  343. if (!$ret->ok()) {
  344. return array(null, new Error($url, $ret));
  345. }
  346. return array($ret->json(), null);
  347. }
  348. private function post($url, $body)
  349. {
  350. $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
  351. $ret = Client::post($url, $body, $headers);
  352. if (!$ret->ok()) {
  353. return array(null, new Error($url, $ret));
  354. }
  355. $r = ($ret->body === null) ? array() : $ret->json();
  356. return array($r, null);
  357. }
  358. public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
  359. {
  360. return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
  361. }
  362. public static function buildBatchRename($bucket, $key_pairs, $force)
  363. {
  364. return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
  365. }
  366. public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
  367. {
  368. return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
  369. }
  370. public static function buildBatchDelete($bucket, $keys)
  371. {
  372. return self::oneKeyBatch('/delete', $bucket, $keys);
  373. }
  374. public static function buildBatchStat($bucket, $keys)
  375. {
  376. return self::oneKeyBatch('/stat', $bucket, $keys);
  377. }
  378. public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
  379. {
  380. $data = array();
  381. foreach ($key_day_pairs as $key => $day) {
  382. array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
  383. }
  384. return $data;
  385. }
  386. public static function buildBatchChangeMime($bucket, $key_mime_pairs)
  387. {
  388. $data = array();
  389. foreach ($key_mime_pairs as $key => $mime) {
  390. array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
  391. }
  392. return $data;
  393. }
  394. public static function buildBatchChangeType($bucket, $key_type_pairs)
  395. {
  396. $data = array();
  397. foreach ($key_type_pairs as $key => $type) {
  398. array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
  399. }
  400. return $data;
  401. }
  402. private static function oneKeyBatch($operation, $bucket, $keys)
  403. {
  404. $data = array();
  405. foreach ($keys as $key) {
  406. array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
  407. }
  408. return $data;
  409. }
  410. private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
  411. {
  412. if ($target_bucket === null) {
  413. $target_bucket = $source_bucket;
  414. }
  415. $data = array();
  416. $forceOp = "false";
  417. if ($force) {
  418. $forceOp = "true";
  419. }
  420. foreach ($key_pairs as $from_key => $to_key) {
  421. $from = \Qiniu\entry($source_bucket, $from_key);
  422. $to = \Qiniu\entry($target_bucket, $to_key);
  423. array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
  424. }
  425. return $data;
  426. }
  427. }