1
|
|
|
<?php |
2
|
|
|
namespace Qiniu\Storage; |
3
|
|
|
|
4
|
|
|
use Qiniu\Auth; |
5
|
|
|
use Qiniu\Config; |
6
|
|
|
use Qiniu\Zone; |
7
|
|
|
use Qiniu\Http\Client; |
8
|
|
|
use Qiniu\Http\Error; |
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
|
|
|
* @return string[] 包含所有空间名 |
34
|
|
|
*/ |
35
|
3 |
|
public function buckets($shared = true) |
36
|
|
|
{ |
37
|
3 |
|
$includeShared = "false"; |
38
|
3 |
|
if ($shared === true) { |
39
|
3 |
|
$includeShared = "true"; |
40
|
3 |
|
} |
41
|
3 |
|
return $this->rsGet('/buckets?shared=' . $includeShared); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 创建空间 |
46
|
|
|
* |
47
|
|
|
* @param $name 创建的空间名 |
48
|
|
|
* @param $region 创建的区域,默认华东 |
49
|
|
|
* |
50
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
51
|
|
|
*/ |
52
|
|
|
public function creatBucket($name, $region='z0') |
53
|
|
|
{ |
54
|
|
|
$path = '/mkbucketv2/'.$name.'/region/' . $region; |
55
|
|
|
return $this->rsPost($path, null); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* 删除空间 |
60
|
|
|
* |
61
|
|
|
* @param $name 删除的空间名 |
62
|
|
|
* |
63
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
64
|
|
|
*/ |
65
|
|
|
public function deleteBucket($name) |
66
|
|
|
{ |
67
|
|
|
$path = '/drop/'.$name; |
68
|
|
|
return $this->rsPost($path, null); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* 获取指定空间绑定的所有的域名 |
73
|
|
|
* |
74
|
|
|
* @return string[] 包含所有空间域名 |
75
|
|
|
*/ |
76
|
|
|
public function domains($bucket) |
77
|
|
|
{ |
78
|
|
|
return $this->apiGet('/v6/domain/list?tbl=' . $bucket); |
79
|
3 |
|
} |
80
|
|
|
|
81
|
3 |
|
/** |
82
|
3 |
|
* 获取指定空间的相关信息 |
83
|
3 |
|
* |
84
|
3 |
|
* @return string[] 包含空间信息 |
85
|
3 |
|
*/ |
86
|
3 |
|
public function bucketInfo($bucket){ |
87
|
3 |
|
$path = '/v2/bucketInfo?bucket=' . $bucket; |
88
|
|
|
$info = $this->ucPost($path); |
89
|
|
|
return $info; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* 获取指定zone的空间信息列表 |
94
|
|
|
* 在Region 未指定且Global 不为 true 时(包含未指定的情况,下同),返回用户的所有空间。 |
95
|
|
|
* 在指定了 region 参数且 global 不为 true 时,只列举非全局空间。 |
96
|
|
|
* 在指定了global为 true 时,返回所有全局空间,忽略region 参数 |
97
|
|
|
* shared 不指定shared参数或指定shared为rw或false时,返回包含具有读写权限空间,指定shared为rd或true时,返回包含具有读权限空间。 |
98
|
|
|
* fs:如果为 true,会返回每个空间当前的文件数和存储量(实时数据)。 |
99
|
|
|
* |
100
|
|
|
* @return string[] 包含空间信息 |
101
|
|
|
*/ |
102
|
|
|
public function bucketInfos($region=null, $global='false', $shared='false', $fs='false'){ |
103
|
|
|
$path = '/v2/bucketInfos?region=' . $region . '&global=' . $global . '&shared=' . $shared . '&fs=' . $fs; |
104
|
|
|
$info = $this->ucPost($path); |
105
|
|
|
return $info; |
106
|
|
|
} |
107
|
6 |
|
|
108
|
|
|
/** |
109
|
6 |
|
* 获取空间绑定的域名列表 |
110
|
6 |
|
* @return string[] 包含空间绑定的所有域名 |
111
|
|
|
*/ |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* 列取空间的文件列表 |
115
|
|
|
* |
116
|
|
|
* @param $bucket 空间名 |
117
|
|
|
* @param $prefix 列举前缀 |
118
|
|
|
* @param $marker 列举标识符 |
119
|
|
|
* @param $limit 单次列举个数限制 |
120
|
|
|
* @param $delimiter 指定目录分隔符 |
121
|
|
|
* |
122
|
15 |
|
* @return array 包含文件信息的数组,类似:[ |
123
|
|
|
* { |
124
|
15 |
|
* "hash" => "<Hash string>", |
125
|
15 |
|
* "key" => "<Key string>", |
126
|
15 |
|
* "fsize" => "<file size>", |
127
|
|
|
* "putTime" => "<file modify time>" |
128
|
|
|
* }, |
129
|
|
|
* ... |
130
|
|
|
* ] |
131
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html |
132
|
|
|
*/ |
133
|
|
|
public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null) |
134
|
|
|
{ |
135
|
|
|
$query = array('bucket' => $bucket); |
136
|
|
|
\Qiniu\setWithoutEmpty($query, 'prefix', $prefix); |
137
|
|
|
\Qiniu\setWithoutEmpty($query, 'marker', $marker); |
138
|
|
|
\Qiniu\setWithoutEmpty($query, 'limit', $limit); |
139
|
3 |
|
\Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter); |
140
|
|
|
$url = $this->getRsfHost() . '/list?' . http_build_query($query); |
141
|
3 |
|
return $this->get($url); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* 设置Referer防盗链 |
146
|
|
|
* |
147
|
|
|
* @param $bucket 空间名 |
148
|
|
|
* @param $mode 0: 表示关闭Referer(使用此选项将会忽略以下参数并将恢复默认值); 1: 表示设置Referer白名单; 2: 表示设置Referer黑名单 |
149
|
|
|
* @param $norefer 0: 表示不允许空 Refer 访问; 1: 表示允许空 Refer 访问 |
150
|
|
|
* @param $pattern 规则字符串, 当前允许格式分为三种: 一种为空主机头域名, 比如 foo.com; 一种是泛域名, 比如 *.bar.com; 一种是完全通配符, 即一个 *; 多个规则之间用;隔开, 比如: foo.com;*.bar.com;sub.foo.com;*.sub.bar.com |
151
|
|
|
* @param $source_enabled 源站是否支持,默认为0只给CDN配置, 设置为1表示开启源站防盗链 |
152
|
|
|
* |
153
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
154
|
|
|
*/ |
155
|
15 |
|
public function referAntiLeech(){ |
156
|
|
|
|
157
|
15 |
|
} |
158
|
15 |
|
|
159
|
15 |
|
/** |
160
|
15 |
|
* 增加bucket生命规则 |
161
|
3 |
|
* |
162
|
3 |
|
* @param $bucket 空间名 |
163
|
15 |
|
* @param $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线 |
164
|
15 |
|
* @param $prefix 同一个 bucket 里面前缀不能重复 |
165
|
|
|
* @param $delete_after_days 指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除,需大于 to_line_after_days |
166
|
|
|
* @param $to_line_after_days 指定文件上传多少天后转低频存储。指定为0表示不转低频存储,小于0表示上传的文件立即变低频存储 |
167
|
|
|
* |
168
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
169
|
|
|
*/ |
170
|
|
|
public function bucketLifecycleRule($bucket, $name, $prefix, $delete_after_days, $to_line_after_days){ |
171
|
|
|
$path = '/rules/add'; |
172
|
|
|
if ($bucket) { |
173
|
|
|
$params['bucket'] = $bucket; |
|
|
|
|
174
|
|
|
} |
175
|
|
|
if ($name) { |
176
|
|
|
$params['name'] = $name; |
|
|
|
|
177
|
|
|
} |
178
|
3 |
|
if ($prefix) { |
179
|
|
|
$params['prefix'] = $prefix; |
180
|
3 |
|
} |
181
|
3 |
|
if ($delete_after_days) { |
182
|
3 |
|
$params['delete_after_days'] = $delete_after_days; |
183
|
3 |
|
} |
184
|
|
|
if ($to_line_after_days) { |
185
|
|
|
$params['to_line_after_days'] = $to_line_after_days; |
186
|
3 |
|
} |
187
|
3 |
|
$data = http_build_query($params); |
188
|
|
|
$info = $this->ucPost($path, $data); |
189
|
|
|
return $info; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* 更新bucket生命规则 |
194
|
|
|
* |
195
|
|
|
* @param $bucket 空间名 |
196
|
|
|
* @param $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线 |
197
|
|
|
* @param $prefix 同一个 bucket 里面前缀不能重复 |
198
|
|
|
* @param $delete_after_days 指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除,需大于 to_line_after_days |
199
|
|
|
* @param $to_line_after_days 指定文件上传多少天后转低频存储。指定为0表示不转低频存储,小于0表示上传的文件立即变低频存储 |
200
|
3 |
|
* |
201
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
202
|
3 |
|
*/ |
203
|
3 |
|
public function updateBucketLifecycleRule($bucket, $name, $prefix, $delete_after_days, $to_line_after_days){ |
204
|
3 |
|
$path = '/rules/update'; |
205
|
3 |
|
if ($bucket) { |
206
|
3 |
|
$params['bucket'] = $bucket; |
|
|
|
|
207
|
|
|
} |
208
|
|
|
if ($name) { |
209
|
|
|
$params['name'] = $name; |
|
|
|
|
210
|
|
|
} |
211
|
|
|
if ($prefix) { |
212
|
|
|
$params['prefix'] = $prefix; |
213
|
|
|
} |
214
|
|
|
if ($delete_after_days) { |
215
|
|
|
$params['delete_after_days'] = $delete_after_days; |
216
|
|
|
} |
217
|
|
|
if ($to_line_after_days) { |
218
|
|
|
$params['to_line_after_days'] = $to_line_after_days; |
219
|
|
|
} |
220
|
|
|
$data = http_build_query($params); |
221
|
|
|
$info = $this->ucPost($path, $data); |
222
|
|
|
return $info; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* 获取bucket生命规则 |
227
|
|
|
* |
228
|
|
|
* @param $bucket 空间名 |
229
|
|
|
* |
230
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
231
|
|
|
*/ |
232
|
|
|
public function getBucketLifecycleRules($bucket){ |
233
|
|
|
$path = '/rules/get?bucket=' . $bucket; |
234
|
|
|
$info = $this->ucGet($path); |
235
|
|
|
return $info; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* 删除bucket生命规则 |
240
|
|
|
* |
241
|
|
|
* @param $bucket 空间名 |
242
|
|
|
* @param $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线 |
243
|
|
|
* |
244
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
245
|
|
|
*/ |
246
|
|
|
public function deleteBucketLifecycleRule($bucket, $name){ |
247
|
|
|
$path = '/rules/delete'; |
248
|
|
|
if ($bucket) { |
249
|
|
|
$params['bucket'] = $bucket; |
|
|
|
|
250
|
|
|
} |
251
|
|
|
if ($name) { |
252
|
|
|
$params['name'] = $name; |
|
|
|
|
253
|
|
|
} |
254
|
|
|
$data = http_build_query($params); |
255
|
|
|
$info = $this->ucPost($path, $data); |
256
|
|
|
return $info; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* 增加bucket事件通知规则 |
261
|
|
|
* |
262
|
|
|
* @param $bucket 空间名 |
263
|
|
|
* @param $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线 |
264
|
|
|
* @param $prefix 同一个 bucket 里面前缀不能重复 |
265
|
|
|
* @param $suffix 可选,文件配置的后缀 |
266
|
|
|
* @param $event 事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,disable,enable,deleteMarkerCreate |
267
|
|
|
* @param $callbackURL 通知URL,可以指定多个,失败依次重试 |
268
|
3 |
|
* @param $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名 |
269
|
|
|
* @param $host 可选,通知请求的host |
270
|
|
|
* |
271
|
3 |
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
272
|
3 |
|
*/ |
273
|
3 |
|
public function putBucketEvent($bucket, $name, $prefix, $suffix, $event, $callbackURL, $access_key=null, $host=null){ |
274
|
|
|
$path = '/events/add'; |
275
|
3 |
|
if ($bucket) { |
276
|
3 |
|
$params['bucket'] = $bucket; |
|
|
|
|
277
|
|
|
} |
278
|
3 |
|
if ($name) { |
279
|
3 |
|
$params['name'] = $name; |
|
|
|
|
280
|
|
|
} |
281
|
|
|
if ($prefix) { |
282
|
|
|
$params['prefix'] = $prefix; |
283
|
|
|
} |
284
|
|
|
if ($suffix) { |
285
|
|
|
$params['suffix'] = $suffix; |
286
|
|
|
} |
287
|
|
|
if ($event) { |
288
|
|
|
$params['event'] = $event; |
289
|
|
|
} |
290
|
|
|
if ($callbackURL) { |
291
|
3 |
|
$params['callbackURL'] = $callbackURL; |
292
|
|
|
} |
293
|
3 |
|
if ($access_key) { |
294
|
3 |
|
$params['access_key'] = $access_key; |
295
|
|
|
} |
296
|
3 |
|
if ($host) { |
297
|
3 |
|
$params['host'] = $host; |
298
|
|
|
} |
299
|
3 |
|
$data = http_build_query($params); |
300
|
3 |
|
$info = $this->ucPost($path, $data); |
301
|
3 |
|
return $info; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* 更新bucket事件通知规则 |
306
|
|
|
* |
307
|
|
|
* @param $bucket 空间名 |
308
|
|
|
* @param $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线 |
309
|
|
|
* @param $prefix 同一个 bucket 里面前缀不能重复 |
310
|
|
|
* @param $suffix 可选,文件配置的后缀 |
311
|
|
|
* @param $event 事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,disable,enable,deleteMarkerCreate |
312
|
|
|
* @param $callbackURL 通知URL,可以指定多个,失败依次重试 |
313
|
|
|
* @param $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名 |
314
|
|
|
* @param $host 可选,通知请求的host |
315
|
|
|
* |
316
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
317
|
|
|
*/ |
318
|
|
|
public function updateBucketEvent($bucket, $name, $prefix, $suffix, $event, $callbackURL, $access_key=null, $host=null){ |
319
|
|
|
$path = '/events/update'; |
320
|
12 |
|
if ($bucket) { |
321
|
|
|
$params['bucket'] = $bucket; |
|
|
|
|
322
|
12 |
|
} |
323
|
12 |
|
if ($name) { |
324
|
|
|
$params['name'] = $name; |
|
|
|
|
325
|
|
|
} |
326
|
|
|
if ($prefix) { |
327
|
|
|
$params['prefix'] = $prefix; |
328
|
|
|
} |
329
|
|
|
if ($suffix) { |
330
|
|
|
$params['suffix'] = $suffix; |
331
|
|
|
} |
332
|
|
|
if ($event) { |
333
|
|
|
$params['event'] = $event; |
334
|
|
|
} |
335
|
|
|
if ($callbackURL) { |
336
|
3 |
|
$params['callbackURL'] = $callbackURL; |
337
|
|
|
} |
338
|
3 |
|
if ($access_key) { |
339
|
3 |
|
$params['access_key'] = $access_key; |
340
|
3 |
|
} |
341
|
3 |
|
if ($host) { |
342
|
|
|
$params['host'] = $host; |
343
|
|
|
} |
344
|
3 |
|
$data = http_build_query($params); |
345
|
|
|
$info = $this->ucPost($path, $data); |
346
|
3 |
|
return $info; |
347
|
3 |
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
3 |
|
* 获取bucket事件通知规则 |
351
|
|
|
* |
352
|
|
|
* @param $bucket 空间名 |
353
|
33 |
|
* |
354
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
355
|
33 |
|
*/ |
356
|
33 |
|
public function getBucketEvents($bucket){ |
357
|
|
|
$path = '/events/get?bucket=' . $bucket; |
358
|
|
|
$info = $this->ucGet($path); |
359
|
33 |
|
return $info; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* 删除bucket事件通知规则 |
364
|
|
|
* |
365
|
|
|
* @param $bucket 空间名 |
366
|
|
|
* @param $name 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线 |
367
|
|
|
* |
368
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
369
|
|
|
*/ |
370
|
|
|
public function deleteBucketEvent($bucket, $name){ |
371
|
27 |
|
$path = '/events/delete'; |
372
|
|
|
if ($bucket) { |
373
|
27 |
|
$params['bucket'] = $bucket; |
|
|
|
|
374
|
27 |
|
} |
375
|
|
|
if ($name) { |
376
|
|
|
$params['name'] = $name; |
|
|
|
|
377
|
|
|
} |
378
|
|
|
$data = http_build_query($params); |
379
|
|
|
$info = $this->ucPost($path, $data); |
380
|
|
|
return $info; |
381
|
|
|
} |
382
|
|
|
|
383
|
9 |
|
/** |
384
|
|
|
* 设置bucket的跨域信息,最多允许设置10条跨域规则。 |
385
|
9 |
|
* 对于同一个域名如果设置了多条规则,那么按顺序使用第一条匹配的规则去生成返回值。 |
386
|
9 |
|
* 对于简单跨域请求,只匹配 Origin; |
387
|
|
|
* 对于预检请求, 需要匹配 Origin、AllowedMethod、AllowedHeader; |
388
|
|
|
* allowed_orgin: 允许的域名。必填;支持通配符*;*表示全部匹配;只有第一个*生效;需要设置"Scheme";大小写敏感。例如 |
389
|
12 |
|
* 规则:http://*.abc.*.com 请求:"http://test.abc.test.com" 结果:不通过 |
390
|
|
|
* 规则:"http://abc.com" 请求:"https://abc.com"/"abc.com" 结果:不通过 |
391
|
12 |
|
* 规则:"abc.com" 请求:"http://abc.com" 结果:不通过 |
392
|
12 |
|
* allowed_method: 允许的方法。必填;不支持通配符;大小写不敏感; |
393
|
12 |
|
* allowed_header: 允许的header。选填;支持通配符*,但只能是单独的*,表示允许全部header,其他*不生效;空则不允许任何header;大小写不敏感; |
394
|
8 |
|
* exposed_header: 暴露的header。选填;不支持通配符;X-Log, X-Reqid是默认会暴露的两个header;其他的header如果没有设置,则不会暴露;大小写不敏感; |
395
|
|
|
* max_age: 结果可以缓存的时间。选填;空则不缓存; |
396
|
8 |
|
* allowed_credentials:该配置不支持设置,默认为true。 |
397
|
|
|
* 备注:如果没有设置任何corsRules,那么默认允许所有的跨域请求 |
398
|
|
|
*/ |
399
|
33 |
|
public function putCorsRules($bucket, $params){ |
400
|
|
|
$path = '/corsRules/set/' . $bucket; |
401
|
33 |
|
$data = json_encode($params); |
402
|
33 |
|
$info = $this->ucPost($path, $data); |
403
|
33 |
|
return $info; |
404
|
9 |
|
} |
405
|
|
|
|
406
|
30 |
|
/** |
407
|
30 |
|
* 获取bucket的跨域信息 |
408
|
|
|
* |
409
|
|
|
* $bucket 空间名 |
410
|
3 |
|
*/ |
411
|
|
|
public function getCorsRules($bucket){ |
412
|
3 |
|
$path = '/corsRules/get/' . $bucket; |
413
|
|
|
$info = $this->ucGet($path); |
414
|
|
|
return $info; |
415
|
|
|
} |
416
|
3 |
|
|
417
|
|
|
/** |
418
|
3 |
|
* 设置回源规则 |
419
|
|
|
* 使用该API设置源站优先级高于/image设置的源站,即IO优先读取source接口设置的源站配置,如果存在会忽略/image设置的源站 |
420
|
|
|
* Bucket 空间名 |
421
|
|
|
* Host(可选)回源Host |
422
|
6 |
|
* RetryCodes(可选),镜像回源时源站返回Code可以重试,最多指定3个,当前只支持4xx错误码重试 |
423
|
|
|
* SourceQiniuAK,SourceQiniuSK(可选)如果存在将在回源时对URL进行签名,客户源站可以验证以保证请求来自Qiniu服务器 |
424
|
6 |
|
* Expires(可选) 签名过期时间,如果不设置默认为1小时 |
425
|
|
|
* Addr 回源地址,不可重复。 |
426
|
|
|
* Weight 权重,范围限制1-100,不填默认为1,回源时会根据所有源的权重值进行源站选择,主备源会分开计算. |
427
|
|
|
* Backup 是否备用回源,回源优先尝试主源 |
428
|
3 |
|
*/ |
429
|
|
|
public function putBucktSourceConfig($params){ |
430
|
3 |
|
$path = '/mirrorConfig/set'; |
431
|
|
|
$data = json_encode($params); |
432
|
|
|
$info = $this->ucPostV2($path, $data); |
433
|
|
|
return $info; |
434
|
3 |
|
} |
435
|
|
|
|
436
|
3 |
|
/** |
437
|
|
|
* 获取空间回源配置 |
438
|
|
|
*/ |
439
|
|
|
public function getBucktSourceConfig($params){ |
440
|
|
|
$path = '/mirrorConfig/get'; |
441
|
|
|
$data = json_encode($params); |
442
|
|
|
$info = $this->ucPostV2($path, $data); |
443
|
|
|
return $info; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* 获取资源的元信息,但不返回文件内容 |
448
|
|
|
* |
449
|
|
|
* @param $bucket 待获取信息资源所在的空间 |
450
|
|
|
* @param $key 待获取资源的文件名 |
451
|
|
|
* |
452
|
|
|
* @return array 包含文件信息的数组,类似: |
453
|
|
|
* [ |
454
|
|
|
* "hash" => "<Hash string>", |
455
|
|
|
* "key" => "<Key string>", |
456
|
|
|
* "fsize" => <file size>, |
457
|
|
|
* "putTime" => "<file modify time>" |
458
|
|
|
* "fileType" => <file type> |
459
|
|
|
* ] |
460
|
|
|
* |
461
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html |
462
|
|
|
*/ |
463
|
|
|
public function stat($bucket, $key) |
464
|
|
|
{ |
465
|
|
|
$path = '/stat/' . \Qiniu\entry($bucket, $key); |
466
|
6 |
|
return $this->rsGet($path); |
467
|
|
|
} |
468
|
6 |
|
|
469
|
6 |
|
/** |
470
|
6 |
|
* 删除指定资源 |
471
|
6 |
|
* |
472
|
6 |
|
* @param $bucket 待删除资源所在的空间 |
473
|
|
|
* @param $key 待删除资源的文件名 |
474
|
|
|
* |
475
|
9 |
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
476
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html |
477
|
9 |
|
*/ |
478
|
|
|
public function delete($bucket, $key) |
479
|
|
|
{ |
480
|
9 |
|
$path = '/delete/' . \Qiniu\entry($bucket, $key); |
481
|
9 |
|
list(, $error) = $this->rsPost($path); |
482
|
9 |
|
return $error; |
483
|
9 |
|
} |
484
|
9 |
|
|
485
|
9 |
|
|
486
|
9 |
|
/** |
487
|
9 |
|
* 给资源进行重命名,本质为move操作。 |
488
|
9 |
|
* |
489
|
9 |
|
* @param $bucket 待操作资源所在空间 |
490
|
9 |
|
* @param $oldname 待操作资源文件名 |
491
|
|
|
* @param $newname 目标资源文件名 |
492
|
|
|
* |
493
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
494
|
|
|
*/ |
495
|
|
|
public function rename($bucket, $oldname, $newname) |
496
|
|
|
{ |
497
|
|
|
return $this->move($bucket, $oldname, $bucket, $newname); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* 给资源进行重命名,本质为move操作。 |
502
|
|
|
* |
503
|
|
|
* @param $from_bucket 待操作资源所在空间 |
504
|
|
|
* @param $from_key 待操作资源文件名 |
505
|
|
|
* @param $to_bucket 目标资源空间名 |
506
|
|
|
* @param $to_key 目标资源文件名 |
507
|
|
|
* |
508
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
509
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html |
510
|
|
|
*/ |
511
|
|
|
public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
512
|
|
|
{ |
513
|
|
|
$from = \Qiniu\entry($from_bucket, $from_key); |
514
|
|
|
$to = \Qiniu\entry($to_bucket, $to_key); |
515
|
|
|
$path = '/copy/' . $from . '/' . $to; |
516
|
|
|
if ($force === true) { |
517
|
|
|
$path .= '/force/true'; |
518
|
|
|
} |
519
|
|
|
list(, $error) = $this->rsPost($path); |
520
|
|
|
return $error; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* 将资源从一个空间到另一个空间 |
525
|
|
|
* |
526
|
|
|
* @param $from_bucket 待操作资源所在空间 |
527
|
|
|
* @param $from_key 待操作资源文件名 |
528
|
|
|
* @param $to_bucket 目标资源空间名 |
529
|
|
|
* @param $to_key 目标资源文件名 |
530
|
|
|
* |
531
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
532
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html |
533
|
|
|
*/ |
534
|
|
|
public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
535
|
|
|
{ |
536
|
|
|
$from = \Qiniu\entry($from_bucket, $from_key); |
537
|
|
|
$to = \Qiniu\entry($to_bucket, $to_key); |
538
|
|
|
$path = '/move/' . $from . '/' . $to; |
539
|
|
|
if ($force) { |
540
|
|
|
$path .= '/force/true'; |
541
|
|
|
} |
542
|
|
|
list(, $error) = $this->rsPost($path); |
543
|
|
|
return $error; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* 主动修改指定资源的文件元信息 |
548
|
|
|
* |
549
|
|
|
* @param $bucket 待操作资源所在空间 |
550
|
|
|
* @param $key 待操作资源文件名 |
551
|
|
|
* @param $mime 待操作文件目标mimeType |
552
|
|
|
* |
553
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
554
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html |
555
|
|
|
*/ |
556
|
|
|
public function changeMime($bucket, $key, $mime) |
557
|
|
|
{ |
558
|
|
|
$resource = \Qiniu\entry($bucket, $key); |
559
|
|
|
$encode_mime = \Qiniu\base64_urlSafeEncode($mime); |
560
|
|
|
$path = '/chgm/' . $resource . '/mime/' . $encode_mime; |
561
|
|
|
list(, $error) = $this->rsPost($path); |
562
|
|
|
return $error; |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* 修改指定资源的存储类型 |
568
|
|
|
* |
569
|
|
|
* @param $bucket 待操作资源所在空间 |
570
|
|
|
* @param $key 待操作资源文件名 |
571
|
|
|
* @param $fileType 待操作文件目标文件类型 |
572
|
|
|
* |
573
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
574
|
|
|
* @link https://developer.qiniu.com/kodo/api/3710/modify-the-file-type |
575
|
|
|
*/ |
576
|
|
|
public function changeType($bucket, $key, $fileType) |
577
|
|
|
{ |
578
|
|
|
$resource = \Qiniu\entry($bucket, $key); |
579
|
|
|
$path = '/chtype/' . $resource . '/type/' . $fileType; |
580
|
|
|
list(, $error) = $this->rsPost($path); |
581
|
|
|
return $error; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* 修改文件的存储状态,即禁用状态和启用状态间的的互相转换 |
586
|
|
|
* |
587
|
|
|
* @param $bucket 待操作资源所在空间 |
588
|
|
|
* @param $key 待操作资源文件名 |
589
|
|
|
* @param $status 待操作文件目标文件类型 |
590
|
|
|
* |
591
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
592
|
|
|
* @link https://developer.qiniu.com/kodo/api/4173/modify-the-file-status |
593
|
|
|
*/ |
594
|
|
|
public function changeStatus($bucket, $key, $status) |
595
|
|
|
{ |
596
|
|
|
$resource = \Qiniu\entry($bucket, $key); |
597
|
|
|
$path = '/chstatus/' . $resource . '/status/' . $status; |
598
|
|
|
list(, $error) = $this->rsPost($path); |
599
|
|
|
return $error; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* 从指定URL抓取资源,并将该资源存储到指定空间中 |
604
|
|
|
* |
605
|
|
|
* @param $url 指定的URL |
606
|
|
|
* @param $bucket 目标资源空间 |
607
|
|
|
* @param $key 目标资源文件名 |
608
|
|
|
* |
609
|
|
|
* @return array 包含已拉取的文件信息。 |
610
|
|
|
* 成功时: [ |
611
|
|
|
* [ |
612
|
|
|
* "hash" => "<Hash string>", |
613
|
|
|
* "key" => "<Key string>" |
614
|
|
|
* ], |
615
|
|
|
* null |
616
|
|
|
* ] |
617
|
|
|
* |
618
|
|
|
* 失败时: [ |
619
|
|
|
* null, |
620
|
|
|
* Qiniu/Http/Error |
621
|
|
|
* ] |
622
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html |
623
|
|
|
*/ |
624
|
|
|
public function fetch($url, $bucket, $key = null) |
625
|
|
|
{ |
626
|
|
|
|
627
|
|
|
$resource = \Qiniu\base64_urlSafeEncode($url); |
628
|
|
|
$to = \Qiniu\entry($bucket, $key); |
629
|
|
|
$path = '/fetch/' . $resource . '/to/' . $to; |
630
|
|
|
|
631
|
|
|
$ak = $this->auth->getAccessKey(); |
632
|
|
|
$ioHost = $this->config->getIovipHost($ak, $bucket); |
633
|
|
|
|
634
|
|
|
$url = $ioHost . $path; |
635
|
|
|
return $this->post($url, null); |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源 |
640
|
|
|
* |
641
|
|
|
* @param $bucket 待获取资源所在的空间 |
642
|
|
|
* @param $key 代获取资源文件名 |
643
|
|
|
* |
644
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
645
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html |
646
|
|
|
*/ |
647
|
|
|
public function prefetch($bucket, $key) |
648
|
|
|
{ |
649
|
|
|
$resource = \Qiniu\entry($bucket, $key); |
650
|
|
|
$path = '/prefetch/' . $resource; |
651
|
|
|
|
652
|
|
|
$ak = $this->auth->getAccessKey(); |
653
|
|
|
$ioHost = $this->config->getIovipHost($ak, $bucket); |
654
|
|
|
|
655
|
|
|
$url = $ioHost . $path; |
656
|
|
|
list(, $error) = $this->post($url, null); |
657
|
|
|
return $error; |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* 在单次请求中进行多个资源管理操作 |
662
|
|
|
* |
663
|
|
|
* @param $operations 资源管理操作数组 |
664
|
|
|
* |
665
|
|
|
* @return array 每个资源的处理情况,结果类似: |
666
|
|
|
* [ |
667
|
|
|
* { "code" => <HttpCode int>, "data" => <Data> }, |
668
|
|
|
* { "code" => <HttpCode int> }, |
669
|
|
|
* { "code" => <HttpCode int> }, |
670
|
|
|
* { "code" => <HttpCode int> }, |
671
|
|
|
* { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } }, |
672
|
|
|
* ... |
673
|
|
|
* ] |
674
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html |
675
|
|
|
*/ |
676
|
|
|
public function batch($operations) |
677
|
|
|
{ |
678
|
|
|
$params = 'op=' . implode('&op=', $operations); |
679
|
|
|
return $this->rsPost('/batch', $params); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
/** |
683
|
|
|
* 设置文件的生命周期 |
684
|
|
|
* |
685
|
|
|
* @param $bucket 设置文件生命周期文件所在的空间 |
686
|
|
|
* @param $key 设置文件生命周期文件的文件名 |
687
|
|
|
* @param $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期 |
688
|
|
|
* |
689
|
|
|
* @return Mixed |
690
|
|
|
* @link https://developer.qiniu.com/kodo/api/update-file-lifecycle |
691
|
|
|
*/ |
692
|
|
|
public function deleteAfterDays($bucket, $key, $days) |
693
|
|
|
{ |
694
|
|
|
$entry = \Qiniu\entry($bucket, $key); |
695
|
|
|
$path = "/deleteAfterDays/$entry/$days"; |
696
|
|
|
list(, $error) = $this->rsPost($path); |
697
|
|
|
return $error; |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
private function getRsfHost() |
701
|
|
|
{ |
702
|
|
|
$scheme = "http://"; |
703
|
|
|
if ($this->config->useHTTPS == true) { |
|
|
|
|
704
|
|
|
$scheme = "https://"; |
705
|
|
|
} |
706
|
|
|
return $scheme . Config::RSF_HOST; |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
private function getRsHost() |
710
|
|
|
{ |
711
|
|
|
$scheme = "http://"; |
712
|
|
|
if ($this->config->useHTTPS == true) { |
|
|
|
|
713
|
|
|
$scheme = "https://"; |
714
|
|
|
} |
715
|
|
|
return $scheme . Config::RS_HOST; |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
private function getApiHost() |
719
|
|
|
{ |
720
|
|
|
$scheme = "http://"; |
721
|
|
|
if ($this->config->useHTTPS == true) { |
|
|
|
|
722
|
|
|
$scheme = "https://"; |
723
|
|
|
} |
724
|
|
|
return $scheme . Config::API_HOST; |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
private function getUcHost() |
728
|
|
|
{ |
729
|
|
|
$scheme = "http://"; |
730
|
|
|
if ($this->config->useHTTPS == true) { |
|
|
|
|
731
|
|
|
$scheme = "https://"; |
732
|
|
|
} |
733
|
|
|
return $scheme . Config::UC_HOST; |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
private function rsPost($path, $body = null) |
737
|
|
|
{ |
738
|
|
|
$url = $this->getRsHost() . $path; |
739
|
|
|
return $this->post($url, $body); |
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
private function apiPost($path, $body = null) |
|
|
|
|
743
|
|
|
{ |
744
|
|
|
$url = $this->getApiHost() . $path; |
745
|
|
|
return $this->post($url, $body); |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
private function ucPost($path, $body = null) |
749
|
|
|
{ |
750
|
|
|
$url = $this->getUcHost() . $path; |
751
|
|
|
return $this->post($url, $body); |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
private function ucGet($path) |
755
|
|
|
{ |
756
|
|
|
$url = $this->getUcHost() . $path; |
757
|
|
|
return $this->get($url); |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
private function apiGet($path) |
761
|
|
|
{ |
762
|
|
|
$url = $this->getApiHost() . $path; |
763
|
|
|
return $this->get($url); |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
private function rsGet($path) |
767
|
|
|
{ |
768
|
|
|
$url = $this->getRsHost() . $path; |
769
|
|
|
return $this->get($url); |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
private function get($url) |
773
|
|
|
{ |
774
|
|
|
$headers = $this->auth->authorization($url); |
775
|
|
|
$ret = Client::get($url, $headers); |
776
|
|
|
if (!$ret->ok()) { |
777
|
|
|
return array(null, new Error($url, $ret)); |
778
|
|
|
} |
779
|
|
|
return array($ret->json(), null); |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
private function post($url, $body) |
783
|
|
|
{ |
784
|
|
|
$headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded'); |
785
|
|
|
$ret = Client::post($url, $body, $headers); |
786
|
|
|
if (!$ret->ok()) { |
787
|
|
|
return array(null, new Error($url, $ret)); |
788
|
|
|
} |
789
|
|
|
$r = ($ret->body === null) ? array() : $ret->json(); |
790
|
|
|
return array($r, null); |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
private function ucPostV2($path, $body){ |
794
|
|
|
$url = $this->getUcHost() . $path; |
795
|
|
|
return $this->postV2($url, $body); |
796
|
|
|
} |
797
|
|
|
|
798
|
|
|
private function postV2($url, $body) |
799
|
|
|
{ |
800
|
|
|
$headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/json'); |
801
|
|
|
$headers["Content-Type"] = 'application/json'; |
802
|
|
|
$ret = Client::post($url, $body, $headers); |
803
|
|
|
if (!$ret->ok()) { |
804
|
|
|
return array(null, new Error($url, $ret)); |
805
|
|
|
} |
806
|
|
|
$r = ($ret->body === null) ? array() : $ret->json(); |
807
|
|
|
return array($r, null); |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force) |
811
|
|
|
{ |
812
|
|
|
return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force); |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
|
816
|
|
|
public static function buildBatchRename($bucket, $key_pairs, $force) |
817
|
|
|
{ |
818
|
|
|
return self::buildBatchMove($bucket, $key_pairs, $bucket, $force); |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
|
822
|
|
|
public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force) |
823
|
|
|
{ |
824
|
|
|
return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force); |
825
|
|
|
} |
826
|
|
|
|
827
|
|
|
|
828
|
|
|
public static function buildBatchDelete($bucket, $keys) |
829
|
|
|
{ |
830
|
|
|
return self::oneKeyBatch('/delete', $bucket, $keys); |
831
|
|
|
} |
832
|
|
|
|
833
|
|
|
|
834
|
|
|
public static function buildBatchStat($bucket, $keys) |
835
|
|
|
{ |
836
|
|
|
return self::oneKeyBatch('/stat', $bucket, $keys); |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs) |
840
|
|
|
{ |
841
|
|
|
$data = array(); |
842
|
|
|
foreach ($key_day_pairs as $key => $day) { |
843
|
|
|
array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day); |
844
|
|
|
} |
845
|
|
|
return $data; |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
public static function buildBatchChangeMime($bucket, $key_mime_pairs) |
849
|
|
|
{ |
850
|
|
|
$data = array(); |
851
|
|
|
foreach ($key_mime_pairs as $key => $mime) { |
852
|
|
|
array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime)); |
853
|
|
|
} |
854
|
|
|
return $data; |
855
|
|
|
} |
856
|
|
|
|
857
|
|
|
public static function buildBatchChangeType($bucket, $key_type_pairs) |
858
|
|
|
{ |
859
|
|
|
$data = array(); |
860
|
|
|
foreach ($key_type_pairs as $key => $type) { |
861
|
|
|
array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type); |
862
|
|
|
} |
863
|
|
|
return $data; |
864
|
|
|
} |
865
|
|
|
|
866
|
|
|
private static function oneKeyBatch($operation, $bucket, $keys) |
867
|
|
|
{ |
868
|
|
|
$data = array(); |
869
|
|
|
foreach ($keys as $key) { |
870
|
|
|
array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key)); |
871
|
|
|
} |
872
|
|
|
return $data; |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force) |
876
|
|
|
{ |
877
|
|
|
if ($target_bucket === null) { |
878
|
|
|
$target_bucket = $source_bucket; |
879
|
|
|
} |
880
|
|
|
$data = array(); |
881
|
|
|
$forceOp = "false"; |
882
|
|
|
if ($force) { |
883
|
|
|
$forceOp = "true"; |
884
|
|
|
} |
885
|
|
|
foreach ($key_pairs as $from_key => $to_key) { |
886
|
|
|
$from = \Qiniu\entry($source_bucket, $from_key); |
887
|
|
|
$to = \Qiniu\entry($target_bucket, $to_key); |
888
|
|
|
array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp); |
889
|
|
|
} |
890
|
|
|
return $data; |
891
|
|
|
} |
892
|
|
|
} |
893
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.