BucketManager.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. <?php
  2. namespace Qiniu\Storage;
  3. use Qiniu\Auth;
  4. use Qiniu\Config;
  5. use Qiniu\Http\Error;
  6. use Qiniu\Http\Client;
  7. use Qiniu\Http\Proxy;
  8. use Qiniu\Http\Response;
  9. /**
  10. * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
  11. *
  12. * @link https://developer.qiniu.com/kodo/api/1274/rs
  13. */
  14. final class BucketManager
  15. {
  16. private $auth;
  17. private $config;
  18. private $proxy;
  19. public function __construct(
  20. Auth $auth,
  21. Config $config = null,
  22. $proxy = null,
  23. $proxy_auth = null,
  24. $proxy_user_password = null
  25. ) {
  26. $this->auth = $auth;
  27. if ($config == null) {
  28. $this->config = new Config();
  29. } else {
  30. $this->config = $config;
  31. }
  32. $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
  33. }
  34. /**
  35. * 获取指定账号下所有的空间名
  36. *
  37. * @param bool $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
  38. * @return array 包含所有空间名
  39. */
  40. public function buckets($shared = true)
  41. {
  42. $includeShared = "false";
  43. if ($shared === true) {
  44. $includeShared = "true";
  45. }
  46. return $this->getV2($this->config->getUcHost() . '/buckets?shared=' . $includeShared);
  47. }
  48. /**
  49. * 列举空间,返回bucket列表
  50. *
  51. * @param string $region 区域
  52. * @param string $line
  53. * @param string $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
  54. * @return array
  55. */
  56. public function listbuckets(
  57. $region = null,
  58. $line = 'false',
  59. $shared = 'false'
  60. ) {
  61. $path = '/v3/buckets?region=' . $region . '&line=' . $line . '&shared=' . $shared;
  62. return $this->ucPost($path);
  63. }
  64. /**
  65. * 创建空间
  66. *
  67. * @param string $name 创建的空间名
  68. * @param string $region 创建的区域,默认华东
  69. *
  70. * @return array
  71. * @link https://developer.qiniu.com/kodo/api/1382/mkbucketv3
  72. */
  73. public function createBucket($name, $region = 'z0')
  74. {
  75. $path = '/mkbucketv3/' . $name . '/region/' . $region;
  76. return $this->postV2($this->config->getUcHost() . $path, null);
  77. }
  78. /**
  79. * 删除空间
  80. *
  81. * @param string $name 需要删除的目标空间名
  82. *
  83. * @return array
  84. * @link https://developer.qiniu.com/kodo/api/1601/drop-bucket
  85. */
  86. public function deleteBucket($name)
  87. {
  88. $path = '/drop/' . $name;
  89. return $this->postV2($this->config->getUcHost() . $path, null);
  90. }
  91. /**
  92. * 获取指定空间绑定的所有的域名
  93. *
  94. * @param string $bucket 空间名称
  95. * @return array
  96. */
  97. public function domains($bucket)
  98. {
  99. return $this->ucGet('/v2/domains?tbl=' . $bucket);
  100. }
  101. /**
  102. * 获取指定空间的相关信息
  103. *
  104. * @param string $bucket 空间名称
  105. * @return array
  106. */
  107. public function bucketInfo($bucket)
  108. {
  109. $path = '/v2/bucketInfo?bucket=' . $bucket;
  110. return $this->ucPost($path);
  111. }
  112. /**
  113. * 获取指定zone的空间信息列表
  114. *
  115. * @param string $region 区域
  116. * @param string $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
  117. * @param string $fs 如果为 true,会返回每个空间当前的文件数和存储量(实时数据)
  118. * @return array
  119. */
  120. public function bucketInfos($region = null, $shared = 'false', $fs = 'false')
  121. {
  122. $path = '/v2/bucketInfos?region=' . $region . '&shared=' . $shared . '&fs=' . $fs;
  123. return $this->ucPost($path);
  124. }
  125. /**
  126. * 列取空间的文件列表
  127. *
  128. * @param string $bucket 空间名
  129. * @param string $prefix 列举前缀
  130. * @param string $marker 列举标识符
  131. * @param int $limit 单次列举个数限制
  132. * @param string $delimiter 指定目录分隔符
  133. *
  134. * @return array
  135. * @link https://developer.qiniu.com/kodo/api/1284/list
  136. */
  137. public function listFiles(
  138. $bucket,
  139. $prefix = null,
  140. $marker = null,
  141. $limit = 1000,
  142. $delimiter = null
  143. ) {
  144. $query = array('bucket' => $bucket);
  145. \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
  146. \Qiniu\setWithoutEmpty($query, 'marker', $marker);
  147. \Qiniu\setWithoutEmpty($query, 'limit', $limit);
  148. \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
  149. return $this->rsfGet($bucket, '/list?' . http_build_query($query));
  150. }
  151. /**
  152. * 列取空间的文件列表
  153. *
  154. * @deprecated API 可能返回仅包含 marker,不包含 item 或 dir 的项,请使用 {@link listFiles}
  155. *
  156. * @param string $bucket 空间名
  157. * @param string $prefix 列举前缀
  158. * @param string $marker 列举标识符
  159. * @param int $limit 单次列举个数限制
  160. * @param string $delimiter 指定目录分隔符
  161. * @param bool $skipconfirm 是否跳过已删除条目的确认机制
  162. *
  163. * @return array
  164. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
  165. */
  166. public function listFilesv2(
  167. $bucket,
  168. $prefix = null,
  169. $marker = null,
  170. $limit = 1000,
  171. $delimiter = null,
  172. $skipconfirm = true
  173. ) {
  174. $query = array('bucket' => $bucket);
  175. \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
  176. \Qiniu\setWithoutEmpty($query, 'marker', $marker);
  177. \Qiniu\setWithoutEmpty($query, 'limit', $limit);
  178. \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
  179. \Qiniu\setWithoutEmpty($query, 'skipconfirm', $skipconfirm);
  180. $path = '/v2/list?' . http_build_query($query);
  181. list($host, $err) = $this->config->getRsfHostV2(
  182. $this->auth->getAccessKey(),
  183. $bucket,
  184. $this->proxy->makeReqOpt()
  185. );
  186. if ($err != null) {
  187. return array(null, $err);
  188. }
  189. $url = $host . $path;
  190. $headers = $this->auth->authorizationV2($url, 'POST', null, 'application/x-www-form-urlencoded');
  191. $ret = Client::post($url, null, $headers, $this->proxy->makeReqOpt());
  192. if (!$ret->ok()) {
  193. return array(null, new Error($url, $ret));
  194. }
  195. $r = explode("\n", $ret->body);
  196. array_pop($r);
  197. return array($r, null);
  198. }
  199. /**
  200. * 增加bucket生命规则
  201. *
  202. * @param string $bucket
  203. * 空间名
  204. * @param string $name
  205. * 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
  206. * @param string $prefix
  207. * 同一个 bucket 里面前缀不能重复
  208. * @param int $delete_after_days
  209. * 指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除。
  210. * 需大于 to_line_after_days
  211. * @param int $to_line_after_days
  212. * 指定文件上传多少天后转低频存储。指定为0表示不转低频存储
  213. * @param int $to_archive_ir_after_days
  214. * 指定文件上传多少天后转归档直读。指定为0表示不转归档直读
  215. * @param int $to_archive_after_days
  216. * 指定文件上传多少天后转归档存储。指定为0表示不转归档存储
  217. * @param int $to_deep_archive_after_days
  218. * 指定文件上传多少天后转深度归档存储。指定为0表示不转深度归档存储
  219. * @return array
  220. */
  221. public function bucketLifecycleRule(
  222. $bucket,
  223. $name,
  224. $prefix,
  225. $delete_after_days = null,
  226. $to_line_after_days = null,
  227. $to_archive_after_days = null,
  228. $to_deep_archive_after_days = null,
  229. $to_archive_ir_after_days = null
  230. ) {
  231. $path = '/rules/add';
  232. $params = array();
  233. if ($bucket) {
  234. $params['bucket'] = $bucket;
  235. }
  236. if ($name) {
  237. $params['name'] = $name;
  238. }
  239. if ($prefix) {
  240. $params['prefix'] = $prefix;
  241. }
  242. if ($delete_after_days) {
  243. $params['delete_after_days'] = $delete_after_days;
  244. }
  245. if ($to_line_after_days) {
  246. $params['to_line_after_days'] = $to_line_after_days;
  247. }
  248. if ($to_archive_ir_after_days) {
  249. $params['to_archive_ir_after_days'] = $to_archive_ir_after_days;
  250. }
  251. if ($to_archive_after_days) {
  252. $params['to_archive_after_days'] = $to_archive_after_days;
  253. }
  254. if ($to_deep_archive_after_days) {
  255. $params['to_deep_archive_after_days'] = $to_deep_archive_after_days;
  256. }
  257. $data = http_build_query($params);
  258. $info = $this->ucPost($path, $data);
  259. return $info;
  260. }
  261. /**
  262. * 更新bucket生命规则
  263. *
  264. * @param string $bucket
  265. * 空间名
  266. * @param string $name
  267. * 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
  268. * @param string $prefix
  269. * 同一个 bucket 里面前缀不能重复
  270. * @param int $delete_after_days
  271. * 指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除
  272. * 需大于 to_line_after_days
  273. * @param int $to_line_after_days
  274. * 指定文件上传多少天后转低频存储。指定为0表示不转低频存储
  275. * @param int $to_archive_ir_after_days
  276. * 指定文件上传多少天后转归档只读。指定为0表示不转归档只读
  277. * @param int $to_archive_after_days
  278. * 指定文件上传多少天后转归档存储。指定为0表示不转归档存储
  279. * @param int $to_deep_archive_after_days
  280. * 指定文件上传多少天后转深度归档存储。指定为0表示不转深度归档存储
  281. * @return array
  282. */
  283. public function updateBucketLifecycleRule(
  284. $bucket,
  285. $name,
  286. $prefix,
  287. $delete_after_days = null,
  288. $to_line_after_days = null,
  289. $to_archive_after_days = null,
  290. $to_deep_archive_after_days = null,
  291. $to_archive_ir_after_days = null
  292. ) {
  293. $path = '/rules/update';
  294. $params = array();
  295. if ($bucket) {
  296. $params['bucket'] = $bucket;
  297. }
  298. if ($name) {
  299. $params['name'] = $name;
  300. }
  301. if ($prefix) {
  302. $params['prefix'] = $prefix;
  303. }
  304. if ($delete_after_days) {
  305. $params['delete_after_days'] = $delete_after_days;
  306. }
  307. if ($to_line_after_days) {
  308. $params['to_line_after_days'] = $to_line_after_days;
  309. }
  310. if ($to_archive_ir_after_days) {
  311. $params['to_archive_ir_after_days'] = $to_archive_ir_after_days;
  312. }
  313. if ($to_archive_after_days) {
  314. $params['to_archive_after_days'] = $to_archive_after_days;
  315. }
  316. if ($to_deep_archive_after_days) {
  317. $params['to_deep_archive_after_days'] = $to_deep_archive_after_days;
  318. }
  319. $data = http_build_query($params);
  320. return $this->ucPost($path, $data);
  321. }
  322. /**
  323. * 获取bucket生命规则
  324. *
  325. * @param string $bucket 空间名
  326. * @return array
  327. */
  328. public function getBucketLifecycleRules($bucket)
  329. {
  330. $path = '/rules/get?bucket=' . $bucket;
  331. $info = $this->ucGet($path);
  332. return $info;
  333. }
  334. /**
  335. * 删除bucket生命规则
  336. *
  337. * @param string $bucket 空间名
  338. * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
  339. * 只能为字母、数字、下划线()
  340. * @return array
  341. */
  342. public function deleteBucketLifecycleRule($bucket, $name)
  343. {
  344. $path = '/rules/delete';
  345. $params = array();
  346. if ($bucket) {
  347. $params['bucket'] = $bucket;
  348. }
  349. if ($name) {
  350. $params['name'] = $name;
  351. }
  352. $data = http_build_query($params);
  353. $info = $this->ucPost($path, $data);
  354. return $info;
  355. }
  356. /**
  357. * 增加bucket事件通知规则
  358. *
  359. * @param string $bucket 空间名
  360. * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
  361. * 只能为字母、数字、下划线()
  362. * @param string $prefix 同一个 bucket 里面前缀不能重复
  363. * @param string $suffix 可选,文件配置的后缀
  364. * @param array $event 事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,
  365. * disable,enable,deleteMarkerCreate
  366. * @param string $callbackURL 通知URL,可以指定多个,失败依次重试
  367. * @param string $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
  368. * @param string $host 可选,通知请求的host
  369. *
  370. * @return array
  371. */
  372. public function putBucketEvent(
  373. $bucket,
  374. $name,
  375. $prefix,
  376. $suffix,
  377. $event,
  378. $callbackURL,
  379. $access_key = null,
  380. $host = null
  381. ) {
  382. $path = '/events/add';
  383. $params = array();
  384. if (!empty($bucket)) {
  385. $params['bucket'] = $bucket;
  386. }
  387. if (!empty($name)) {
  388. $params['name'] = $name;
  389. }
  390. if (!empty($prefix)) {
  391. $params['prefix'] = $prefix;
  392. }
  393. if (!empty($suffix)) {
  394. $params['suffix'] = $suffix;
  395. }
  396. if (!empty($callbackURL)) {
  397. $params['callbackURL'] = $callbackURL;
  398. }
  399. if (!empty($access_key)) {
  400. $params['access_key'] = $access_key;
  401. }
  402. if (!empty($host)) {
  403. $params['host'] = $host;
  404. }
  405. $data = http_build_query($params);
  406. if (!empty($event)) {
  407. $eventpath = "";
  408. foreach ($event as $key => $value) {
  409. $eventpath .= "&event=$value";
  410. }
  411. $data .= $eventpath;
  412. }
  413. $info = $this->ucPost($path, $data);
  414. return $info;
  415. }
  416. /**
  417. * 更新bucket事件通知规则
  418. *
  419. * @param string $bucket 空间名
  420. * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
  421. * 只能为字母、数字、下划线()
  422. * @param string $prefix 同一个 bucket 里面前缀不能重复
  423. * @param string $suffix 可选,文件配置的后缀
  424. * @param array $event 事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,disable,
  425. * enable,deleteMarkerCreate
  426. * @param string $callbackURL 通知URL,可以指定多个,失败依次重试
  427. * @param string $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
  428. * @param string $host 可选,通知请求的host
  429. *
  430. * @return array
  431. */
  432. public function updateBucketEvent(
  433. $bucket,
  434. $name,
  435. $prefix,
  436. $suffix,
  437. $event,
  438. $callbackURL,
  439. $access_key = null,
  440. $host = null
  441. ) {
  442. $path = '/events/update';
  443. $params = array();
  444. if (!empty($bucket)) {
  445. $params['bucket'] = $bucket;
  446. }
  447. if (!empty($name)) {
  448. $params['name'] = $name;
  449. }
  450. if (!empty($prefix)) {
  451. $params['prefix'] = $prefix;
  452. }
  453. if ($suffix) {
  454. $params['suffix'] = $suffix;
  455. }
  456. if (!empty($event)) {
  457. $params['event'] = $event;
  458. }
  459. if (!empty($callbackURL)) {
  460. $params['callbackURL'] = $callbackURL;
  461. }
  462. if (!empty($access_key)) {
  463. $params['access_key'] = $access_key;
  464. }
  465. if (!empty($host)) {
  466. $params['host'] = $host;
  467. }
  468. $data = http_build_query($params);
  469. if (!empty($event)) {
  470. $eventpath = "";
  471. foreach ($event as $key => $value) {
  472. $eventpath .= "&event=$value";
  473. }
  474. $data .= $eventpath;
  475. }
  476. return $this->ucPost($path, $data);
  477. }
  478. /**
  479. * 获取bucket事件通知规则
  480. *
  481. * @param string $bucket 空间名
  482. * @return array
  483. */
  484. public function getBucketEvents($bucket)
  485. {
  486. $path = '/events/get?bucket=' . $bucket;
  487. return $this->ucGet($path);
  488. }
  489. /**
  490. * 删除bucket事件通知规则
  491. *
  492. * @param string $bucket 空间名
  493. * @param string $name 规则名称bucket内唯一,长度小于50,不能为空,只能为字母、数字、下划线
  494. * @return array
  495. */
  496. public function deleteBucketEvent($bucket, $name)
  497. {
  498. $path = '/events/delete';
  499. $params = array();
  500. if ($bucket) {
  501. $params['bucket'] = $bucket;
  502. }
  503. if ($name) {
  504. $params['name'] = $name;
  505. }
  506. $data = http_build_query($params);
  507. return $this->ucPost($path, $data);
  508. }
  509. /**
  510. * 获取bucket的跨域信息
  511. *
  512. * @param string $bucket 空间名
  513. * @return array
  514. */
  515. public function getCorsRules($bucket)
  516. {
  517. $path = '/corsRules/get/' . $bucket;
  518. return $this->ucGet($path);
  519. }
  520. /**
  521. * 开关原图保护
  522. *
  523. * @param string $bucket 空间名称
  524. * @param int $mode mode 为1表示开启原图保护,0表示关闭
  525. * @return array
  526. */
  527. public function putBucketAccessStyleMode($bucket, $mode)
  528. {
  529. $path = '/accessMode/' . $bucket . '/mode/' . $mode;
  530. return $this->ucPost($path, null);
  531. }
  532. /**
  533. * 设置私有属性
  534. *
  535. * @param string $bucket 空间名称
  536. * @param int $private private为0表示公开,为1表示私有
  537. * @return array
  538. */
  539. public function putBucketAccessMode($bucket, $private)
  540. {
  541. $path = "/private?bucket=$bucket&private=$private";
  542. return $this->ucPost($path, null);
  543. }
  544. /**
  545. * 设置 referer 防盗链
  546. *
  547. * @param string $bucket 空间名称
  548. * @param int $mode 0:关闭Referer(使用此选项将会忽略以下参数并将恢复默认值);
  549. * 1:设置Referer白名单; 2:设置Referer黑名单
  550. * @param string $norefer 0:不允许空 Refer 访问; 1:表示允许空Refer访问
  551. * @param string $pattern 规则字符串
  552. * @param int $enabled 源站是否支持,默认为0只给CDN配置, 设置为1表示开启源站防盗链
  553. * @return array
  554. * @link https://developer.qiniu.com/kodo/manual/6093/set-the-hotlinking-prevention
  555. */
  556. public function putReferAntiLeech($bucket, $mode, $norefer, $pattern, $enabled = 1)
  557. {
  558. $path = "/referAntiLeech?bucket=$bucket&mode=$mode&norefer=$norefer&pattern=$pattern&source_enabled=$enabled";
  559. return $this->ucPost($path, null);
  560. }
  561. /**
  562. * 设置Bucket的maxAge
  563. *
  564. * @param string $bucket 空间名称
  565. * @param int $maxAge maxAge为0或者负数表示为默认值(31536000)
  566. * @return array
  567. */
  568. public function putBucketMaxAge($bucket, $maxAge)
  569. {
  570. $path = '/maxAge?bucket=' . $bucket . '&maxAge=' . $maxAge;
  571. return $this->ucPost($path, null);
  572. }
  573. /**
  574. * 设置空间配额
  575. *
  576. * @param string $bucket 空间名称,不支持授权空间
  577. * @param string $size 空间存储量配额,参数传入0或不传表示不更改当前配置,传入-1表示取消限额,新创建的空间默认没有限额
  578. * @param string $count 空间文件数配额,参数含义同<size>
  579. * @return array
  580. */
  581. public function putBucketQuota($bucket, $size, $count)
  582. {
  583. $path = '/setbucketquota/' . $bucket . '/size/' . $size . '/count/' . $count;
  584. return $this->apiPost($bucket, $path);
  585. }
  586. /**
  587. * 获取空间配额
  588. *
  589. * @param string $bucket 空间名称
  590. * @return array
  591. */
  592. public function getBucketQuota($bucket)
  593. {
  594. $path = '/getbucketquota/' . $bucket;
  595. return $this->apiPost($bucket, $path);
  596. }
  597. /**
  598. * 获取资源的元信息,但不返回文件内容
  599. *
  600. * @param string $bucket 待获取信息资源所在的空间
  601. * @param string $key 待获取资源的文件名
  602. *
  603. * @return array
  604. * @link https://developer.qiniu.com/kodo/api/1308/stat
  605. */
  606. public function stat($bucket, $key)
  607. {
  608. $path = '/stat/' . \Qiniu\entry($bucket, $key);
  609. return $this->rsGet($bucket, $path);
  610. }
  611. /**
  612. * 删除指定资源
  613. *
  614. * @param string $bucket 待删除资源所在的空间
  615. * @param string $key 待删除资源的文件名
  616. *
  617. * @return array
  618. * @link https://developer.qiniu.com/kodo/api/1257/delete
  619. */
  620. public function delete($bucket, $key)
  621. {
  622. $path = '/delete/' . \Qiniu\entry($bucket, $key);
  623. return $this->rsPost($bucket, $path);
  624. }
  625. /**
  626. * 给资源进行重命名,本质为move操作。
  627. *
  628. * @param string $bucket 待操作资源所在空间
  629. * @param string $oldname 待操作资源文件名
  630. * @param string $newname 目标资源文件名
  631. *
  632. * @return array
  633. */
  634. public function rename($bucket, $oldname, $newname)
  635. {
  636. return $this->move($bucket, $oldname, $bucket, $newname);
  637. }
  638. /**
  639. * 对资源进行复制。
  640. *
  641. * @param string $from_bucket 待操作资源所在空间
  642. * @param string $from_key 待操作资源文件名
  643. * @param string $to_bucket 目标资源空间名
  644. * @param string $to_key 目标资源文件名
  645. *
  646. * @return array
  647. * @link https://developer.qiniu.com/kodo/api/1254/copy
  648. */
  649. public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
  650. {
  651. $from = \Qiniu\entry($from_bucket, $from_key);
  652. $to = \Qiniu\entry($to_bucket, $to_key);
  653. $path = '/copy/' . $from . '/' . $to;
  654. if ($force === true) {
  655. $path .= '/force/true';
  656. }
  657. return $this->rsPost($from_bucket, $path);
  658. }
  659. /**
  660. * 将资源从一个空间到另一个空间
  661. *
  662. * @param string $from_bucket 待操作资源所在空间
  663. * @param string $from_key 待操作资源文件名
  664. * @param string $to_bucket 目标资源空间名
  665. * @param string $to_key 目标资源文件名
  666. *
  667. * @return array
  668. * @link https://developer.qiniu.com/kodo/api/1288/move
  669. */
  670. public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
  671. {
  672. $from = \Qiniu\entry($from_bucket, $from_key);
  673. $to = \Qiniu\entry($to_bucket, $to_key);
  674. $path = '/move/' . $from . '/' . $to;
  675. if ($force) {
  676. $path .= '/force/true';
  677. }
  678. return $this->rsPost($from_bucket, $path);
  679. }
  680. /**
  681. * 主动修改指定资源的文件元信息
  682. *
  683. * @param string $bucket 待操作资源所在空间
  684. * @param string $key 待操作资源文件名
  685. * @param string $mime 待操作文件目标mimeType
  686. *
  687. * @return array
  688. * @link https://developer.qiniu.com/kodo/api/1252/chgm
  689. */
  690. public function changeMime($bucket, $key, $mime)
  691. {
  692. $resource = \Qiniu\entry($bucket, $key);
  693. $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
  694. $path = '/chgm/' . $resource . '/mime/' . $encode_mime;
  695. return $this->rsPost($bucket, $path);
  696. }
  697. /**
  698. * 修改指定资源的存储类型
  699. *
  700. * @param string $bucket 待操作资源所在空间
  701. * @param string $key 待操作资源文件名
  702. * @param int $fileType 对象存储类型
  703. * 0 表示标准存储;
  704. * 1 表示低频存储;
  705. * 2 表示归档存储;
  706. * 3 表示深度归档存储;
  707. * 4 表示归档直读存储;
  708. *
  709. * @return array
  710. * @link https://developer.qiniu.com/kodo/api/3710/chtype
  711. */
  712. public function changeType($bucket, $key, $fileType)
  713. {
  714. $resource = \Qiniu\entry($bucket, $key);
  715. $path = '/chtype/' . $resource . '/type/' . $fileType;
  716. return $this->rsPost($bucket, $path);
  717. }
  718. /**
  719. * 解冻指定资源的存储类型
  720. *
  721. * @param string $bucket 待操作资源所在空间
  722. * @param string $key 待操作资源文件名
  723. * @param int $freezeAfterDays 解冻有效时长,取值范围 1~7
  724. *
  725. * @return array
  726. * @link https://developer.qiniu.com/kodo/api/6380/restore-archive
  727. */
  728. public function restoreAr($bucket, $key, $freezeAfterDays)
  729. {
  730. $resource = \Qiniu\entry($bucket, $key);
  731. $path = '/restoreAr/' . $resource . '/freezeAfterDays/' . $freezeAfterDays;
  732. return $this->rsPost($bucket, $path);
  733. }
  734. /**
  735. * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换
  736. *
  737. * @param string $bucket 待操作资源所在空间
  738. * @param string $key 待操作资源文件名
  739. * @param int $status 0表示启用;1表示禁用
  740. *
  741. * @return array
  742. * @link https://developer.qiniu.com/kodo/api/4173/modify-the-file-status
  743. */
  744. public function changeStatus($bucket, $key, $status)
  745. {
  746. $resource = \Qiniu\entry($bucket, $key);
  747. $path = '/chstatus/' . $resource . '/status/' . $status;
  748. return $this->rsPost($bucket, $path);
  749. }
  750. /**
  751. * 从指定URL抓取资源,并将该资源存储到指定空间中
  752. *
  753. * @param string $url 指定的URL
  754. * @param string $bucket 目标资源空间
  755. * @param string $key 目标资源文件名
  756. *
  757. * @return array
  758. * @link https://developer.qiniu.com/kodo/api/1263/fetch
  759. */
  760. public function fetch($url, $bucket, $key = null)
  761. {
  762. $resource = \Qiniu\base64_urlSafeEncode($url);
  763. $to = \Qiniu\entry($bucket, $key);
  764. $path = '/fetch/' . $resource . '/to/' . $to;
  765. $ak = $this->auth->getAccessKey();
  766. list($ioHost, $err) = $this->config->getIovipHostV2($ak, $bucket, $this->proxy->makeReqOpt());
  767. if ($err != null) {
  768. return array(null, $err);
  769. }
  770. $url = $ioHost . $path;
  771. return $this->postV2($url, null);
  772. }
  773. /**
  774. * 从指定URL异步抓取资源,并将该资源存储到指定空间中
  775. *
  776. * @param string $url 需要抓取的url
  777. * @param string $bucket 所在区域的bucket
  778. * @param string $host 从指定url下载数据时使用的Host
  779. * @param string $key 文件存储的key
  780. * @param string $md5 文件md5
  781. * @param string $etag 文件etag
  782. * @param string $callbackurl 回调URL
  783. * @param string $callbackbody 回调Body
  784. * @param string $callbackbodytype 回调Body内容类型,默认为"application/x-www-form-urlencoded"
  785. * @param string $callbackhost 回调时使用的Host
  786. * @param int $file_type 存储文件类型
  787. * 0:标准存储(默认)
  788. * 1:低频存储
  789. * 2:归档存储
  790. * 3:深度归档存储
  791. * 4:归档直读存储
  792. * @param bool $ignore_same_key 如果空间中已经存在同名文件则放弃本次抓取
  793. * @return array
  794. * @link https://developer.qiniu.com/kodo/api/4097/asynch-fetch
  795. */
  796. public function asynchFetch(
  797. $url,
  798. $bucket,
  799. $host = null,
  800. $key = null,
  801. $md5 = null,
  802. $etag = null,
  803. $callbackurl = null,
  804. $callbackbody = null,
  805. $callbackbodytype = 'application/x-www-form-urlencoded',
  806. $callbackhost = null,
  807. $file_type = 0,
  808. $ignore_same_key = false
  809. ) {
  810. $path = '/sisyphus/fetch';
  811. $params = array('url' => $url, 'bucket' => $bucket);
  812. \Qiniu\setWithoutEmpty($params, 'host', $host);
  813. \Qiniu\setWithoutEmpty($params, 'key', $key);
  814. \Qiniu\setWithoutEmpty($params, 'md5', $md5);
  815. \Qiniu\setWithoutEmpty($params, 'etag', $etag);
  816. \Qiniu\setWithoutEmpty($params, 'callbackurl', $callbackurl);
  817. \Qiniu\setWithoutEmpty($params, 'callbackbody', $callbackbody);
  818. \Qiniu\setWithoutEmpty($params, 'callbackbodytype', $callbackbodytype);
  819. \Qiniu\setWithoutEmpty($params, 'callbackhost', $callbackhost);
  820. \Qiniu\setWithoutEmpty($params, 'file_type', $file_type);
  821. \Qiniu\setWithoutEmpty($params, 'ignore_same_key', $ignore_same_key);
  822. $data = json_encode($params);
  823. return $this->apiPost($bucket, $path, $data);
  824. }
  825. /**
  826. * 查询异步第三方资源抓取任务状态
  827. *
  828. * @param string $zone
  829. * @param string $id
  830. * @return array
  831. * @link https://developer.qiniu.com/kodo/api/4097/asynch-fetch
  832. */
  833. public function asynchFetchStatus($zone, $id)
  834. {
  835. $scheme = "http://";
  836. if ($this->config->useHTTPS === true) {
  837. $scheme = "https://";
  838. }
  839. $url = $scheme . "api-" . $zone . ".qiniuapi.com/sisyphus/fetch?id=" . $id;
  840. list($ret, $err) = $this->getV2($url);
  841. if ($err != null) {
  842. return array(null, $err);
  843. }
  844. return array($ret, null);
  845. }
  846. /**
  847. * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
  848. *
  849. * @param string $bucket 待获取资源所在的空间
  850. * @param string $key 代获取资源文件名
  851. *
  852. * @return array
  853. * @link https://developer.qiniu.com/kodo/api/1293/prefetch
  854. */
  855. public function prefetch($bucket, $key)
  856. {
  857. $resource = \Qiniu\entry($bucket, $key);
  858. $path = '/prefetch/' . $resource;
  859. $ak = $this->auth->getAccessKey();
  860. list($ioHost, $err) = $this->config->getIovipHostV2($ak, $bucket, $this->proxy->makeReqOpt());
  861. if ($err != null) {
  862. return array(null, $err);
  863. }
  864. $url = $ioHost . $path;
  865. return $this->postV2($url, null);
  866. }
  867. /**
  868. * 在单次请求中进行多个资源管理操作
  869. *
  870. * @param array $operations 资源管理操作数组
  871. *
  872. * @return array 每个资源的处理情况,结果类似:
  873. * [
  874. * { "code" => <HttpCode int>, "data" => <Data> },
  875. * { "code" => <HttpCode int> },
  876. * { "code" => <HttpCode int> },
  877. * { "code" => <HttpCode int> },
  878. * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
  879. * ...
  880. * ]
  881. * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
  882. */
  883. public function batch($operations)
  884. {
  885. $scheme = "http://";
  886. if ($this->config->useHTTPS === true) {
  887. $scheme = "https://";
  888. }
  889. $params = 'op=' . implode('&op=', $operations);
  890. $errResp = new Response(0, 0);
  891. if (count($operations) <= 0) {
  892. $errResp->error = 'empty operations';
  893. return array(null, new Error($scheme . '/batch', $errResp));
  894. }
  895. $bucket = '';
  896. foreach ($operations as $op) {
  897. $segments = explode('/', $op);
  898. if (count($segments) < 3) {
  899. continue;
  900. }
  901. list($bucket,) = \Qiniu\decodeEntry($segments[2]);
  902. }
  903. return $this->rsPost($bucket, '/batch', $params);
  904. }
  905. /**
  906. * 设置文件的生命周期
  907. *
  908. * @param string $bucket 设置文件生命周期文件所在的空间
  909. * @param string $key 设置文件生命周期文件的文件名
  910. * @param int $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期
  911. *
  912. * @return array
  913. * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle
  914. */
  915. public function deleteAfterDays($bucket, $key, $days)
  916. {
  917. $entry = \Qiniu\entry($bucket, $key);
  918. $path = "/deleteAfterDays/$entry/$days";
  919. return $this->rsPost($bucket, $path);
  920. }
  921. /**
  922. * 更新 object 生命周期
  923. *
  924. * @param string $bucket 空间名
  925. * @param string $key 目标资源
  926. * @param int $to_line_after_days 多少天后将文件转为低频存储。
  927. * -1 表示取消已设置的转低频存储的生命周期规则;
  928. * 0 表示不修改转低频生命周期规则。
  929. * @param int $to_archive_ir_after_days 多少天后转为归档直读存储。
  930. * -1 表示取消已设置的转归档直读存储的生命周期规则;
  931. * 0 表示不修改转归档直读生命周期规则。
  932. * @param int $to_archive_after_days 多少天后将文件转为归档存储。
  933. * -1 表示取消已设置的转归档存储的生命周期规则;
  934. * 0 表示不修改转归档生命周期规则。
  935. * @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
  936. * -1 表示取消已设置的转深度归档存储的生命周期规则;
  937. * 0 表示不修改转深度归档生命周期规则。
  938. * @param int $delete_after_days 多少天后将文件删除。
  939. * -1 表示取消已设置的删除存储的生命周期规则;
  940. * 0 表示不修改删除存储的生命周期规则。
  941. * @return array
  942. */
  943. public function setObjectLifecycle(
  944. $bucket,
  945. $key,
  946. $to_line_after_days = 0,
  947. $to_archive_after_days = 0,
  948. $to_deep_archive_after_days = 0,
  949. $delete_after_days = 0,
  950. $to_archive_ir_after_days = 0
  951. ) {
  952. return $this->setObjectLifecycleWithCond(
  953. $bucket,
  954. $key,
  955. null,
  956. $to_line_after_days,
  957. $to_archive_after_days,
  958. $to_deep_archive_after_days,
  959. $delete_after_days,
  960. $to_archive_ir_after_days
  961. );
  962. }
  963. /**
  964. * 更新 object 生命周期
  965. *
  966. * @param string $bucket 空间名
  967. * @param string $key 目标资源
  968. * @param int $to_line_after_days 多少天后将文件转为低频存储。
  969. * 设置为 -1 表示取消已设置的转低频存储的生命周期规则;
  970. * 0 表示不修改转低频生命周期规则。
  971. * @param int $to_archive_ir_after_days 多少天后将文件转为归档直读存储。
  972. * 设置为 -1 表示取消已设置的转归档直读存储的生命周期规则;
  973. * 0 表示不修改转归档直读生命周期规则。
  974. * @param int $to_archive_after_days 多少天后将文件转为归档存储。
  975. * -1 表示取消已设置的转归档存储的生命周期规则;
  976. * 0 表示不修改转归档生命周期规则。
  977. * @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
  978. * -1 表示取消已设置的转深度归档存储的生命周期规则;
  979. * 0 表示不修改转深度归档生命周期规则。
  980. * @param int $delete_after_days 多少天后将文件删除。
  981. * -1 表示取消已设置的删除存储的生命周期规则;
  982. * 0 表示不修改删除存储的生命周期规则。
  983. * @param array<string, mixed> $cond 匹配条件,只有条件匹配才会设置成功。
  984. * 目前支持:hash、mime、fsize、putTime
  985. * @return array
  986. */
  987. public function setObjectLifecycleWithCond(
  988. $bucket,
  989. $key,
  990. $cond = null,
  991. $to_line_after_days = 0,
  992. $to_archive_after_days = 0,
  993. $to_deep_archive_after_days = 0,
  994. $delete_after_days = 0,
  995. $to_archive_ir_after_days = 0
  996. ) {
  997. $encodedEntry = \Qiniu\entry($bucket, $key);
  998. $path = '/lifecycle/' . $encodedEntry .
  999. '/toIAAfterDays/' . $to_line_after_days .
  1000. '/toArchiveIRAfterDays/' . $to_archive_ir_after_days .
  1001. '/toArchiveAfterDays/' . $to_archive_after_days .
  1002. '/toDeepArchiveAfterDays/' . $to_deep_archive_after_days .
  1003. '/deleteAfterDays/' . $delete_after_days;
  1004. if ($cond != null) {
  1005. $condStrArr = array();
  1006. foreach ($cond as $key => $value) {
  1007. array_push($condStrArr, $key . '=' . $value);
  1008. }
  1009. $condStr = implode('&', $condStrArr);
  1010. $path .= '/cond' . \Qiniu\base64_urlSafeEncode($condStr);
  1011. }
  1012. return $this->rsPost($bucket, $path);
  1013. }
  1014. private function rsfGet($bucket, $path)
  1015. {
  1016. list($host, $err) = $this->config->getRsfHostV2(
  1017. $this->auth->getAccessKey(),
  1018. $bucket,
  1019. $this->proxy->makeReqOpt()
  1020. );
  1021. if ($err != null) {
  1022. return array(null, $err);
  1023. }
  1024. return $this->getV2($host . $path);
  1025. }
  1026. private function rsGet($bucket, $path)
  1027. {
  1028. list($host, $err) = $this->config->getRsHostV2(
  1029. $this->auth->getAccessKey(),
  1030. $bucket,
  1031. $this->proxy->makeReqOpt()
  1032. );
  1033. if ($err != null) {
  1034. return array(null, $err);
  1035. }
  1036. return $this->getV2($host . $path);
  1037. }
  1038. private function rsPost($bucket, $path, $body = null)
  1039. {
  1040. list($host, $err) = $this->config->getRsHostV2(
  1041. $this->auth->getAccessKey(),
  1042. $bucket,
  1043. $this->proxy->makeReqOpt()
  1044. );
  1045. if ($err != null) {
  1046. return array(null, $err);
  1047. }
  1048. return $this->postV2($host . $path, $body);
  1049. }
  1050. private function apiGet($bucket, $path)
  1051. {
  1052. list($host, $err) = $this->config->getApiHostV2(
  1053. $this->auth->getAccessKey(),
  1054. $bucket,
  1055. $this->proxy->makeReqOpt()
  1056. );
  1057. if ($err != null) {
  1058. return array(null, $err);
  1059. }
  1060. return $this->getV2($host . $path);
  1061. }
  1062. private function apiPost($bucket, $path, $body = null)
  1063. {
  1064. list($host, $err) = $this->config->getApiHostV2(
  1065. $this->auth->getAccessKey(),
  1066. $bucket,
  1067. $this->proxy->makeReqOpt()
  1068. );
  1069. if ($err != null) {
  1070. return array(null, $err);
  1071. }
  1072. return $this->postV2($host . $path, $body);
  1073. }
  1074. private function ucGet($path)
  1075. {
  1076. $url = $this->config->getUcHost() . $path;
  1077. return $this->getV2($url);
  1078. }
  1079. private function ucPost($path, $body = null)
  1080. {
  1081. $url = $this->config->getUcHost() . $path;
  1082. return $this->postV2($url, $body);
  1083. }
  1084. private function getV2($url)
  1085. {
  1086. $headers = $this->auth->authorizationV2($url, 'GET', null, 'application/x-www-form-urlencoded');
  1087. $ret = Client::get($url, $headers, $this->proxy->makeReqOpt());
  1088. if (!$ret->ok()) {
  1089. return array(null, new Error($url, $ret));
  1090. }
  1091. return array($ret->json(), null);
  1092. }
  1093. private function postV2($url, $body)
  1094. {
  1095. $headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/x-www-form-urlencoded');
  1096. $ret = Client::post($url, $body, $headers, $this->proxy->makeReqOpt());
  1097. if (!$ret->ok()) {
  1098. return array(null, new Error($url, $ret));
  1099. }
  1100. $r = ($ret->body === null) ? array() : $ret->json();
  1101. return array($r, null);
  1102. }
  1103. public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
  1104. {
  1105. return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
  1106. }
  1107. public static function buildBatchRename($bucket, $key_pairs, $force)
  1108. {
  1109. return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
  1110. }
  1111. public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
  1112. {
  1113. return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
  1114. }
  1115. public static function buildBatchDelete($bucket, $keys)
  1116. {
  1117. return self::oneKeyBatch('/delete', $bucket, $keys);
  1118. }
  1119. public static function buildBatchStat($bucket, $keys)
  1120. {
  1121. return self::oneKeyBatch('/stat', $bucket, $keys);
  1122. }
  1123. public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
  1124. {
  1125. $data = array();
  1126. foreach ($key_day_pairs as $key => $day) {
  1127. array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
  1128. }
  1129. return $data;
  1130. }
  1131. /**
  1132. * @param string $bucket 空间名
  1133. * @param array<string> $keys 目标资源
  1134. * @param int $to_line_after_days 多少天后将文件转为低频存储。
  1135. * -1 表示取消已设置的转低频存储的生命周期规则;
  1136. * 0 表示不修改转低频生命周期规则。
  1137. * @param int $to_archive_ir_after_days 多少天后将文件转为归档直读。
  1138. * -1 表示取消已设置的转归档只读的生命周期规则;
  1139. * 0 表示不修改转归档只读周期规则。
  1140. * @param int $to_archive_after_days 多少天后将文件转为归档存储。
  1141. * -1 表示取消已设置的转归档存储的生命周期规则;
  1142. * 0 表示不修改转归档生命周期规则。
  1143. * @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
  1144. * -1 表示取消已设置的转深度归档存储的生命周期规则;
  1145. * 0 表示不修改转深度归档生命周期规则。
  1146. * @param int $delete_after_days 多少天后将文件删除。
  1147. * -1 表示取消已设置的删除存储的生命周期规则;
  1148. * 0 表示不修改删除存储的生命周期规则。
  1149. *
  1150. * @retrun array<string>
  1151. */
  1152. public static function buildBatchSetObjectLifecycle(
  1153. $bucket,
  1154. $keys,
  1155. $to_line_after_days,
  1156. $to_archive_after_days,
  1157. $to_deep_archive_after_days,
  1158. $delete_after_days,
  1159. $to_archive_ir_after_days = 0
  1160. ) {
  1161. $result = array();
  1162. foreach ($keys as $key) {
  1163. $encodedEntry = \Qiniu\entry($bucket, $key);
  1164. $op = '/lifecycle/' . $encodedEntry .
  1165. '/toIAAfterDays/' . $to_line_after_days .
  1166. '/toArchiveIRAfterDays/' . $to_archive_ir_after_days .
  1167. '/toArchiveAfterDays/' . $to_archive_after_days .
  1168. '/toDeepArchiveAfterDays/' . $to_deep_archive_after_days .
  1169. '/deleteAfterDays/' . $delete_after_days;
  1170. array_push($result, $op);
  1171. }
  1172. return $result;
  1173. }
  1174. public static function buildBatchChangeMime($bucket, $key_mime_pairs)
  1175. {
  1176. $data = array();
  1177. foreach ($key_mime_pairs as $key => $mime) {
  1178. array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
  1179. }
  1180. return $data;
  1181. }
  1182. public static function buildBatchChangeType($bucket, $key_type_pairs)
  1183. {
  1184. $data = array();
  1185. foreach ($key_type_pairs as $key => $type) {
  1186. array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
  1187. }
  1188. return $data;
  1189. }
  1190. public static function buildBatchRestoreAr($bucket, $key_restore_days_pairs)
  1191. {
  1192. $data = array();
  1193. foreach ($key_restore_days_pairs as $key => $restore_days) {
  1194. array_push($data, '/restoreAr/' . \Qiniu\entry($bucket, $key) . '/freezeAfterDays/' . $restore_days);
  1195. }
  1196. return $data;
  1197. }
  1198. private static function oneKeyBatch($operation, $bucket, $keys)
  1199. {
  1200. $data = array();
  1201. foreach ($keys as $key) {
  1202. array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
  1203. }
  1204. return $data;
  1205. }
  1206. private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
  1207. {
  1208. if ($target_bucket === null) {
  1209. $target_bucket = $source_bucket;
  1210. }
  1211. $data = array();
  1212. $forceOp = "false";
  1213. if ($force) {
  1214. $forceOp = "true";
  1215. }
  1216. foreach ($key_pairs as $from_key => $to_key) {
  1217. $from = \Qiniu\entry($source_bucket, $from_key);
  1218. $to = \Qiniu\entry($target_bucket, $to_key);
  1219. array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
  1220. }
  1221. return $data;
  1222. }
  1223. }