Passed
Push — master ( 0e9296...4dc187 )
by
unknown
20:54 queued 12s
created

BucketManager::putBucketEvent()   C

Complexity

Conditions 10
Paths 256

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 10
eloc 24
nc 256
nop 8
dl 0
loc 43
ccs 0
cts 23
cp 0
crap 110
rs 6.1333
c 2
b 0
f 1

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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->getV2($this->getUcHost(). '/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->postV2($this->getUcHost() . $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->postV2($this->getUcHost() . $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($bucket, '/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
        return $this->rsfGet($bucket, '/list?' . http_build_query($query));
153
    }
154
155
    /**
156
     * 列取空间的文件列表
157 3
     *
158
     * @param string $bucket 空间名
159
     * @param string $prefix 列举前缀
160
     * @param string $marker 列举标识符
161
     * @param int $limit 单次列举个数限制
162
     * @param string $delimiter 指定目录分隔符
163
     * @param bool $skipconfirm 是否跳过已删除条目的确认机制
164 3
     *
165 3
     * @return array
166 3
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
167 3
     */
168 3
    public function listFilesv2(
169 3
        $bucket,
170 3
        $prefix = null,
171
        $marker = null,
172
        $limit = 1000,
173
        $delimiter = null,
174
        $skipconfirm = true
175
    ) {
176
        $query = array('bucket' => $bucket);
177
        \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
178
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
179
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
180
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
181
        \Qiniu\setWithoutEmpty($query, 'skipconfirm', $skipconfirm);
182
        $path = '/v2/list?' . http_build_query($query);
183
184
        list($host, $err) = $this->config->getRsfHostV2($this->auth->getAccessKey(), $bucket);
185
186
        if ($err != null) {
187
            return array(null, $err);
188
        }
189
190
        $url = $host . $path;
191
        $headers = $this->auth->authorizationV2($url, 'POST', null, 'application/x-www-form-urlencoded');
192
        $ret = Client::post($url, null, $headers);
193
        if (!$ret->ok()) {
194
            return array(null, new Error($url, $ret));
195
        }
196
        $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

196
        $r = explode("\n", /** @scrutinizer ignore-type */ $ret->body);
Loading history...
197
        array_pop($r);
198
        return array($r, null);
199
    }
200
201
    /**
202
     * 增加bucket生命规则
203
     *
204
     * @param string $bucket 空间名
205
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为
206
     * 字母、数字、下划线
207
     * @param string $prefix 同一个 bucket 里面前缀不能重复
208
     * @param int $delete_after_days 指定上传文件多少天后删除,指定为0表示不删除,
209
     * 大于0表示多少天后删除,需大于 to_line_after_days
210
     * @param int $to_line_after_days 指定文件上传多少天后转低频存储。指定为0表示
211
     * 不转低频存储,小于0表示上传的文件立即变低频存储
212
     * @param int $to_archive_after_days 指定文件上传多少天后转归档存储。指定为0表示
213
     * 不转归档存储,小于0表示上传的文件立即变归档存储
214
     * @param int $to_deep_archive_after_days 指定文件上传多少天后转深度归档存储。指定为0表示
215
     * 不转深度归档存储,小于0表示上传的文件立即变深度归档存储
216
     * @return array
217
     */
218
    public function bucketLifecycleRule(
219
        $bucket,
220
        $name,
221
        $prefix,
222
        $delete_after_days = null,
223
        $to_line_after_days = null,
224
        $to_archive_after_days = null,
225
        $to_deep_archive_after_days = null
226
    ) {
227
        $path = '/rules/add';
228
        $params = array();
229
        if ($bucket) {
230
            $params['bucket'] = $bucket;
231
        }
232
        if ($name) {
233
            $params['name'] = $name;
234
        }
235
        if ($prefix) {
236
            $params['prefix'] = $prefix;
237
        }
238
        if ($delete_after_days) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $delete_after_days of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
239
            $params['delete_after_days'] = $delete_after_days;
240
        }
241
        if ($to_line_after_days) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $to_line_after_days of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
242
            $params['to_line_after_days'] = $to_line_after_days;
243
        }
244
        if ($to_archive_after_days) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $to_archive_after_days of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
245
            $params['to_archive_after_days'] = $to_archive_after_days;
246
        }
247
        if ($to_deep_archive_after_days) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $to_deep_archive_after_days of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
248
            $params['to_deep_archive_after_days'] = $to_deep_archive_after_days;
249
        }
250
        $data = http_build_query($params);
251
        $info = $this->ucPost($path, $data);
252
        return $info;
253
    }
254
255
    /**
256
     * 更新bucket生命规则
257
     *
258
     * @param string $bucket 空间名
259
     * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、
260
     * 数字、下划线
261
     * @param string $prefix 同一个 bucket 里面前缀不能重复
262
     * @param int $delete_after_days 指定上传文件多少天后删除,指定为0表示不删除,
263
     * 大于0表示多少天后删除,需大于 to_line_after_days
264
     * @param int $to_line_after_days 指定文件上传多少天后转低频存储。指定为0表示不
265
     * 转低频存储,小于0表示上传的文件立即变低频存储
266
     * @param int $to_archive_after_days 指定文件上传多少天后转归档存储。指定为0表示
267
     * 不转归档存储,小于0表示上传的文件立即变归档存储
268
     * @param int $to_deep_archive_after_days 指定文件上传多少天后转深度归档存储。指定为0表示
269
     * 不转深度归档存储,小于0表示上传的文件立即变深度归档存储
270
     * @return array
271
     */
272
    public function updateBucketLifecycleRule(
273
        $bucket,
274
        $name,
275
        $prefix,
276
        $delete_after_days = null,
277
        $to_line_after_days = null,
278
        $to_archive_after_days = null,
279
        $to_deep_archive_after_days = null
280
    ) {
281
        $path = '/rules/update';
282
        $params = array();
283
        if ($bucket) {
284
            $params['bucket'] = $bucket;
285
        }
286
        if ($name) {
287
            $params['name'] = $name;
288
        }
289
        if ($prefix) {
290
            $params['prefix'] = $prefix;
291
        }
292
        if ($delete_after_days) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $delete_after_days of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
293
            $params['delete_after_days'] = $delete_after_days;
294
        }
295
        if ($to_line_after_days) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $to_line_after_days of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
296
            $params['to_line_after_days'] = $to_line_after_days;
297
        }
298
        if ($to_archive_after_days) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $to_archive_after_days of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
299
            $params['to_archive_after_days'] = $to_archive_after_days;
300
        }
301
        if ($to_deep_archive_after_days) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $to_deep_archive_after_days of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

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