1
|
|
|
<?php |
2
|
|
|
namespace Qiniu\Storage; |
3
|
|
|
|
4
|
|
|
use phpDocumentor\Reflection\Types\Integer; |
5
|
|
|
use phpDocumentor\Reflection\Types\Mixed; |
6
|
|
|
use Qiniu\Auth; |
7
|
|
|
use Qiniu\Config; |
8
|
|
|
use Qiniu\Zone; |
9
|
|
|
use Qiniu\Http\Client; |
10
|
|
|
use Qiniu\Http\Error; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考 |
14
|
|
|
* |
15
|
|
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/ |
16
|
|
|
*/ |
17
|
|
|
final class BucketManager |
18
|
|
|
{ |
19
|
|
|
private $auth; |
20
|
39 |
|
private $zone; |
21
|
|
|
|
22
|
39 |
|
public function __construct(Auth $auth, Zone $zone = null) |
23
|
39 |
|
{ |
24
|
39 |
|
$this->auth = $auth; |
25
|
26 |
|
if ($zone === null) { |
26
|
39 |
|
$this->zone = new Zone(); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* 获取指定账号下所有的空间名。 |
32
|
|
|
* |
33
|
3 |
|
* @return string[] 包含所有空间名 |
34
|
|
|
*/ |
35
|
3 |
|
public function buckets() |
36
|
|
|
{ |
37
|
|
|
return $this->rsGet('/buckets'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 列取空间的文件列表 |
42
|
|
|
* |
43
|
|
|
* @param $bucket 空间名 |
44
|
|
|
* @param $prefix 列举前缀 |
45
|
|
|
* @param $marker 列举标识符 |
46
|
|
|
* @param $limit 单次列举个数限制 |
47
|
|
|
* @param $delimiter 指定目录分隔符 |
48
|
|
|
* |
49
|
|
|
* @return array 包含文件信息的数组,类似:[ |
50
|
|
|
* { |
51
|
|
|
* "hash" => "<Hash string>", |
52
|
|
|
* "key" => "<Key string>", |
53
|
|
|
* "fsize" => "<file size>", |
54
|
|
|
* "putTime" => "<file modify time>" |
55
|
|
|
* }, |
56
|
|
|
* ... |
57
|
|
|
* ] |
58
|
3 |
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html |
59
|
|
|
*/ |
60
|
3 |
|
public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null) |
61
|
3 |
|
{ |
62
|
3 |
|
$query = array('bucket' => $bucket); |
63
|
3 |
|
\Qiniu\setWithoutEmpty($query, 'prefix', $prefix); |
64
|
3 |
|
\Qiniu\setWithoutEmpty($query, 'marker', $marker); |
65
|
3 |
|
\Qiniu\setWithoutEmpty($query, 'limit', $limit); |
66
|
3 |
|
\Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter); |
67
|
3 |
|
$url = Config::RSF_HOST . '/list?' . http_build_query($query); |
68
|
|
|
list($ret, $error) = $this->get($url); |
69
|
|
|
if ($ret === null) { |
70
|
3 |
|
return array(null, null, $error); |
71
|
3 |
|
} |
72
|
|
|
$marker = array_key_exists('marker', $ret) ? $ret['marker'] : null; |
73
|
|
|
return array($ret['items'], $marker, null); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* 获取资源的元信息,但不返回文件内容 |
78
|
|
|
* |
79
|
|
|
* @param $bucket 待获取信息资源所在的空间 |
80
|
|
|
* @param $key 待获取资源的文件名 |
81
|
|
|
* |
82
|
|
|
* @return array 包含文件信息的数组,类似: |
83
|
|
|
* [ |
84
|
|
|
* "hash" => "<Hash string>", |
85
|
|
|
* "key" => "<Key string>", |
86
|
|
|
* "fsize" => "<file size>", |
87
|
|
|
* "putTime" => "<file modify time>" |
88
|
|
|
* ] |
89
|
|
|
* |
90
|
6 |
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html |
91
|
|
|
*/ |
92
|
6 |
|
public function stat($bucket, $key) |
93
|
6 |
|
{ |
94
|
|
|
$path = '/stat/' . \Qiniu\entry($bucket, $key); |
95
|
|
|
return $this->rsGet($path); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* 删除指定资源 |
100
|
|
|
* |
101
|
|
|
* @param $bucket 待删除资源所在的空间 |
102
|
|
|
* @param $key 待删除资源的文件名 |
103
|
|
|
* |
104
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
105
|
15 |
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html |
106
|
|
|
*/ |
107
|
15 |
|
public function delete($bucket, $key) |
108
|
15 |
|
{ |
109
|
15 |
|
$path = '/delete/' . \Qiniu\entry($bucket, $key); |
110
|
|
|
list(, $error) = $this->rsPost($path); |
111
|
|
|
return $error; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* 给资源进行重命名,本质为move操作。 |
117
|
|
|
* |
118
|
|
|
* @param $bucket 待操作资源所在空间 |
119
|
|
|
* @param $oldname 待操作资源文件名 |
120
|
|
|
* @param $newname 目标资源文件名 |
121
|
|
|
* |
122
|
3 |
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
123
|
|
|
*/ |
124
|
3 |
|
public function rename($bucket, $oldname, $newname) |
125
|
|
|
{ |
126
|
|
|
return $this->move($bucket, $oldname, $bucket, $newname); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* 给资源进行重命名,本质为move操作。 |
131
|
|
|
* |
132
|
|
|
* @param $from_bucket 待操作资源所在空间 |
133
|
|
|
* @param $from_key 待操作资源文件名 |
134
|
|
|
* @param $to_bucket 目标资源空间名 |
135
|
|
|
* @param $to_key 目标资源文件名 |
136
|
|
|
* |
137
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
138
|
12 |
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html |
139
|
|
|
*/ |
140
|
12 |
|
public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
141
|
12 |
|
{ |
142
|
12 |
|
$from = \Qiniu\entry($from_bucket, $from_key); |
143
|
12 |
|
$to = \Qiniu\entry($to_bucket, $to_key); |
144
|
3 |
|
$path = '/copy/' . $from . '/' . $to; |
145
|
2 |
|
if ($force) { |
146
|
12 |
|
$path .= '/force/true'; |
147
|
12 |
|
} |
148
|
|
|
list(, $error) = $this->rsPost($path); |
149
|
|
|
return $error; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* 将资源从一个空间到另一个空间 |
154
|
|
|
* |
155
|
|
|
* @param $from_bucket 待操作资源所在空间 |
156
|
|
|
* @param $from_key 待操作资源文件名 |
157
|
|
|
* @param $to_bucket 目标资源空间名 |
158
|
|
|
* @param $to_key 目标资源文件名 |
159
|
|
|
* |
160
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
161
|
3 |
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html |
162
|
|
|
*/ |
163
|
3 |
|
public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
164
|
3 |
|
{ |
165
|
3 |
|
$from = \Qiniu\entry($from_bucket, $from_key); |
166
|
3 |
|
$to = \Qiniu\entry($to_bucket, $to_key); |
167
|
|
|
$path = '/move/' . $from . '/' . $to; |
168
|
|
|
if ($force) { |
169
|
3 |
|
$path .= '/force/true'; |
170
|
3 |
|
} |
171
|
|
|
list(, $error) = $this->rsPost($path); |
172
|
|
|
return $error; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* 主动修改指定资源的文件类型 |
177
|
|
|
* |
178
|
|
|
* @param $bucket 待操作资源所在空间 |
179
|
|
|
* @param $key 待操作资源文件名 |
180
|
|
|
* @param $mime 待操作文件目标mimeType |
181
|
|
|
* |
182
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
183
|
3 |
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html |
184
|
|
|
*/ |
185
|
3 |
|
public function changeMime($bucket, $key, $mime) |
186
|
3 |
|
{ |
187
|
3 |
|
$resource = \Qiniu\entry($bucket, $key); |
188
|
3 |
|
$encode_mime = \Qiniu\base64_urlSafeEncode($mime); |
189
|
3 |
|
$path = '/chgm/' . $resource . '/mime/' .$encode_mime; |
190
|
|
|
list(, $error) = $this->rsPost($path); |
191
|
|
|
return $error; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* 从指定URL抓取资源,并将该资源存储到指定空间中 |
196
|
|
|
* |
197
|
|
|
* @param $url 指定的URL |
198
|
|
|
* @param $bucket 目标资源空间 |
199
|
|
|
* @param $key 目标资源文件名 |
200
|
|
|
* |
201
|
|
|
* @return array 包含已拉取的文件信息。 |
202
|
|
|
* 成功时: [ |
203
|
|
|
* [ |
204
|
|
|
* "hash" => "<Hash string>", |
205
|
|
|
* "key" => "<Key string>" |
206
|
|
|
* ], |
207
|
|
|
* null |
208
|
|
|
* ] |
209
|
|
|
* |
210
|
|
|
* 失败时: [ |
211
|
|
|
* null, |
212
|
|
|
* Qiniu/Http/Error |
213
|
|
|
* ] |
214
|
3 |
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html |
215
|
|
|
*/ |
216
|
|
|
public function fetch($url, $bucket, $key = null) |
217
|
3 |
|
{ |
218
|
3 |
|
|
219
|
3 |
|
$resource = \Qiniu\base64_urlSafeEncode($url); |
220
|
|
|
$to = \Qiniu\entry($bucket, $key); |
221
|
3 |
|
$path = '/fetch/' . $resource . '/to/' . $to; |
222
|
3 |
|
|
223
|
|
|
$ak = $this->auth->getAccessKey(); |
224
|
3 |
|
$ioHost = $this->zone->getIoHost($ak, $bucket); |
225
|
3 |
|
|
226
|
|
|
$url = $ioHost . $path; |
227
|
|
|
return $this->post($url, null); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源 |
232
|
|
|
* |
233
|
|
|
* @param $bucket 待获取资源所在的空间 |
234
|
|
|
* @param $key 代获取资源文件名 |
235
|
|
|
* |
236
|
|
|
* @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
237
|
3 |
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html |
238
|
|
|
*/ |
239
|
3 |
|
public function prefetch($bucket, $key) |
240
|
3 |
|
{ |
241
|
|
|
$resource = \Qiniu\entry($bucket, $key); |
242
|
3 |
|
$path = '/prefetch/' . $resource; |
243
|
3 |
|
|
244
|
|
|
$ak = $this->auth->getAccessKey(); |
245
|
3 |
|
$ioHost = $this->zone->getIoHost($ak, $bucket); |
246
|
3 |
|
|
247
|
3 |
|
$url = $ioHost . $path; |
248
|
|
|
list(, $error) = $this->post($url, null); |
249
|
|
|
return $error; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* 在单次请求中进行多个资源管理操作 |
254
|
|
|
* |
255
|
|
|
* @param $operations 资源管理操作数组 |
256
|
|
|
* |
257
|
|
|
* @return array 每个资源的处理情况,结果类似: |
258
|
|
|
* [ |
259
|
|
|
* { "code" => <HttpCode int>, "data" => <Data> }, |
260
|
|
|
* { "code" => <HttpCode int> }, |
261
|
|
|
* { "code" => <HttpCode int> }, |
262
|
|
|
* { "code" => <HttpCode int> }, |
263
|
|
|
* { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } }, |
264
|
|
|
* ... |
265
|
|
|
* ] |
266
|
12 |
|
* @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html |
267
|
|
|
*/ |
268
|
12 |
|
public function batch($operations) |
269
|
12 |
|
{ |
270
|
|
|
$params = 'op=' . implode('&op=', $operations); |
271
|
|
|
return $this->rsPost('/batch', $params); |
272
|
24 |
|
} |
273
|
|
|
|
274
|
24 |
|
/** |
275
|
24 |
|
* 设置文件的生命周期 |
276
|
|
|
* |
277
|
|
|
* @param $bucket 设置文件生命周期文件所在的空间 |
278
|
9 |
|
* @param $key 设置文件生命周期文件的文件名 |
279
|
|
|
* @param $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期 |
280
|
9 |
|
* |
281
|
9 |
|
* @return Mixed |
282
|
|
|
* @link https://developer.qiniu.com/kodo/api/update-file-lifecycle |
283
|
|
|
*/ |
284
|
|
|
public function deleteAfterDays($bucket, $key, $days) |
285
|
|
|
{ |
286
|
|
|
$entry = \Qiniu\entry($bucket, $key); |
287
|
|
|
$url = "/deleteAfterDays/$entry/$days"; |
288
|
|
|
list(, $error) = $this->rsPost($url); |
289
|
|
|
return $error; |
290
|
12 |
|
} |
291
|
|
|
|
292
|
12 |
|
private function rsPost($path, $body = null) |
293
|
12 |
|
{ |
294
|
12 |
|
$url = Config::RS_HOST . $path; |
295
|
6 |
|
return $this->post($url, $body); |
296
|
|
|
} |
297
|
12 |
|
|
298
|
|
|
private function rsGet($path) |
299
|
|
|
{ |
300
|
30 |
|
$url = Config::RS_HOST . $path; |
301
|
|
|
return $this->get($url); |
302
|
30 |
|
} |
303
|
30 |
|
|
304
|
30 |
|
private function ioPost($path, $body = null) |
|
|
|
|
305
|
6 |
|
{ |
306
|
|
|
$url = Config::IO_HOST . $path; |
307
|
27 |
|
return $this->post($url, $body); |
308
|
27 |
|
} |
309
|
|
|
|
310
|
|
|
private function get($url) |
311
|
3 |
|
{ |
312
|
|
|
$headers = $this->auth->authorization($url); |
313
|
3 |
|
$ret = Client::get($url, $headers); |
314
|
|
|
if (!$ret->ok()) { |
315
|
|
|
return array(null, new Error($url, $ret)); |
316
|
|
|
} |
317
|
3 |
|
return array($ret->json(), null); |
318
|
|
|
} |
319
|
3 |
|
|
320
|
|
|
private function post($url, $body) |
321
|
|
|
{ |
322
|
|
|
$headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded'); |
323
|
6 |
|
$ret = Client::post($url, $body, $headers); |
324
|
|
|
if (!$ret->ok()) { |
325
|
6 |
|
return array(null, new Error($url, $ret)); |
326
|
|
|
} |
327
|
|
|
$r = ($ret->body === null) ? array() : $ret->json(); |
328
|
|
|
return array($r, null); |
329
|
3 |
|
} |
330
|
|
|
|
331
|
3 |
|
public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket) |
332
|
|
|
{ |
333
|
|
|
return self::twoKeyBatch('copy', $source_bucket, $key_pairs, $target_bucket); |
334
|
|
|
} |
335
|
3 |
|
|
336
|
|
|
|
337
|
3 |
|
public static function buildBatchRename($bucket, $key_pairs) |
338
|
|
|
{ |
339
|
|
|
return self::buildBatchMove($bucket, $key_pairs, $bucket); |
340
|
6 |
|
} |
341
|
|
|
|
342
|
6 |
|
|
343
|
6 |
|
public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket) |
344
|
6 |
|
{ |
345
|
4 |
|
return self::twoKeyBatch('move', $source_bucket, $key_pairs, $target_bucket); |
346
|
6 |
|
} |
347
|
|
|
|
348
|
|
|
|
349
|
9 |
|
public static function buildBatchDelete($bucket, $keys) |
350
|
|
|
{ |
351
|
9 |
|
return self::oneKeyBatch('delete', $bucket, $keys); |
352
|
|
|
} |
353
|
|
|
|
354
|
9 |
|
|
355
|
9 |
|
public static function buildBatchStat($bucket, $keys) |
356
|
9 |
|
{ |
357
|
9 |
|
return self::oneKeyBatch('stat', $bucket, $keys); |
358
|
9 |
|
} |
359
|
6 |
|
|
360
|
9 |
|
private static function oneKeyBatch($operation, $bucket, $keys) |
361
|
|
|
{ |
362
|
|
|
$data = array(); |
363
|
|
|
foreach ($keys as $key) { |
364
|
|
|
array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key)); |
365
|
|
|
} |
366
|
|
|
return $data; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket) |
370
|
|
|
{ |
371
|
|
|
if ($target_bucket === null) { |
372
|
|
|
$target_bucket = $source_bucket; |
373
|
|
|
} |
374
|
|
|
$data = array(); |
375
|
|
|
foreach ($key_pairs as $from_key => $to_key) { |
376
|
|
|
$from = \Qiniu\entry($source_bucket, $from_key); |
377
|
|
|
$to = \Qiniu\entry($target_bucket, $to_key); |
378
|
|
|
array_push($data, $operation . '/' . $from . '/' . $to); |
379
|
|
|
} |
380
|
|
|
return $data; |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|