Completed
Push — master ( ee5344...f211be )
by
unknown
14s queued 12s
created

BucketManager::bucketLifecycleRule()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

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