BucketManager   F
last analyzed

Complexity

Total Complexity 135

Size/Duplication

Total Lines 1306
Duplicated Lines 0 %

Test Coverage

Coverage 32.42%

Importance

Changes 35
Bugs 3 Features 11
Metric Value
eloc 385
c 35
b 3
f 11
dl 0
loc 1306
ccs 131
cts 404
cp 0.3242
rs 2
wmc 135

63 Methods

Rating   Name   Duplication   Size   Complexity  
C updateBucketLifecycleRule() 0 38 9
A listFiles() 0 13 1
A getBucketEvents() 0 4 1
A changeMime() 0 6 1
A buildBatchRestoreAr() 0 7 2
A buckets() 0 7 2
A buildBatchDelete() 0 3 1
A buildBatchSetObjectLifecycle() 0 21 2
A move() 0 9 2
A setObjectLifecycleWithCond() 0 26 3
A stat() 0 4 1
A restoreAr() 0 5 1
C bucketLifecycleRule() 0 39 9
A putBucketAccessMode() 0 4 1
A copy() 0 9 2
A ucGet() 0 4 1
A createBucket() 0 4 1
A buildBatchRename() 0 3 1
A domains() 0 3 1
A buildBatchMove() 0 3 1
A asynchFetchStatus() 0 16 3
A putReferAntiLeech() 0 4 1
A changeType() 0 5 1
A buildBatchDeleteAfterDays() 0 7 2
A deleteBucketEvent() 0 12 3
A buildBatchCopy() 0 3 1
A postV2() 0 9 3
A putBucketAccessStyleMode() 0 4 1
A setObjectLifecycle() 0 18 1
A twoKeyBatch() 0 16 4
A rsGet() 0 13 2
A buildBatchStat() 0 3 1
A listFilesv2() 0 35 3
A bucketInfo() 0 4 1
A getBucketQuota() 0 4 1
A getCorsRules() 0 4 1
A deleteBucket() 0 4 1
A changeStatus() 0 5 1
A rsPost() 0 13 2
A delete() 0 4 1
A batch() 0 21 5
A asynchFetch() 0 30 1
A rsfGet() 0 13 2
A deleteAfterDays() 0 5 1
A bucketInfos() 0 4 1
A apiPost() 0 14 2
A buildBatchChangeType() 0 7 2
A putBucketQuota() 0 4 1
F updateBucketEvent() 0 45 11
A putBucketMaxAge() 0 4 1
A listbuckets() 0 7 1
A fetch() 0 17 2
A getBucketLifecycleRules() 0 5 1
A buildBatchChangeMime() 0 7 2
A rename() 0 3 1
A ucPost() 0 4 1
C putBucketEvent() 0 43 10
A oneKeyBatch() 0 7 2
A apiGet() 0 13 2
A getV2() 0 8 2
A deleteBucketLifecycleRule() 0 13 3
A __construct() 0 14 2
A prefetch() 0 14 2

How to fix   Complexity   

Complex Class

Complex classes like BucketManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BucketManager, and based on these observations, apply Extract Interface, too.

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
use Qiniu\Http\Proxy;
10
use Qiniu\Http\Response;
11
12
/**
13
 * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
14
 *
15
 * @link https://developer.qiniu.com/kodo/api/1274/rs
16
 */
