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