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
|
|
|
* @return string[] 包含所有空间域名 |
48
|
|
|
*/ |
49
|
|
|
public function domains($bucket) |
50
|
|
|
{ |
51
|
|
|
return $this->apiGet('/v6/domain/list?tbl=' . $bucket); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* 获取空间绑定的域名列表 |
56
|
|
|
* @return string[] 包含空间绑定的所有域名 |
57
|
|
|
*/ |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* 列取空间的文件列表 |
61
|
|
|
* |
62
|
|
|
* @param $bucket 空间名 |
63
|
|
|
* @param $prefix 列举前缀 |
64
|
|
|
* @param $marker 列举标识符 |
65
|
|
|
* @param $limit 单次列举个数限制 |
66
|
|
|
* @param $delimiter 指定目录分隔符 |
67
|
|
|
* |
68
|
|
|
* @return array 包含文件信息的数组,类似:[ |
69
|
|
|
* { |
70
|
|
|
* "hash" => "<Hash string>", |
71
|
|
|
* "key" => "<Key string>", |
72
|
|
|
* "fsize" => "<file size>", |
73
|
|
|
* "putTime" => "<file modify time>" |
74
|
|
|
* }, |
75
|
|
|
* ... |
76
|
|
|
* ] |
77
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html |
78
|
|
|
*/ |
79
|
3 |
|
public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null) |
80
|
|
|
{ |
81
|
3 |
|
$query = array('bucket' => $bucket); |
82
|
3 |
|
\Qiniu\setWithoutEmpty($query, 'prefix', $prefix); |
83
|
3 |
|
\Qiniu\setWithoutEmpty($query, 'marker', $marker); |
84
|
3 |
|
\Qiniu\setWithoutEmpty($query, 'limit', $limit); |
85
|
3 |
|
\Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter); |
86
|
3 |
|
$url = $this->getRsfHost() . '/list?' . http_build_query($query); |
87
|
3 |
|
return $this->get($url); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* 获取资源的元信息,但不返回文件内容 |
92
|
|
|
* |
93
|
|
|
* @param $bucket 待获取信息资源所在的空间 |
94
|
|
|
* @param $key 待获取资源的文件名 |
95
|
|
|
* |
96
|
|
|
* @return array 包含文件信息的数组,类似: |
97
|
|
|
* [ |
98
|
|
|
* "hash" => "<Hash string>", |
99
|
|
|
* "key" => "<Key string>", |
100
|
|
|
* "fsize" => <file size>, |
101
|
|
|
* "putTime" => "<file modify time>" |
102
|
|
|
* "fileType" => <file type> |
103
|
|
|
* ] |
104
|
|
|
* |
105
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html |
106
|
|
|
*/ |
107
|
3 |
|
public function stat($bucket, $key) |
108
|
|
|
{ |
109
|
3 |
|
$path = '/stat/' . \Qiniu\entry($bucket, $key); |
110
|
3 |
|
return $this->rsGet($path); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* 删除指定资源 |
115
|
|
|
* |
116
|
|
|
* @param $bucket 待删除资源所在的空间 |
117
|
|
|
* @param $key 待删除资源的文件名 |
118
|
|
|
* |
119
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
120
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html |
121
|
|
|
*/ |
122
|
6 |
|
public function delete($bucket, $key) |
123
|
|
|
{ |
124
|
6 |
|
$path = '/delete/' . \Qiniu\entry($bucket, $key); |
125
|
6 |
|
list(, $error) = $this->rsPost($path); |
126
|
6 |
|
return $error; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* 给资源进行重命名,本质为move操作。 |
132
|
|
|
* |
133
|
|
|
* @param $bucket 待操作资源所在空间 |
134
|
|
|
* @param $oldname 待操作资源文件名 |
135
|
|
|
* @param $newname 目标资源文件名 |
136
|
|
|
* |
137
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
138
|
|
|
*/ |
139
|
3 |
|
public function rename($bucket, $oldname, $newname) |
140
|
|
|
{ |
141
|
3 |
|
return $this->move($bucket, $oldname, $bucket, $newname); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* 给资源进行重命名,本质为move操作。 |
146
|
|
|
* |
147
|
|
|
* @param $from_bucket 待操作资源所在空间 |
148
|
|
|
* @param $from_key 待操作资源文件名 |
149
|
|
|
* @param $to_bucket 目标资源空间名 |
150
|
|
|
* @param $to_key 目标资源文件名 |
151
|
|
|
* |
152
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
153
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html |
154
|
|
|
*/ |
155
|
12 |
|
public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
156
|
|
|
{ |
157
|
12 |
|
$from = \Qiniu\entry($from_bucket, $from_key); |
158
|
12 |
|
$to = \Qiniu\entry($to_bucket, $to_key); |
159
|
12 |
|
$path = '/copy/' . $from . '/' . $to; |
160
|
12 |
|
if ($force === true) { |
161
|
|
|
$path .= '/force/true'; |
162
|
|
|
} |
163
|
12 |
|
list(, $error) = $this->rsPost($path); |
164
|
12 |
|
return $error; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* 将资源从一个空间到另一个空间 |
169
|
|
|
* |
170
|
|
|
* @param $from_bucket 待操作资源所在空间 |
171
|
|
|
* @param $from_key 待操作资源文件名 |
172
|
|
|
* @param $to_bucket 目标资源空间名 |
173
|
|
|
* @param $to_key 目标资源文件名 |
174
|
|
|
* |
175
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
176
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html |
177
|
|
|
*/ |
178
|
3 |
|
public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
179
|
|
|
{ |
180
|
3 |
|
$from = \Qiniu\entry($from_bucket, $from_key); |
181
|
3 |
|
$to = \Qiniu\entry($to_bucket, $to_key); |
182
|
3 |
|
$path = '/move/' . $from . '/' . $to; |
183
|
3 |
|
if ($force) { |
184
|
|
|
$path .= '/force/true'; |
185
|
|
|
} |
186
|
3 |
|
list(, $error) = $this->rsPost($path); |
187
|
3 |
|
return $error; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* 主动修改指定资源的文件类型 |
192
|
|
|
* |
193
|
|
|
* @param $bucket 待操作资源所在空间 |
194
|
|
|
* @param $key 待操作资源文件名 |
195
|
|
|
* @param $mime 待操作文件目标mimeType |
196
|
|
|
* |
197
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
198
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html |
199
|
|
|
*/ |
200
|
3 |
|
public function changeMime($bucket, $key, $mime) |
201
|
|
|
{ |
202
|
3 |
|
$resource = \Qiniu\entry($bucket, $key); |
203
|
3 |
|
$encode_mime = \Qiniu\base64_urlSafeEncode($mime); |
204
|
3 |
|
$path = '/chgm/' . $resource . '/mime/' . $encode_mime; |
205
|
3 |
|
list(, $error) = $this->rsPost($path); |
206
|
3 |
|
return $error; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* 修改指定资源的存储类型 |
212
|
|
|
* |
213
|
|
|
* @param $bucket 待操作资源所在空间 |
214
|
|
|
* @param $key 待操作资源文件名 |
215
|
|
|
* @param $fileType 待操作文件目标文件类型 |
216
|
|
|
* |
217
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
218
|
|
|
* @link https://developer.qiniu.com/kodo/api/3710/modify-the-file-type |
219
|
|
|
*/ |
220
|
|
|
public function changeType($bucket, $key, $fileType) |
221
|
|
|
{ |
222
|
|
|
$resource = \Qiniu\entry($bucket, $key); |
223
|
|
|
$path = '/chtype/' . $resource . '/type/' . $fileType; |
224
|
|
|
list(, $error) = $this->rsPost($path); |
225
|
|
|
return $error; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* 从指定URL抓取资源,并将该资源存储到指定空间中 |
231
|
|
|
* |
232
|
|
|
* @param $url 指定的URL |
233
|
|
|
* @param $bucket 目标资源空间 |
234
|
|
|
* @param $key 目标资源文件名 |
235
|
|
|
* |
236
|
|
|
* @return array 包含已拉取的文件信息。 |
237
|
|
|
* 成功时: [ |
238
|
|
|
* [ |
239
|
|
|
* "hash" => "<Hash string>", |
240
|
|
|
* "key" => "<Key string>" |
241
|
|
|
* ], |
242
|
|
|
* null |
243
|
|
|
* ] |
244
|
|
|
* |
245
|
|
|
* 失败时: [ |
246
|
|
|
* null, |
247
|
|
|
* Qiniu/Http/Error |
248
|
|
|
* ] |
249
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html |
250
|
|
|
*/ |
251
|
3 |
|
public function fetch($url, $bucket, $key = null) |
252
|
|
|
{ |
253
|
|
|
|
254
|
3 |
|
$resource = \Qiniu\base64_urlSafeEncode($url); |
255
|
3 |
|
$to = \Qiniu\entry($bucket, $key); |
256
|
3 |
|
$path = '/fetch/' . $resource . '/to/' . $to; |
257
|
|
|
|
258
|
3 |
|
$ak = $this->auth->getAccessKey(); |
259
|
3 |
|
$ioHost = $this->config->getIovipHost($ak, $bucket); |
260
|
|
|
|
261
|
|
|
$url = $ioHost . $path; |
262
|
|
|
return $this->post($url, null); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源 |
267
|
|
|
* |
268
|
|
|
* @param $bucket 待获取资源所在的空间 |
269
|
|
|
* @param $key 代获取资源文件名 |
270
|
|
|
* |
271
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
272
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html |
273
|
|
|
*/ |
274
|
3 |
|
public function prefetch($bucket, $key) |
275
|
|
|
{ |
276
|
3 |
|
$resource = \Qiniu\entry($bucket, $key); |
277
|
3 |
|
$path = '/prefetch/' . $resource; |
278
|
|
|
|
279
|
3 |
|
$ak = $this->auth->getAccessKey(); |
280
|
3 |
|
$ioHost = $this->config->getIovipHost($ak, $bucket); |
281
|
|
|
|
282
|
|
|
$url = $ioHost . $path; |
283
|
|
|
list(, $error) = $this->post($url, null); |
284
|
|
|
return $error; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* 在单次请求中进行多个资源管理操作 |
289
|
|
|
* |
290
|
|
|
* @param $operations 资源管理操作数组 |
291
|
|
|
* |
292
|
|
|
* @return array 每个资源的处理情况,结果类似: |
293
|
|
|
* [ |
294
|
|
|
* { "code" => <HttpCode int>, "data" => <Data> }, |
295
|
|
|
* { "code" => <HttpCode int> }, |
296
|
|
|
* { "code" => <HttpCode int> }, |
297
|
|
|
* { "code" => <HttpCode int> }, |
298
|
|
|
* { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } }, |
299
|
|
|
* ... |
300
|
|
|
* ] |
301
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html |
302
|
|
|
*/ |
303
|
12 |
|
public function batch($operations) |
304
|
|
|
{ |
305
|
12 |
|
$params = 'op=' . implode('&op=', $operations); |
306
|
12 |
|
return $this->rsPost('/batch', $params); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* 设置文件的生命周期 |
311
|
|
|
* |
312
|
|
|
* @param $bucket 设置文件生命周期文件所在的空间 |
313
|
|
|
* @param $key 设置文件生命周期文件的文件名 |
314
|
|
|
* @param $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期 |
315
|
|
|
* |
316
|
|
|
* @return Mixed |
317
|
|
|
* @link https://developer.qiniu.com/kodo/api/update-file-lifecycle |
318
|
|
|
*/ |
319
|
3 |
|
public function deleteAfterDays($bucket, $key, $days) |
320
|
|
|
{ |
321
|
3 |
|
$entry = \Qiniu\entry($bucket, $key); |
322
|
3 |
|
$path = "/deleteAfterDays/$entry/$days"; |
323
|
3 |
|
list(, $error) = $this->rsPost($path); |
324
|
3 |
|
return $error; |
325
|
|
|
} |
326
|
|
|
|
327
|
3 |
|
private function getRsfHost() |
328
|
|
|
{ |
329
|
3 |
|
$scheme = "http://"; |
330
|
3 |
|
if ($this->config->useHTTPS == true) { |
|
|
|
|
331
|
|
|
$scheme = "https://"; |
332
|
|
|
} |
333
|
3 |
|
return $scheme . Config::RSF_HOST; |
334
|
|
|
} |
335
|
|
|
|
336
|
33 |
|
private function getRsHost() |
337
|
|
|
{ |
338
|
33 |
|
$scheme = "http://"; |
339
|
33 |
|
if ($this->config->useHTTPS == true) { |
|
|
|
|
340
|
|
|
$scheme = "https://"; |
341
|
|
|
} |
342
|
33 |
|
return $scheme . Config::RS_HOST; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
private function getApiHost() |
346
|
|
|
{ |
347
|
|
|
$scheme = "http://"; |
348
|
|
|
if ($this->config->useHTTPS == true) { |
|
|
|
|
349
|
|
|
$scheme = "https://"; |
350
|
|
|
} |
351
|
|
|
return $scheme . Config::API_HOST; |
352
|
|
|
} |
353
|
|
|
|
354
|
27 |
|
private function rsPost($path, $body = null) |
355
|
|
|
{ |
356
|
27 |
|
$url = $this->getRsHost() . $path; |
357
|
27 |
|
return $this->post($url, $body); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
private function apiGet($path) |
361
|
|
|
{ |
362
|
|
|
$url = $this->getApiHost() . $path; |
363
|
|
|
return $this->get($url); |
364
|
|
|
} |
365
|
|
|
|
366
|
6 |
|
private function rsGet($path) |
367
|
|
|
{ |
368
|
6 |
|
$url = $this->getRsHost() . $path; |
369
|
6 |
|
return $this->get($url); |
370
|
|
|
} |
371
|
|
|
|
372
|
9 |
|
private function get($url) |
373
|
|
|
{ |
374
|
9 |
|
$headers = $this->auth->authorization($url); |
375
|
9 |
|
$ret = Client::get($url, $headers); |
376
|
9 |
|
if (!$ret->ok()) { |
377
|
9 |
|
return array(null, new Error($url, $ret)); |
378
|
|
|
} |
379
|
|
|
return array($ret->json(), null); |
380
|
|
|
} |
381
|
|
|
|
382
|
27 |
|
private function post($url, $body) |
383
|
|
|
{ |
384
|
27 |
|
$headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded'); |
385
|
27 |
|
$ret = Client::post($url, $body, $headers); |
386
|
27 |
|
if (!$ret->ok()) { |
387
|
27 |
|
return array(null, new Error($url, $ret)); |
388
|
|
|
} |
389
|
|
|
$r = ($ret->body === null) ? array() : $ret->json(); |
390
|
|
|
return array($r, null); |
391
|
|
|
} |
392
|
|
|
|
393
|
3 |
|
public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force) |
394
|
|
|
{ |
395
|
3 |
|
return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
|
399
|
3 |
|
public static function buildBatchRename($bucket, $key_pairs, $force) |
400
|
|
|
{ |
401
|
3 |
|
return self::buildBatchMove($bucket, $key_pairs, $bucket, $force); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
|
405
|
6 |
|
public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force) |
406
|
|
|
{ |
407
|
6 |
|
return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
|
411
|
|
|
public static function buildBatchDelete($bucket, $keys) |
412
|
|
|
{ |
413
|
|
|
return self::oneKeyBatch('/delete', $bucket, $keys); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
|
417
|
3 |
|
public static function buildBatchStat($bucket, $keys) |
418
|
|
|
{ |
419
|
3 |
|
return self::oneKeyBatch('/stat', $bucket, $keys); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs) |
423
|
|
|
{ |
424
|
|
|
$data = array(); |
425
|
|
|
foreach ($key_day_pairs as $key => $day) { |
426
|
|
|
array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day); |
427
|
|
|
} |
428
|
|
|
return $data; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
public static function buildBatchChangeMime($bucket, $key_mime_pairs) |
432
|
|
|
{ |
433
|
|
|
$data = array(); |
434
|
|
|
foreach ($key_mime_pairs as $key => $mime) { |
435
|
|
|
array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime)); |
436
|
|
|
} |
437
|
|
|
return $data; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
public static function buildBatchChangeType($bucket, $key_type_pairs) |
441
|
|
|
{ |
442
|
|
|
$data = array(); |
443
|
|
|
foreach ($key_type_pairs as $key => $type) { |
444
|
|
|
array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type); |
445
|
|
|
} |
446
|
|
|
return $data; |
447
|
|
|
} |
448
|
|
|
|
449
|
3 |
|
private static function oneKeyBatch($operation, $bucket, $keys) |
450
|
|
|
{ |
451
|
3 |
|
$data = array(); |
452
|
3 |
|
foreach ($keys as $key) { |
453
|
3 |
|
array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key)); |
454
|
3 |
|
} |
455
|
3 |
|
return $data; |
456
|
|
|
} |
457
|
|
|
|
458
|
9 |
|
private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force) |
459
|
|
|
{ |
460
|
9 |
|
if ($target_bucket === null) { |
461
|
|
|
$target_bucket = $source_bucket; |
462
|
|
|
} |
463
|
9 |
|
$data = array(); |
464
|
9 |
|
$forceOp = "false"; |
465
|
9 |
|
if ($force) { |
466
|
9 |
|
$forceOp = "true"; |
467
|
9 |
|
} |
468
|
9 |
|
foreach ($key_pairs as $from_key => $to_key) { |
469
|
9 |
|
$from = \Qiniu\entry($source_bucket, $from_key); |
470
|
9 |
|
$to = \Qiniu\entry($target_bucket, $to_key); |
471
|
9 |
|
array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp); |
472
|
9 |
|
} |
473
|
9 |
|
return $data; |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.