17
final class BucketManager
18
{
19
    private $auth;
20 42
    private $config;
21
    private $proxy;
22 42
23 42
    public function __construct(
24 42
        Auth $auth,
25 42
        Config $config = null,
26
        $proxy = null,
27
        $proxy_auth = null,
28 42
        $proxy_user_password = null
29
    ) {
30
        $this->auth = $auth;
31
        if ($config == null) {
32
            $this->config = new Config();
33
        } else {
34
            $this->config = $config;
35 3
        }
36
        $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
37 3
    }
38 3
39 3
    /**
40 3
     * 获取指定账号下所有的空间名
41 3
     *
42
     * @param bool $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
43
     * @return array 包含所有空间名
44
     */
45
    public function buckets($shared = true)
46
    {
47
        $includeShared = "false";
48
        if ($shared === true) {
49
            $includeShared = "true";
50
        }
51
        return $this->getV2($this->config->getUcHost() . '/buckets?shared=' . $includeShared);
52
    }
53
54
    /**
55
     * 列举空间,返回bucket列表
56
     *
57
     * @param string $region 区域
58
     * @param string $line
59
     * @param string $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
60
     * @return array
61
     */
62
    public function listbuckets(
63
        $region = null,
64
        $line = 'false',
65
        $shared = 'false'
66
    ) {
67
        $path = '/v3/buckets?region=' . $region . '&line=' . $line . '&shared=' . $shared;
68
        return $this->ucPost($path);
69
    }
70
71
    /**
72
     * 创建空间
73
     *
74
     * @param string $name 创建的空间名
75
     * @param string $region 创建的区域,默认华东
76
     *
77
     * @return array
78
     * @link https://developer.qiniu.com/kodo/api/1382/mkbucketv3
79
     */
80
    public function createBucket($name, $region = 'z0')
81
    {
82
        $path = '/mkbucketv3/' . $name . '/region/' . $region;
83
        return $this->postV2($this->config->getUcHost() . $path, null);
84
    }
85
86
    /**
87
     * 删除空间
88
     *
89
     * @param string $name 需要删除的目标空间名
90
     *
91
     * @return array
92
     * @link https://developer.qiniu.com/kodo/api/1601/drop-bucket
93
     */
94
    public function deleteBucket($name)
95
    {
96
        $path = '/drop/' . $name;
97
        return $this->postV2($this->config->getUcHost() . $path, null);
98
    }
99
100
    /**
101
     * 获取指定空间绑定的所有的域名
102
     *
103
     * @param string $bucket 空间名称
104
     * @return array
105
     */
106
    public function domains($bucket)
107
    {
108
        return $this->ucGet('/v2/domains?tbl=' . $bucket);
109
    }
110
111
    /**
112
     * 获取指定空间的相关信息
113
     *
114
     * @param string $bucket 空间名称
115
     * @return array
116
     */
117
    public function bucketInfo($bucket)
118
    {
119
        $path = '/v2/bucketInfo?bucket=' . $bucket;
120
        return $this->ucPost($path);
121
    }
122
123
    /**
124
     * 获取指定zone的空间信息列表
125
     *
126
     * @param string $region 区域
127
     * @param string $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
128
     * @param string $fs 如果为 true,会返回每个空间当前的文件数和存储量(实时数据)
129
     * @return array
130
     */
131
    public function bucketInfos($region = null, $shared = 'false', $fs = 'false')
132
    {
133
        $path = '/v2/bucketInfos?region=' . $region . '&shared=' . $shared . '&fs=' . $fs;
134
        return $this->ucPost($path);
135
    }
136
137
    /**
138
     * 列取空间的文件列表
139
     *
140
     * @param string $bucket 空间名
141
     * @param string $prefix 列举前缀
142
     * @param string $marker 列举标识符
143
     * @param int $limit 单次列举个数限制
144
     * @param string $delimiter 指定目录分隔符
145
     *
146
     * @return array
147
     * @link  https://developer.qiniu.com/kodo/api/1284/list
148
     */
149
    public function listFiles(
150
        $bucket,
151
        $prefix = null,
152
        $marker = null,
153
        $limit = 1000,
154
        $delimiter = null
155
    ) {
156
        $query = array('bucket' => $bucket);
157 3
        \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
158
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
159
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
160
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
161
        return $this->rsfGet($bucket, '/list?' . http_build_query($query));
162
    }
163
164 3
    /**
165 3
     * 列取空间的文件列表
166 3
     *
167 3
     * @deprecated API 可能返回仅包含 marker,不包含 item 或 dir 的项,请使用 {@link listFiles}
168 3
     *
169 3
     * @param string $bucket 空间名
170 3
     * @param string $prefix 列举前缀
171
     * @param string $marker 列举标识符
172
     * @param int $limit 单次列举个数限制
173
     * @param string $delimiter 指定目录分隔符
174
     * @param bool $skipconfirm 是否跳过已删除条目的确认机制
175
     *
176
     * @return array
177
     * @link  http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
178
     */
179
    public function listFilesv2(
180
        $bucket,
181
        $prefix = null,
182
        $marker = null,
183
        $limit = 1000,
184
        $delimiter = null,
185
        $skipconfirm = true
186
    ) {
187
        $query = array('bucket' => $bucket);
188
        \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
189
        \Qiniu\setWithoutEmpty($query, 'marker', $marker);
190
        \Qiniu\setWithoutEmpty($query, 'limit', $limit);
191
        \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
192
        \Qiniu\setWithoutEmpty($query, 'skipconfirm', $skipconfirm);
193
        $path = '/v2/list?' . http_build_query($query);
194
195
        list($host, $err) = $this->config->getRsfHostV2(
196
            $this->auth->getAccessKey(),
197
            $bucket,
198
            $this->proxy->makeReqOpt()
199
        );
200
201
        if ($err != null) {
202
            return array(null, $err);
203
        }
204
205
        $url = $host . $path;
206
        $headers = $this->auth->authorizationV2($url, 'POST', null, 'application/x-www-form-urlencoded');
207
        $ret = Client::post($url, null, $headers, $this->proxy->makeReqOpt());
208
        if (!$ret->ok()) {
209
            return array(null, new Error($url, $ret));
210
        }
211
        $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

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

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
1126
    {
1127
        list($host, $err) = $this->config->getApiHostV2(
1128
            $this->auth->getAccessKey(),
1129
            $bucket,
1130
            $this->proxy->makeReqOpt()
1131
        );
1132
1133
        if ($err != null) {
1134
            return array(null, $err);
1135
        }
1136
1137
        return $this->getV2($host . $path);
1138
    }
1139
1140
    private function apiPost($bucket, $path, $body = null)
1141
    {
1142
1143
        list($host, $err) = $this->config->getApiHostV2(
1144
            $this->auth->getAccessKey(),
1145
            $bucket,
1146
            $this->proxy->makeReqOpt()
1147
        );
1148
1149
        if ($err != null) {
1150
            return array(null, $err);
1151
        }
1152
1153
        return $this->postV2($host . $path, $body);
1154
    }
1155
1156
    private function ucGet($path)
1157
    {
1158
        $url = $this->config->getUcHost() . $path;
1159
        return $this->getV2($url);
1160
    }
1161
1162
    private function ucPost($path, $body = null)
1163
    {
1164
        $url = $this->config->getUcHost() . $path;
1165
        return $this->postV2($url, $body);
1166
    }
1167
1168
    private function getV2($url)
1169
    {
1170
        $headers = $this->auth->authorizationV2($url, 'GET', null, 'application/x-www-form-urlencoded');
1171
        $ret = Client::get($url, $headers, $this->proxy->makeReqOpt());
1172
        if (!$ret->ok()) {
1173
            return array(null, new Error($url, $ret));
1174
        }
1175
        return array($ret->json(), null);
1176
    }
1177
1178
    private function postV2($url, $body)
1179
    {
1180
        $headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/x-www-form-urlencoded');
1181
        $ret = Client::post($url, $body, $headers, $this->proxy->makeReqOpt());
1182
        if (!$ret->ok()) {
1183
            return array(null, new Error($url, $ret));
1184
        }
1185
        $r = ($ret->body === null) ? array() : $ret->json();
1186
        return array($r, null);
1187
    }
1188
1189
    public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
1190
    {
1191
        return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
1192
    }
1193
1194
1195
    public static function buildBatchRename($bucket, $key_pairs, $force)
1196
    {
1197
        return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
1198
    }
1199
1200
1201
    public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
1202
    {
1203
        return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
1204
    }
1205
1206
1207
    public static function buildBatchDelete($bucket, $keys)
1208
    {
1209
        return self::oneKeyBatch('/delete', $bucket, $keys);
1210
    }
1211
1212
1213
    public static function buildBatchStat($bucket, $keys)
1214
    {
1215
        return self::oneKeyBatch('/stat', $bucket, $keys);
1216
    }
1217
1218
    public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
1219
    {
1220
        $data = array();
1221
        foreach ($key_day_pairs as $key => $day) {
1222
            array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
1223
        }
1224
        return $data;
1225
    }
1226
1227
    /**
1228
     * @param string $bucket 空间名
1229
     * @param array<string> $keys 目标资源
1230
     * @param int $to_line_after_days 多少天后将文件转为低频存储。
1231
     *   -1 表示取消已设置的转低频存储的生命周期规则;
1232
     *   0 表示不修改转低频生命周期规则。
1233
     * @param int $to_archive_ir_after_days 多少天后将文件转为归档直读。
1234
     *    -1 表示取消已设置的转归档只读的生命周期规则;
1235
     *    0 表示不修改转归档只读周期规则。
1236
     * @param int $to_archive_after_days 多少天后将文件转为归档存储。
1237
     *   -1 表示取消已设置的转归档存储的生命周期规则;
1238
     *   0 表示不修改转归档生命周期规则。
1239
     * @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
1240
     *   -1 表示取消已设置的转深度归档存储的生命周期规则;
1241
     *   0 表示不修改转深度归档生命周期规则。
1242
     * @param int $delete_after_days 多少天后将文件删除。
1243
     *   -1 表示取消已设置的删除存储的生命周期规则;
1244
     *   0 表示不修改删除存储的生命周期规则。
1245
     *
1246
     * @retrun array<string>
1247
     */
1248
    public static function buildBatchSetObjectLifecycle(
1249
        $bucket,
1250
        $keys,
1251
        $to_line_after_days,
1252
        $to_archive_after_days,
1253
        $to_deep_archive_after_days,
1254
        $delete_after_days,
1255
        $to_archive_ir_after_days = 0
1256
    ) {
1257
        $result = array();
1258
        foreach ($keys as $key) {
1259
            $encodedEntry = \Qiniu\entry($bucket, $key);
1260
            $op = '/lifecycle/' . $encodedEntry .
1261
                '/toIAAfterDays/' . $to_line_after_days .
1262
                '/toArchiveIRAfterDays/' . $to_archive_ir_after_days .
1263
                '/toArchiveAfterDays/' . $to_archive_after_days .
1264
                '/toDeepArchiveAfterDays/' . $to_deep_archive_after_days .
1265
                '/deleteAfterDays/' . $delete_after_days;
1266
            array_push($result, $op);
1267
        }
1268
        return $result;
1269
    }
1270
1271
    public static function buildBatchChangeMime($bucket, $key_mime_pairs)
1272
    {
1273
        $data = array();
1274
        foreach ($key_mime_pairs as $key => $mime) {
1275
            array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
1276
        }
1277
        return $data;
1278
    }
1279
1280
    public static function buildBatchChangeType($bucket, $key_type_pairs)
1281
    {
1282
        $data = array();
1283
        foreach ($key_type_pairs as $key => $type) {
1284
            array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
1285
        }
1286
        return $data;
1287
    }
1288
1289
    public static function buildBatchRestoreAr($bucket, $key_restore_days_pairs)
1290
    {
1291
        $data = array();
1292
        foreach ($key_restore_days_pairs as $key => $restore_days) {
1293
            array_push($data, '/restoreAr/' . \Qiniu\entry($bucket, $key) . '/freezeAfterDays/' . $restore_days);
1294
        }
1295
        return $data;
1296
    }
1297
1298
    private static function oneKeyBatch($operation, $bucket, $keys)
1299
    {
1300
        $data = array();
1301
        foreach ($keys as $key) {
1302
            array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
1303
        }
1304
        return $data;
1305
    }
1306
1307
    private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
1308
    {
1309
        if ($target_bucket === null) {
1310
            $target_bucket = $source_bucket;
1311
        }
1312
        $data = array();
1313
        $forceOp = "false";
1314
        if ($force) {
1315
            $forceOp = "true";
1316
        }
1317
        foreach ($key_pairs as $from_key => $to_key) {
1318
            $from = \Qiniu\entry($source_bucket, $from_key);
1319
            $to = \Qiniu\entry($target_bucket, $to_key);
1320
            array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
1321
        }
1322
        return $data;
1323
    }
1324
}
1325