Completed
Push — master ( 4db7bf...47f9e5 )
by
unknown
16s queued 13s
created

BucketManager::post()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.944

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 9
ccs 2
cts 5
cp 0.4
crap 4.944
rs 10
c 1
b 1
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A BucketManager::buildBatchCopy() 0 3 1
1
<?php
2
3
namespace Qiniu\Storage;
4
5
use Qiniu\Auth;
6
use Qiniu\Config;
7
use Qiniu\Http\Error;
8
use Qiniu\Http\Client;
9
10
/**
11
 * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
12
 *
13
 * @link https://developer.qiniu.com/kodo/api/1274/rs
14
 */
15
final class BucketManager
16
{
17
    private $auth;
18
    private $config;
19
20 42
    public function __construct(Auth $auth, Config $config = null)
21
    {
22 42
        $this->auth = $auth;
23 42
        if ($config == null) {
24 42
            $this->config = new Config();
25 42
        } else {
26
            $this->config = $config;
27
        }
28 42
    }
29
30
    /**
31
     * 获取指定账号下所有的空间名
32
     *
33
     * @param bool $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
34
     * @return array 包含所有空间名
35 3
     */
36
    public function buckets($shared = true)
37 3
    {
38 3
        $includeShared = "false";
39 3
        if ($shared === true) {
40 3
            $includeShared = "true";
41 3
        }
42
        return $this->rsGet('/buckets?shared=' . $includeShared);
43
    }
44
45
    /**
46
     * 列举空间,返回bucket列表
47
     *
48
     * @param string $region 区域
49
     * @param string $line
50
     * @param string $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
51
     * @return array
52
     */
53
    public function listbuckets(
54
        $region = null,
55
        $line = 'false',
56
        $shared = 'false'
57
    ) {
58
        $path = '/v3/buckets?region=' . $region . '&line=' . $line . '&shared=' . $shared;
59
        return $this->ucPost($path);
60
    }
61
62
    /**
63
     * 创建空间
64
     *
65
     * @param string $name 创建的空间名
66
     * @param string $region 创建的区域,默认华东
67
     *
68
     * @return array
69
     * @link https://developer.qiniu.com/kodo/api/1382/mkbucketv3
70
     */
71
    public function createBucket($name, $region = 'z0')
72
    {
73
        $path = '/mkbucketv3/' . $name . '/region/' . $region;
74
        return $this->rsPost($path, null);
75
    }
76
77
    /**
78
     * 删除空间
79
     *
80
     * @param string $name 需要删除的目标空间名
81
     *
82
     * @return array
83
     * @link https://developer.qiniu.com/kodo/api/1601/drop-bucket
84
     */
85
    public function deleteBucket($name)
86
    {
87
        $path = '/drop/' . $name;
88
        return $this->rsPost($path, null);
89
    }
90
91
    /**
92
     * 获取指定空间绑定的所有的域名
93
     *
94
     * @param string $bucket 空间名称
95
     * @return array
96
     */
97
    public function domains($bucket)
98
    {
99
        return $this->apiGet('/v6/domain/list?tbl=' . $bucket);
100
    }
101
102
    /**
103
     * 获取指定空间的相关信息
104
     *
105
     * @param string $bucket 空间名称
106
     * @return array
107
     */
108
    public function bucketInfo($bucket)
109
    {
110
        $path = '/v2/bucketInfo?bucket=' . $bucket;
111
        return $this->ucPost($path);
112
    }
113
114
    /**
115
     * 获取指定zone的空间信息列表
116
     *
117
     * @param string $region 区域
118
     * @param string $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
119
     * @param string $fs 如果为 true,会返回每个空间当前的文件数和存储量(实时数据)
120
     * @return array
121
     */
122
    public function bucketInfos($region = null, $shared = 'false', $fs = 'false')
123
    {
124
        $path = '/v2/bucketInfos?region=' . $region . '&shared=' . $shared . '&fs=' . $fs;
125
        return $this->ucPost($path);
126
    }
127
128
    /**
129
     * 列取空间的文件列表
130
     *
131
     * @param string $bucket 空间名
132
     * @param string $prefix 列举前缀
133
     * @param string $marker 列举标识符
134
     * @param int $limit 单次列举个数限制
135
     * @param string $delimiter 指定目录分隔符
136
     *
137
     * @return array
138
     * @link  https://developer.qiniu.com/kodo/api/1284/list
139
     */
140
    public function listFiles(
141
        $bucket,
142
        $prefix = null,
143
        $marker = null,
144
        $limit = 1000,
145
        $delimiter = null
146
    ) {
147
        $query = array('bucket' => $bucket);
148
        \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
149
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
150
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
151
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
152
        $url = $this->getRsfHost() . '/list?' . http_build_query($query);
153
        return $this->getV2($url);
154
    }
155
156
    /**
157 3
     * 列取空间的文件列表
158
     *
159
     * @param string $bucket 空间名
160
     * @param string $prefix 列举前缀
161
     * @param string $marker 列举标识符
162
     * @param int $limit 单次列举个数限制
163
     * @param string $delimiter 指定目录分隔符
164 3
     * @param bool $skipconfirm 是否跳过已删除条目的确认机制
165 3
     *
166 3
     * @return array
167 3
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
168 3
     */
169 3
    public function listFilesv2(
170 3
        $bucket,
171
        $prefix = null,
172
        $marker = null,
173
        $limit = 1000,
174
        $delimiter = null,
175
        $skipconfirm = true
176
    ) {
177
        $query = array('bucket' => $bucket);
178
        \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
179
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
180
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
181
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
182
        \Qiniu\setWithoutEmpty($query, 'skipconfirm', $skipconfirm);
183
        $path = '/v2/list?' . http_build_query($query);
184
        $url = $this->getRsfHost() . $path;
185
        $headers = $this->auth->authorizationV2($url, 'POST', null, 'application/x-www-form-urlencoded');
186
        $ret = Client::post($url, null, $headers);
187
        if (!$ret->ok()) {
188
            return array(null, new Error($url, $ret));
189
        }
190
        $r = explode("\n", $ret->body);
0 ignored issues
show
Bug introduced by
It seems like $ret->body can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

190
        $r = explode("\n", /** @scrutinizer ignore-type */ $ret->body);
Loading history...
191
        array_pop($r);
192
        return array($r, null);
193
    }
194
195
    /**
196
     * 增加bucket生命规则
197
     *
198
     * @param string $bucket 空间名
199
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为
200
     * 字母、数字、下划线
201
     * @param string $prefix 同一个 bucket 里面前缀不能重复
202
     * @param int $delete_after_days 指定上传文件多少天后删除,指定为0表示不删除,
203
     * 大于0表示多少天后删除,需大于 to_line_after_days
204
     * @param int $to_line_after_days 指定文件上传多少天后转低频存储。指定为0表示
205
     * 不转低频存储,小于0表示上传的文件立即变低频存储
206
     * @return array
207
     */
208
    public function bucketLifecycleRule(
209
        $bucket,
210
        $name,
211
        $prefix,
212
        $delete_after_days,
213
        $to_line_after_days
214
    ) {
215
        $path = '/rules/add';
216
        $params = array();
217
        if ($bucket) {
218
            $params['bucket'] = $bucket;
219
        }
220
        if ($name) {
221
            $params['name'] = $name;
222
        }
223
        if ($prefix) {
224
            $params['prefix'] = $prefix;
225
        }
226
        if ($delete_after_days) {
227
            $params['delete_after_days'] = $delete_after_days;
228
        }
229
        if ($to_line_after_days) {
230
            $params['to_line_after_days'] = $to_line_after_days;
231
        }
232
        $data = http_build_query($params);
233
        $info = $this->ucPost($path, $data);
234
        return $info;
235
    }
236
237
    /**
238
     * 更新bucket生命规则
239
     *
240
     * @param string $bucket 空间名
241
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、
242
     * 数字、下划线
243
     * @param string $prefix 同一个 bucket 里面前缀不能重复
244
     * @param int $delete_after_days 指定上传文件多少天后删除,指定为0表示不删除,
245
     * 大于0表示多少天后删除,需大于 to_line_after_days
246
     * @param int $to_line_after_days 指定文件上传多少天后转低频存储。指定为0表示不
247
     * 转低频存储,小于0表示上传的文件立即变低频存储
248
     * @return array
249
     */
250
    public function updateBucketLifecycleRule(
251
        $bucket,
252
        $name,
253
        $prefix,
254
        $delete_after_days,
255
        $to_line_after_days
256
    ) {
257
        $path = '/rules/update';
258
        $params = array();
259
        if ($bucket) {
260
            $params['bucket'] = $bucket;
261
        }
262
        if ($name) {
263
            $params['name'] = $name;
264
        }
265
        if ($prefix) {
266
            $params['prefix'] = $prefix;
267
        }
268
        if ($delete_after_days) {
269
            $params['delete_after_days'] = $delete_after_days;
270
        }
271
        if ($to_line_after_days) {
272
            $params['to_line_after_days'] = $to_line_after_days;
273
        }
274
        $data = http_build_query($params);
275
        return $this->ucPost($path, $data);
276
    }
277
278
    /**
279
     * 获取bucket生命规则
280
     *
281
     * @param string $bucket 空间名
282
     * @return array
283
     */
284
    public function getBucketLifecycleRules($bucket)
285
    {
286
        $path = '/rules/get?bucket=' . $bucket;
287
        $info = $this->ucGet($path);
288
        return $info;
289
    }
290
291
    /**
292
     * 删除bucket生命规则
293
     *
294
     * @param string $bucket 空间名
295
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
296
     * 只能为字母、数字、下划线()
297
     * @return array
298
     */
299
    public function deleteBucketLifecycleRule($bucket, $name)
300
    {
301
        $path = '/rules/delete';
302
        $params = array();
303
        if ($bucket) {
304
            $params['bucket'] = $bucket;
305
        }
306
        if ($name) {
307
            $params['name'] = $name;
308
        }
309
        $data = http_build_query($params);
310
        $info = $this->ucPost($path, $data);
311
        return $info;
312
    }
313
314
    /**
315
     * 增加bucket事件通知规则
316
     *
317
     * @param string $bucket 空间名
318
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
319
     * 只能为字母、数字、下划线()
320
     * @param string $prefix 同一个 bucket 里面前缀不能重复
321
     * @param string $suffix 可选,文件配置的后缀
322
     * @param array $event 事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,
323
     * disable,enable,deleteMarkerCreate
324
     * @param string $callbackURL 通知URL,可以指定多个,失败依次重试
325
     * @param string $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
326
     * @param string $host 可选,通知请求的host
327
     *
328
     * @return array
329
     */
330
    public function putBucketEvent(
331
        $bucket,
332
        $name,
333
        $prefix,
334
        $suffix,
335
        $event,
336
        $callbackURL,
337
        $access_key = null,
338
        $host = null
339
    ) {
340
        $path = '/events/add';
341
        $params = array();
342
        if (!empty($bucket)) {
343
            $params['bucket'] = $bucket;
344
        }
345
        if (!empty($name)) {
346
            $params['name'] = $name;
347
        }
348
        if (!empty($prefix)) {
349
            $params['prefix'] = $prefix;
350
        }
351
        if (!empty($suffix)) {
352
            $params['suffix'] = $suffix;
353
        }
354
        if (!empty($callbackURL)) {
355
            $params['callbackURL'] = $callbackURL;
356
        }
357
        if (!empty($access_key)) {
358
            $params['access_key'] = $access_key;
359
        }
360
        if (!empty($host)) {
361
            $params['host'] = $host;
362
        }
363
        $data = http_build_query($params);
364
        if (!empty($event)) {
365
            $eventpath = "";
366
            foreach ($event as $key => $value) {
367
                $eventpath .= "&event=$value";
368
            }
369
            $data .= $eventpath;
370
        }
371
        $info = $this->ucPost($path, $data);
372
        return $info;
373
    }
374
375
    /**
376
     * 更新bucket事件通知规则
377
     *
378
     * @param string $bucket 空间名
379
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
380
     * 只能为字母、数字、下划线()
381
     * @param string $prefix 同一个 bucket 里面前缀不能重复
382
     * @param string $suffix 可选,文件配置的后缀
383
     * @param array $event 事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,disable,
384
     * enable,deleteMarkerCreate
385
     * @param string $callbackURL 通知URL,可以指定多个,失败依次重试
386
     * @param string $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
387
     * @param string $host 可选,通知请求的host
388
     *
389
     * @return array
390
     */
391
    public function updateBucketEvent(
392
        $bucket,
393
        $name,
394
        $prefix,
395
        $suffix,
396
        $event,
397
        $callbackURL,
398
        $access_key = null,
399
        $host = null
400
    ) {
401
        $path = '/events/update';
402
        $params = array();
403
        if (!empty($bucket)) {
404
            $params['bucket'] = $bucket;
405
        }
406
        if (!empty($name)) {
407
            $params['name'] = $name;
408
        }
409
        if (!empty($prefix)) {
410
            $params['prefix'] = $prefix;
411
        }
412
        if ($suffix) {
413
            $params['suffix'] = $suffix;
414
        }
415
        if (!empty($event)) {
416
            $params['event'] = $event;
417
        }
418
        if (!empty($callbackURL)) {
419
            $params['callbackURL'] = $callbackURL;
420
        }
421
        if (!empty($access_key)) {
422
            $params['access_key'] = $access_key;
423
        }
424
        if (!empty($host)) {
425
            $params['host'] = $host;
426
        }
427
        $data = http_build_query($params);
428
        if (!empty($event)) {
429
            $eventpath = "";
430
            foreach ($event as $key => $value) {
431
                $eventpath .= "&event=$value";
432
            }
433
            $data .= $eventpath;
434
        }
435
        return $this->ucPost($path, $data);
436
    }
437
438
    /**
439
     * 获取bucket事件通知规则
440
     *
441
     * @param string $bucket 空间名
442
     * @return array
443
     */
444
    public function getBucketEvents($bucket)
445
    {
446
        $path = '/events/get?bucket=' . $bucket;
447
        return $this->ucGet($path);
448
    }
449
450
    /**
451
     * 删除bucket事件通知规则
452
     *
453
     * @param string $bucket 空间名
454
     * @param string $name 规则名称bucket内唯一,长度小于50,不能为空,只能为字母、数字、下划线
455
     * @return array
456
     */
457
    public function deleteBucketEvent($bucket, $name)
458
    {
459
        $path = '/events/delete';
460
        $params = array();
461
        if ($bucket) {
462
            $params['bucket'] = $bucket;
463
        }
464
        if ($name) {
465
            $params['name'] = $name;
466
        }
467
        $data = http_build_query($params);
468
        return $this->ucPost($path, $data);
469
    }
470
471
    /**
472
     * 获取bucket的跨域信息
473
     *
474
     * @param string $bucket 空间名
475
     * @return array
476
     */
477
    public function getCorsRules($bucket)
478
    {
479
        $path = '/corsRules/get/' . $bucket;
480
        return $this->ucGet($path);
481
    }
482
483
    /**
484
     * 开关原图保护
485
     *
486
     * @param string $bucket 空间名称
487
     * @param int $mode mode 为1表示开启原图保护,0表示关闭
488
     * @return array
489
     */
490
    public function putBucketAccessStyleMode($bucket, $mode)
491
    {
492
        $path = '/accessMode/' . $bucket . '/mode/' . $mode;
493
        return $this->ucPost($path, null);
494
    }
495
496
    /**
497
     * 设置私有属性
498
     *
499
     * @param string $bucket 空间名称
500
     * @param int $private private为0表示公开,为1表示私有
501
     * @return array
502
     */
503
    public function putBucketAccessMode($bucket, $private)
504
    {
505
        $path = "/private?bucket=$bucket&private=$private";
506
        return $this->ucPost($path, null);
507
    }
508
509
    /**
510
     * 设置 referer 防盗链
511
     *
512
     * @param string $bucket 空间名称
513
     * @param int $mode 0:关闭Referer(使用此选项将会忽略以下参数并将恢复默认值);
514
     *                  1:设置Referer白名单; 2:设置Referer黑名单
515
     * @param string $norefer 0:不允许空 Refer 访问; 1:表示允许空Refer访问
516
     * @param string $pattern 规则字符串
517
     * @param int $enabled 源站是否支持,默认为0只给CDN配置, 设置为1表示开启源站防盗链
518
     * @return array
519
     * @link https://developer.qiniu.com/kodo/manual/6093/set-the-hotlinking-prevention
520
     */
521
    public function putReferAntiLeech($bucket, $mode, $norefer, $pattern, $enabled = 1)
522
    {
523
        $path = "/referAntiLeech?bucket=$bucket&mode=$mode&norefer=$norefer&pattern=$pattern&source_enabled=$enabled";
524
        return $this->ucPost($path, null);
525
    }
526
527
    /**
528
     * 设置Bucket的maxAge
529
     *
530
     * @param string $bucket 空间名称
531
     * @param int $maxAge maxAge为0或者负数表示为默认值(31536000)
532
     * @return array
533
     */
534
    public function putBucketMaxAge($bucket, $maxAge)
535
    {
536
        $path = '/maxAge?bucket=' . $bucket . '&maxAge=' . $maxAge;
537
        return $this->ucPost($path, null);
538
    }
539
540
    /**
541
     * 设置空间配额
542
     *
543
     * @param string $bucket 空间名称,不支持授权空间
544
     * @param string $size 空间存储量配额,参数传入0或不传表示不更改当前配置,传入-1表示取消限额,新创建的空间默认没有限额
545
     * @param string $count 空间文件数配额,参数含义同<size>
546
     * @return array
547
     */
548
    public function putBucketQuota($bucket, $size, $count)
549
    {
550
        $path = '/setbucketquota/' . $bucket . '/size/' . $size . '/count/' . $count;
551
        return $this->apiPost($path, null);
552
    }
553
554
    /**
555
     * 获取空间配额
556
     *
557
     * @param string $bucket 空间名称
558
     * @return array
559
     */
560
    public function getBucketQuota($bucket)
561
    {
562
        $path = '/getbucketquota/' . $bucket;
563
        return $this->apiPost($path, null);
564
    }
565
566
    /**
567
     * 获取资源的元信息,但不返回文件内容
568
     *
569
     * @param string $bucket 待获取信息资源所在的空间
570
     * @param string $key 待获取资源的文件名
571
     *
572
     * @return array
573
     * @link  https://developer.qiniu.com/kodo/api/1308/stat
574
     */
575
    public function stat($bucket, $key)
576
    {
577
        $path = '/stat/' . \Qiniu\entry($bucket, $key);
578
        return $this->rsGet($path);
579
    }
580
581
    /**
582
     * 删除指定资源
583
     *
584
     * @param string $bucket 待删除资源所在的空间
585
     * @param string $key 待删除资源的文件名
586
     *
587
     * @return array
588
     * @link  https://developer.qiniu.com/kodo/api/1257/delete
589
     */
590
    public function delete($bucket, $key)
591
    {
592
        $path = '/delete/' . \Qiniu\entry($bucket, $key);
593
        return $this->rsPost($path);
594
    }
595
596
    /**
597
     * 给资源进行重命名,本质为move操作。
598
     *
599
     * @param string $bucket 待操作资源所在空间
600
     * @param string $oldname 待操作资源文件名
601
     * @param string $newname 目标资源文件名
602
     *
603
     * @return array
604
     */
605
    public function rename($bucket, $oldname, $newname)
606
    {
607
        return $this->move($bucket, $oldname, $bucket, $newname);
608
    }
609
610
    /**
611
     * 对资源进行复制。
612
     *
613
     * @param string $from_bucket 待操作资源所在空间
614
     * @param string $from_key 待操作资源文件名
615
     * @param string $to_bucket 目标资源空间名
616
     * @param string $to_key 目标资源文件名
617
     *
618
     * @return array
619
     * @link  https://developer.qiniu.com/kodo/api/1254/copy
620
     */
621
    public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
622
    {
623
        $from = \Qiniu\entry($from_bucket, $from_key);
624
        $to = \Qiniu\entry($to_bucket, $to_key);
625
        $path = '/copy/' . $from . '/' . $to;
626
        if ($force === true) {
627
            $path .= '/force/true';
628
        }
629
        return $this->rsPost($path);
630
    }
631
632
    /**
633
     * 将资源从一个空间到另一个空间
634
     *
635
     * @param string $from_bucket 待操作资源所在空间
636
     * @param string $from_key 待操作资源文件名
637
     * @param string $to_bucket 目标资源空间名
638
     * @param string $to_key 目标资源文件名
639
     *
640
     * @return array
641
     * @link  https://developer.qiniu.com/kodo/api/1288/move
642
     */
643
    public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
644
    {
645
        $from = \Qiniu\entry($from_bucket, $from_key);
646
        $to = \Qiniu\entry($to_bucket, $to_key);
647
        $path = '/move/' . $from . '/' . $to;
648
        if ($force) {
649
            $path .= '/force/true';
650
        }
651
        return $this->rsPost($path);
652
    }
653
654
    /**
655
     * 主动修改指定资源的文件元信息
656
     *
657
     * @param string $bucket 待操作资源所在空间
658
     * @param string $key 待操作资源文件名
659
     * @param string $mime 待操作文件目标mimeType
660
     *
661
     * @return array
662
     * @link  https://developer.qiniu.com/kodo/api/1252/chgm
663
     */
664
    public function changeMime($bucket, $key, $mime)
665
    {
666
        $resource = \Qiniu\entry($bucket, $key);
667
        $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
668
        $path = '/chgm/' . $resource . '/mime/' . $encode_mime;
669
        return $this->rsPost($path);
670
    }
671
672
673
    /**
674
     * 修改指定资源的存储类型
675
     *
676
     * @param string $bucket 待操作资源所在空间
677 6
     * @param string $key 待操作资源文件名
678
     * @param int $fileType 0 表示标准存储;1 表示低频存储;2 表示归档存储
679 6
     *
680 6
     * @return array
681
     * @link  https://developer.qiniu.com/kodo/api/3710/chtype
682
     */
683
    public function changeType($bucket, $key, $fileType)
684
    {
685
        $resource = \Qiniu\entry($bucket, $key);
686
        $path = '/chtype/' . $resource . '/type/' . $fileType;
687
        return $this->rsPost($path);
688
    }
689
690
    /**
691
     * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换
692 15
     *
693
     * @param string $bucket 待操作资源所在空间
694 15
     * @param string $key 待操作资源文件名
695 15
     * @param int $status 0表示启用;1表示禁用
696 15
     *
697
     * @return array
698
     * @link  https://developer.qiniu.com/kodo/api/4173/modify-the-file-status
699
     */
700
    public function changeStatus($bucket, $key, $status)
701
    {
702
        $resource = \Qiniu\entry($bucket, $key);
703
        $path = '/chstatus/' . $resource . '/status/' . $status;
704
        return $this->rsPost($path);
705
    }
706
707
    /**
708
     * 从指定URL抓取资源,并将该资源存储到指定空间中
709 3
     *
710
     * @param string $url 指定的URL
711 3
     * @param string $bucket 目标资源空间
712
     * @param string $key 目标资源文件名
713
     *
714
     * @return array
715
     * @link  https://developer.qiniu.com/kodo/api/1263/fetch
716
     */
717
    public function fetch($url, $bucket, $key = null)
718
    {
719
720
        $resource = \Qiniu\base64_urlSafeEncode($url);
721
        $to = \Qiniu\entry($bucket, $key);
722
        $path = '/fetch/' . $resource . '/to/' . $to;
723
724
        $ak = $this->auth->getAccessKey();
725 15
        try {
726
            $ioHost = $this->config->getIovipHost($ak, $bucket);
727 15
        } catch (\Exception $err) {
728 15
            return array(null, $err);
729 15
        }
730 15
731 3
        $url = $ioHost . $path;
732 3
        return $this->postV2($url, null);
733 15
    }
734 15
735
    /**
736
     * 从指定URL异步抓取资源,并将该资源存储到指定空间中
737
     *
738
     * @param string $url 需要抓取的url
739
     * @param string $bucket 所在区域的bucket
740
     * @param string $host 从指定url下载数据时使用的Host
741
     * @param string $key 文件存储的key
742
     * @param string $md5 文件md5
743
     * @param string $etag 文件etag
744
     * @param string $callbackurl 回调URL
745
     * @param string $callbackbody 回调Body
746
     * @param string $callbackbodytype 回调Body内容类型,默认为"application/x-www-form-urlencoded"
747
     * @param string $callbackhost 回调时使用的Host
748 3
     * @param int $file_type 存储文件类型 0:标准存储(默认),1:低频存储,2:归档存储
749
     * @param bool $ignore_same_key 如果空间中已经存在同名文件则放弃本次抓取
750 3
     * @return array
751 3
     * @link  https://developer.qiniu.com/kodo/api/4097/asynch-fetch
752 3
     */
753 3
    public function asynchFetch(
754
        $url,
755
        $bucket,
756 3
        $host = null,
757 3
        $key = null,
758
        $md5 = null,
759
        $etag = null,
760
        $callbackurl = null,
761
        $callbackbody = null,
762
        $callbackbodytype = 'application/x-www-form-urlencoded',
763
        $callbackhost = null,
764
        $file_type = 0,
765
        $ignore_same_key = false
766
    ) {
767
        $path = '/sisyphus/fetch';
768
769
        $params = array('url' => $url, 'bucket' => $bucket);
770 3
        \Qiniu\setWithoutEmpty($params, 'host', $host);
771
        \Qiniu\setWithoutEmpty($params, 'key', $key);
772 3
        \Qiniu\setWithoutEmpty($params, 'md5', $md5);
773 3
        \Qiniu\setWithoutEmpty($params, 'etag', $etag);
774 3
        \Qiniu\setWithoutEmpty($params, 'callbackurl', $callbackurl);
775 3
        \Qiniu\setWithoutEmpty($params, 'callbackbody', $callbackbody);
776 3
        \Qiniu\setWithoutEmpty($params, 'callbackbodytype', $callbackbodytype);
777
        \Qiniu\setWithoutEmpty($params, 'callbackhost', $callbackhost);
778
        \Qiniu\setWithoutEmpty($params, 'file_type', $file_type);
779
        \Qiniu\setWithoutEmpty($params, 'ignore_same_key', $ignore_same_key);
780
        $data = json_encode($params);
781
782
        $ak = $this->auth->getAccessKey();
783
        try {
784
            $apiHost = $this->config->getApiHost($ak, $bucket);
785
        } catch (\Exception $err) {
786
            return array(null, $err);
787
        }
788
        $url = $apiHost . $path;
789
790
        return $this->postV2($url, $data);
791
    }
792
793
794
    /**
795
     * 查询异步第三方资源抓取任务状态
796
     *
797
     * @param string $zone
798
     * @param string $id
799
     * @return array
800
     * @link  https://developer.qiniu.com/kodo/api/4097/asynch-fetch
801
     */
802
    public function asynchFetchStatus($zone, $id)
803
    {
804
        $scheme = "http://";
805
806
        if ($this->config->useHTTPS === true) {
807
            $scheme = "https://";
808
        }
809
810
        $url = $scheme . "api-" . $zone . ".qiniu.com/sisyphus/fetch?id=" . $id;
811
812
        list($ret, $err) = $this->getV2($url);
813
814
        if ($err != null) {
815
            return array(null, $err);
816
        }
817
        return array($ret, null);
818
    }
819
820
821
    /**
822
     * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
823
     *
824
     * @param string $bucket 待获取资源所在的空间
825
     * @param string $key 代获取资源文件名
826
     *
827
     * @return array
828
     * @link  https://developer.qiniu.com/kodo/api/1293/prefetch
829
     */
830
    public function prefetch($bucket, $key)
831
    {
832
        $resource = \Qiniu\entry($bucket, $key);
833
        $path = '/prefetch/' . $resource;
834
835
        $ak = $this->auth->getAccessKey();
836
        try {
837
            $ioHost = $this->config->getIovipHost($ak, $bucket);
838 3
        } catch (\Exception $err) {
839
            return array(null, $err);
840
        }
841 3
842 3
        $url = $ioHost . $path;
843 3
        return $this->postV2($url, null);
844
    }
845 3
846 3
    /**
847
     * 在单次请求中进行多个资源管理操作
848 3
     *
849 3
     * @param array $operations 资源管理操作数组
850
     *
851
     * @return array 每个资源的处理情况,结果类似:
852
     *              [
853
     *                   { "code" => <HttpCode int>, "data" => <Data> },
854
     *                   { "code" => <HttpCode int> },
855
     *                   { "code" => <HttpCode int> },
856
     *                   { "code" => <HttpCode int> },
857
     *                   { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
858
     *                   ...
859
     *               ]
860
     * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
861 3
     */
862
    public function batch($operations)
863 3
    {
864 3
        $params = 'op=' . implode('&op=', $operations);
865
        return $this->rsPost('/batch', $params);
866 3
    }
867 3
868
    /**
869 3
     * 设置文件的生命周期
870 3
     *
871 3
     * @param string $bucket 设置文件生命周期文件所在的空间
872
     * @param string $key 设置文件生命周期文件的文件名
873
     * @param int $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期
874
     *
875
     * @return array
876
     * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle
877
     */
878
    public function deleteAfterDays($bucket, $key, $days)
879
    {
880
        $entry = \Qiniu\entry($bucket, $key);
881
        $path = "/deleteAfterDays/$entry/$days";
882
        return $this->rsPost($path);
883
    }
884
885
    private function getRsfHost()
886
    {
887
        $scheme = "http://";
888
        if ($this->config->useHTTPS === true) {
889
            $scheme = "https://";
890 12
        }
891
        return $scheme . Config::RSF_HOST;
892 12
    }
893 12
894
    private function getRsHost()
895
    {
896
        $scheme = "http://";
897
        if ($this->config->useHTTPS === true) {
898
            $scheme = "https://";
899
        }
900
        return $scheme . Config::RS_HOST;
901
    }
902
903
    private function getApiHost()
904
    {
905
        $scheme = "http://";
906 3
        if ($this->config->useHTTPS === true) {
907
            $scheme = "https://";
908 3
        }
909 3
        return $scheme . Config::API_HOST;
910 3
    }
911 3
912
    private function getUcHost()
913
    {
914 3
        $scheme = "http://";
915
        if ($this->config->useHTTPS === true) {
916 3
            $scheme = "https://";
917 3
        }
918
        return $scheme . Config::UC_HOST;
919
    }
920 3
921
    private function rsPost($path, $body = null)
922
    {
923 33
        $url = $this->getRsHost() . $path;
924
        return $this->postV2($url, $body);
925 33
    }
926 33
927
    private function apiPost($path, $body = null)
928
    {
929 33
        $url = $this->getApiHost() . $path;
930
        return $this->postV2($url, $body);
931
    }
932
933
    private function ucPost($path, $body = null)
934
    {
935
        $url = $this->getUcHost() . $path;
936
        return $this->postV2($url, $body);
937
    }
938
939
    private function ucGet($path)
940
    {
941
        $url = $this->getUcHost() . $path;
942
        return $this->getV2($url);
943
    }
944
945
    private function apiGet($path)
946
    {
947
        $url = $this->getApiHost() . $path;
948
        return $this->getV2($url);
949
    }
950 27
951
    private function rsGet($path)
952 27
    {
953 27
        $url = $this->getRsHost() . $path;
954
        return $this->getV2($url);
955
    }
956
957
    private function getV2($url)
958
    {
959
        $headers = $this->auth->authorizationV2($url, 'GET', null, 'application/x-www-form-urlencoded');
960
        $ret = Client::get($url, $headers);
961
        if (!$ret->ok()) {
962
            return array(null, new Error($url, $ret));
963
        }
964
        return array($ret->json(), null);
965
    }
966
967
    private function postV2($url, $body)
968
    {
969
        $headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/x-www-form-urlencoded');
970
        $ret = Client::post($url, $body, $headers);
971
        if (!$ret->ok()) {
972
            return array(null, new Error($url, $ret));
973
        }
974
        $r = ($ret->body === null) ? array() : $ret->json();
975
        return array($r, null);
976
    }
977
978
    public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
979
    {
980 9
        return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
981
    }
982 9
983 9
984
    public static function buildBatchRename($bucket, $key_pairs, $force)
985
    {
986 12
        return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
987
    }
988 12
989 12
990 12
    public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
991 6
    {
992
        return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
993 12
    }
994
995
996 33
    public static function buildBatchDelete($bucket, $keys)
997
    {
998 33
        return self::oneKeyBatch('/delete', $bucket, $keys);
999 33
    }
1000 33
1001 9
1002
    public static function buildBatchStat($bucket, $keys)
1003 30
    {
1004 30
        return self::oneKeyBatch('/stat', $bucket, $keys);
1005
    }
1006
1007
    public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
1008
    {
1009
        $data = array();
1010
        foreach ($key_day_pairs as $key => $day) {
1011
            array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
1012
        }
1013
        return $data;
1014
    }
1015
1016
    public static function buildBatchChangeMime($bucket, $key_mime_pairs)
1017
    {
1018
        $data = array();
1019
        foreach ($key_mime_pairs as $key => $mime) {
1020
            array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
1021
        }
1022
        return $data;
1023
    }
1024
1025 3
    public static function buildBatchChangeType($bucket, $key_type_pairs)
1026
    {
1027 3
        $data = array();
1028
        foreach ($key_type_pairs as $key => $type) {
1029
            array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
1030
        }
1031 3
        return $data;
1032
    }
1033 3
1034
    private static function oneKeyBatch($operation, $bucket, $keys)
1035
    {
1036
        $data = array();
1037 6
        foreach ($keys as $key) {
1038
            array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
1039 6
        }
1040
        return $data;
1041
    }
1042
1043 3
    private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
1044
    {
1045 3
        if ($target_bucket === null) {
1046
            $target_bucket = $source_bucket;
1047
        }
1048
        $data = array();
1049 3
        $forceOp = "false";
1050
        if ($force) {
1051 3
            $forceOp = "true";
1052
        }
1053
        foreach ($key_pairs as $from_key => $to_key) {
1054
            $from = \Qiniu\entry($source_bucket, $from_key);
1055
            $to = \Qiniu\entry($target_bucket, $to_key);
1056
            array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
1057
        }
1058
        return $data;
1059
    }
1060
}
1061