1 | <?php |
||
15 | final class BucketManager |
||
16 | { |
||
17 | private $auth; |
||
18 | private $zone; |
||
19 | |||
20 | 42 | public function __construct(Auth $auth, Zone $zone = null) |
|
21 | { |
||
22 | 42 | $this->auth = $auth; |
|
23 | 42 | if ($zone === null) { |
|
24 | 42 | $this->zone = new Zone(); |
|
25 | 28 | } |
|
26 | 42 | } |
|
27 | |||
28 | /** |
||
29 | * 获取指定账号下所有的空间名。 |
||
30 | * |
||
31 | * @return string[] 包含所有空间名 |
||
32 | */ |
||
33 | 3 | public function buckets() |
|
37 | |||
38 | /** |
||
39 | * 列取空间的文件列表 |
||
40 | * |
||
41 | * @param $bucket 空间名 |
||
42 | * @param $prefix 列举前缀 |
||
43 | * @param $marker 列举标识符 |
||
44 | * @param $limit 单次列举个数限制 |
||
45 | * @param $delimiter 指定目录分隔符 |
||
46 | * |
||
47 | * @return array 包含文件信息的数组,类似:[ |
||
48 | * { |
||
49 | * "hash" => "<Hash string>", |
||
50 | * "key" => "<Key string>", |
||
51 | * "fsize" => "<file size>", |
||
52 | * "putTime" => "<file modify time>" |
||
53 | * }, |
||
54 | * ... |
||
55 | * ] |
||
56 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html |
||
57 | */ |
||
58 | 3 | public function listFiles($bucket, $prefix = null, $marker = null, $limit = 1000, $delimiter = null) |
|
59 | { |
||
60 | 3 | $query = array('bucket' => $bucket); |
|
61 | 3 | \Qiniu\setWithoutEmpty($query, 'prefix', $prefix); |
|
62 | 3 | \Qiniu\setWithoutEmpty($query, 'marker', $marker); |
|
63 | 3 | \Qiniu\setWithoutEmpty($query, 'limit', $limit); |
|
64 | 3 | \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter); |
|
65 | 3 | $url = Config::RSF_HOST . '/list?' . http_build_query($query); |
|
66 | 3 | list($ret, $error) = $this->get($url); |
|
67 | 3 | if ($ret === null) { |
|
68 | return array(null, null, $error); |
||
69 | } |
||
70 | 3 | $marker = array_key_exists('marker', $ret) ? $ret['marker'] : null; |
|
71 | 3 | return array($ret['items'], $marker, null); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * 获取资源的元信息,但不返回文件内容 |
||
76 | * |
||
77 | * @param $bucket 待获取信息资源所在的空间 |
||
78 | * @param $key 待获取资源的文件名 |
||
79 | * |
||
80 | * @return array 包含文件信息的数组,类似: |
||
81 | * [ |
||
82 | * "hash" => "<Hash string>", |
||
83 | * "key" => "<Key string>", |
||
84 | * "fsize" => "<file size>", |
||
85 | * "putTime" => "<file modify time>" |
||
86 | * ] |
||
87 | * |
||
88 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/stat.html |
||
89 | */ |
||
90 | 6 | public function stat($bucket, $key) |
|
95 | |||
96 | /** |
||
97 | * 删除指定资源 |
||
98 | * |
||
99 | * @param $bucket 待删除资源所在的空间 |
||
100 | * @param $key 待删除资源的文件名 |
||
101 | * |
||
102 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
103 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/delete.html |
||
104 | */ |
||
105 | 15 | public function delete($bucket, $key) |
|
111 | |||
112 | |||
113 | /** |
||
114 | * 给资源进行重命名,本质为move操作。 |
||
115 | * |
||
116 | * @param $bucket 待操作资源所在空间 |
||
117 | * @param $oldname 待操作资源文件名 |
||
118 | * @param $newname 目标资源文件名 |
||
119 | * |
||
120 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
121 | */ |
||
122 | 3 | public function rename($bucket, $oldname, $newname) |
|
126 | |||
127 | /** |
||
128 | * 给资源进行重命名,本质为move操作。 |
||
129 | * |
||
130 | * @param $from_bucket 待操作资源所在空间 |
||
131 | * @param $from_key 待操作资源文件名 |
||
132 | * @param $to_bucket 目标资源空间名 |
||
133 | * @param $to_key 目标资源文件名 |
||
134 | * |
||
135 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
136 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/copy.html |
||
137 | */ |
||
138 | 15 | public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
|
149 | |||
150 | /** |
||
151 | * 将资源从一个空间到另一个空间 |
||
152 | * |
||
153 | * @param $from_bucket 待操作资源所在空间 |
||
154 | * @param $from_key 待操作资源文件名 |
||
155 | * @param $to_bucket 目标资源空间名 |
||
156 | * @param $to_key 目标资源文件名 |
||
157 | * |
||
158 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
159 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html |
||
160 | */ |
||
161 | 3 | public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false) |
|
172 | |||
173 | /** |
||
174 | * 主动修改指定资源的文件类型 |
||
175 | * |
||
176 | * @param $bucket 待操作资源所在空间 |
||
177 | * @param $key 待操作资源文件名 |
||
178 | * @param $mime 待操作文件目标mimeType |
||
179 | * |
||
180 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
181 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html |
||
182 | */ |
||
183 | 3 | public function changeMime($bucket, $key, $mime) |
|
191 | |||
192 | /** |
||
193 | * 从指定URL抓取资源,并将该资源存储到指定空间中 |
||
194 | * |
||
195 | * @param $url 指定的URL |
||
196 | * @param $bucket 目标资源空间 |
||
197 | * @param $key 目标资源文件名 |
||
198 | * |
||
199 | * @return array 包含已拉取的文件信息。 |
||
200 | * 成功时: [ |
||
201 | * [ |
||
202 | * "hash" => "<Hash string>", |
||
203 | * "key" => "<Key string>" |
||
204 | * ], |
||
205 | * null |
||
206 | * ] |
||
207 | * |
||
208 | * 失败时: [ |
||
209 | * null, |
||
210 | * Qiniu/Http/Error |
||
211 | * ] |
||
212 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html |
||
213 | */ |
||
214 | 3 | public function fetch($url, $bucket, $key = null) |
|
227 | |||
228 | /** |
||
229 | * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源 |
||
230 | * |
||
231 | * @param $bucket 待获取资源所在的空间 |
||
232 | * @param $key 代获取资源文件名 |
||
233 | * |
||
234 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
235 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html |
||
236 | */ |
||
237 | 3 | public function prefetch($bucket, $key) |
|
249 | |||
250 | /** |
||
251 | * 在单次请求中进行多个资源管理操作 |
||
252 | * |
||
253 | * @param $operations 资源管理操作数组 |
||
254 | * |
||
255 | * @return array 每个资源的处理情况,结果类似: |
||
256 | * [ |
||
257 | * { "code" => <HttpCode int>, "data" => <Data> }, |
||
258 | * { "code" => <HttpCode int> }, |
||
259 | * { "code" => <HttpCode int> }, |
||
260 | * { "code" => <HttpCode int> }, |
||
261 | * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } }, |
||
262 | * ... |
||
263 | * ] |
||
264 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html |
||
265 | */ |
||
266 | 12 | public function batch($operations) |
|
271 | |||
272 | /** |
||
273 | * 设置文件的生命周期 |
||
274 | * |
||
275 | * @param $bucket 设置文件生命周期文件所在的空间 |
||
276 | * @param $key 设置文件生命周期文件的文件名 |
||
277 | * @param $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期 |
||
278 | * |
||
279 | * @return Mixed |
||
280 | * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle |
||
281 | */ |
||
282 | 3 | public function deleteAfterDays($bucket, $key, $days) |
|
283 | { |
||
284 | 3 | $entry = \Qiniu\entry($bucket, $key); |
|
285 | 3 | $url = "/deleteAfterDays/$entry/$days"; |
|
286 | 3 | list(, $error) = $this->rsPost($url); |
|
287 | 3 | return $error; |
|
288 | } |
||
289 | |||
290 | 27 | private function rsPost($path, $body = null) |
|
291 | { |
||
292 | 27 | $url = Config::RS_HOST . $path; |
|
293 | 27 | return $this->post($url, $body); |
|
294 | } |
||
295 | |||
296 | 9 | private function rsGet($path) |
|
297 | { |
||
298 | 9 | $url = Config::RS_HOST . $path; |
|
299 | 9 | return $this->get($url); |
|
300 | } |
||
301 | |||
302 | private function ioPost($path, $body = null) |
||
|
|||
303 | { |
||
304 | $url = Config::IO_HOST . $path; |
||
305 | return $this->post($url, $body); |
||
306 | } |
||
307 | |||
308 | 12 | private function get($url) |
|
309 | { |
||
310 | 12 | $headers = $this->auth->authorization($url); |
|
311 | 12 | $ret = Client::get($url, $headers); |
|
312 | 12 | if (!$ret->ok()) { |
|
313 | 6 | return array(null, new Error($url, $ret)); |
|
314 | } |
||
315 | 12 | return array($ret->json(), null); |
|
316 | } |
||
317 | |||
318 | 33 | private function post($url, $body) |
|
319 | { |
||
320 | 33 | $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded'); |
|
321 | 33 | $ret = Client::post($url, $body, $headers); |
|
322 | 33 | if (!$ret->ok()) { |
|
323 | 9 | return array(null, new Error($url, $ret)); |
|
324 | } |
||
325 | 30 | $r = ($ret->body === null) ? array() : $ret->json(); |
|
326 | 30 | return array($r, null); |
|
327 | } |
||
328 | |||
329 | 3 | public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket) |
|
333 | |||
334 | |||
335 | 3 | public static function buildBatchRename($bucket, $key_pairs) |
|
339 | |||
340 | |||
341 | 6 | public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket) |
|
342 | { |
||
343 | 6 | return self::twoKeyBatch('move', $source_bucket, $key_pairs, $target_bucket); |
|
344 | } |
||
345 | |||
346 | |||
347 | 3 | public static function buildBatchDelete($bucket, $keys) |
|
351 | |||
352 | |||
353 | 3 | public static function buildBatchStat($bucket, $keys) |
|
357 | |||
358 | 6 | private static function oneKeyBatch($operation, $bucket, $keys) |
|
366 | |||
367 | 9 | private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket) |
|
380 | } |
||
381 |