1 | <?php |
||
15 | final class BucketManager |
||
16 | { |
||
17 | private $auth; |
||
18 | private $zone; |
||
19 | |||
20 | 39 | public function __construct(Auth $auth, Zone $zone = null) |
|
21 | { |
||
22 | 39 | $this->auth = $auth; |
|
23 | 39 | if ($zone === null) { |
|
24 | 39 | $this->zone = new Zone(); |
|
25 | 26 | } |
|
26 | 39 | } |
|
27 | |||
28 | /** |
||
29 | * 获取指定账号下所有的空间名。 |
||
30 | * |
||
31 | * @return string[] 包含所有空间名 |
||
32 | */ |
||
33 | 3 | public function buckets() |
|
34 | { |
||
35 | 3 | return $this->rsGet('/buckets'); |
|
36 | } |
||
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 | 3 | public function stat($bucket, $key) |
|
91 | { |
||
92 | 3 | $path = '/stat/' . \Qiniu\entry($bucket, $key); |
|
93 | 3 | return $this->rsGet($path); |
|
94 | } |
||
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) |
|
106 | { |
||
107 | 15 | $path = '/delete/' . \Qiniu\entry($bucket, $key); |
|
108 | 15 | list(, $error) = $this->rsPost($path); |
|
109 | 15 | return $error; |
|
110 | } |
||
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) |
|
123 | { |
||
124 | 3 | return $this->move($bucket, $oldname, $bucket, $newname); |
|
125 | } |
||
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 | 12 | public function copy($from_bucket, $from_key, $to_bucket, $to_key) |
|
139 | { |
||
140 | 12 | $from = \Qiniu\entry($from_bucket, $from_key); |
|
141 | 12 | $to = \Qiniu\entry($to_bucket, $to_key); |
|
142 | 12 | $path = '/copy/' . $from . '/' . $to; |
|
143 | 12 | list(, $error) = $this->rsPost($path); |
|
144 | 12 | return $error; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * 将资源从一个空间到另一个空间 |
||
149 | * |
||
150 | * @param $from_bucket 待操作资源所在空间 |
||
151 | * @param $from_key 待操作资源文件名 |
||
152 | * @param $to_bucket 目标资源空间名 |
||
153 | * @param $to_key 目标资源文件名 |
||
154 | * |
||
155 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
156 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/move.html |
||
157 | */ |
||
158 | 3 | public function move($from_bucket, $from_key, $to_bucket, $to_key) |
|
159 | { |
||
160 | 3 | $from = \Qiniu\entry($from_bucket, $from_key); |
|
161 | 3 | $to = \Qiniu\entry($to_bucket, $to_key); |
|
162 | 3 | $path = '/move/' . $from . '/' . $to; |
|
163 | 3 | list(, $error) = $this->rsPost($path); |
|
164 | 3 | return $error; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * 主动修改指定资源的文件类型 |
||
169 | * |
||
170 | * @param $bucket 待操作资源所在空间 |
||
171 | * @param $key 待操作资源文件名 |
||
172 | * @param $mime 待操作文件目标mimeType |
||
173 | * |
||
174 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
175 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/chgm.html |
||
176 | */ |
||
177 | 3 | public function changeMime($bucket, $key, $mime) |
|
178 | { |
||
179 | 3 | $resource = \Qiniu\entry($bucket, $key); |
|
180 | 3 | $encode_mime = \Qiniu\base64_urlSafeEncode($mime); |
|
181 | 3 | $path = '/chgm/' . $resource . '/mime/' .$encode_mime; |
|
182 | 3 | list(, $error) = $this->rsPost($path); |
|
183 | 3 | return $error; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * 从指定URL抓取资源,并将该资源存储到指定空间中 |
||
188 | * |
||
189 | * @param $url 指定的URL |
||
190 | * @param $bucket 目标资源空间 |
||
191 | * @param $key 目标资源文件名 |
||
192 | * |
||
193 | * @return array 包含已拉取的文件信息。 |
||
194 | * 成功时: [ |
||
195 | * [ |
||
196 | * "hash" => "<Hash string>", |
||
197 | * "key" => "<Key string>" |
||
198 | * ], |
||
199 | * null |
||
200 | * ] |
||
201 | * |
||
202 | * 失败时: [ |
||
203 | * null, |
||
204 | * Qiniu/Http/Error |
||
205 | * ] |
||
206 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/fetch.html |
||
207 | */ |
||
208 | 3 | public function fetch($url, $bucket, $key = null) |
|
209 | { |
||
210 | |||
211 | 3 | $resource = \Qiniu\base64_urlSafeEncode($url); |
|
212 | 3 | $to = \Qiniu\entry($bucket, $key); |
|
213 | 3 | $path = '/fetch/' . $resource . '/to/' . $to; |
|
214 | |||
215 | 3 | $ak = $this->auth->getAccessKey(); |
|
216 | 3 | $ioHost = $this->zone->getIoHost($ak, $bucket); |
|
217 | |||
218 | 3 | $url = $ioHost . $path; |
|
219 | 3 | return $this->post($url, null); |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源 |
||
224 | * |
||
225 | * @param $bucket 待获取资源所在的空间 |
||
226 | * @param $key 代获取资源文件名 |
||
227 | * |
||
228 | * @return mixed 成功返回NULL,失败返回对象Qiniu\Http\Error |
||
229 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/prefetch.html |
||
230 | */ |
||
231 | 3 | public function prefetch($bucket, $key) |
|
232 | { |
||
233 | 3 | $resource = \Qiniu\entry($bucket, $key); |
|
234 | 3 | $path = '/prefetch/' . $resource; |
|
235 | |||
236 | 3 | $ak = $this->auth->getAccessKey(); |
|
237 | 3 | $ioHost = $this->zone->getIoHost($ak, $bucket); |
|
238 | |||
239 | 3 | $url = $ioHost . $path; |
|
240 | 3 | list(, $error) = $this->post($url, null); |
|
241 | 3 | return $error; |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * 在单次请求中进行多个资源管理操作 |
||
246 | * |
||
247 | * @param $operations 资源管理操作数组 |
||
248 | * |
||
249 | * @return array 每个资源的处理情况,结果类似: |
||
250 | * [ |
||
251 | * { "code" => <HttpCode int>, "data" => <Data> }, |
||
252 | * { "code" => <HttpCode int> }, |
||
253 | * { "code" => <HttpCode int> }, |
||
254 | * { "code" => <HttpCode int> }, |
||
255 | * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } }, |
||
256 | * ... |
||
257 | * ] |
||
258 | * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html |
||
259 | */ |
||
260 | 12 | public function batch($operations) |
|
261 | { |
||
262 | 12 | $params = 'op=' . implode('&op=', $operations); |
|
263 | 12 | return $this->rsPost('/batch', $params); |
|
264 | } |
||
265 | |||
266 | 24 | private function rsPost($path, $body = null) |
|
267 | { |
||
268 | 24 | $url = Config::RS_HOST . $path; |
|
269 | 24 | return $this->post($url, $body); |
|
270 | } |
||
271 | |||
272 | 6 | private function rsGet($path) |
|
273 | { |
||
274 | 6 | $url = Config::RS_HOST . $path; |
|
275 | 6 | return $this->get($url); |
|
276 | } |
||
277 | |||
278 | private function ioPost($path, $body = null) |
||
|
|||
279 | { |
||
280 | $url = Config::IO_HOST . $path; |
||
281 | return $this->post($url, $body); |
||
282 | } |
||
283 | |||
284 | 9 | private function get($url) |
|
285 | { |
||
286 | 9 | $headers = $this->auth->authorization($url); |
|
287 | 9 | $ret = Client::get($url, $headers); |
|
288 | 9 | if (!$ret->ok()) { |
|
289 | 6 | return array(null, new Error($url, $ret)); |
|
290 | } |
||
291 | 9 | return array($ret->json(), null); |
|
292 | } |
||
293 | |||
294 | 30 | private function post($url, $body) |
|
295 | { |
||
296 | 30 | $headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded'); |
|
297 | 30 | $ret = Client::post($url, $body, $headers); |
|
298 | 30 | if (!$ret->ok()) { |
|
299 | 3 | return array(null, new Error($url, $ret)); |
|
300 | } |
||
301 | 27 | $r = ($ret->body === null) ? array() : $ret->json(); |
|
302 | 27 | return array($r, null); |
|
303 | } |
||
304 | |||
305 | 3 | public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket) |
|
306 | { |
||
307 | 3 | return self::twoKeyBatch('copy', $source_bucket, $key_pairs, $target_bucket); |
|
308 | } |
||
309 | |||
310 | |||
311 | 3 | public static function buildBatchRename($bucket, $key_pairs) |
|
312 | { |
||
313 | 3 | return self::buildBatchMove($bucket, $key_pairs, $bucket); |
|
314 | } |
||
315 | |||
316 | |||
317 | 6 | public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket) |
|
318 | { |
||
319 | 6 | return self::twoKeyBatch('move', $source_bucket, $key_pairs, $target_bucket); |
|
320 | } |
||
321 | |||
322 | |||
323 | 3 | public static function buildBatchDelete($bucket, $keys) |
|
324 | { |
||
325 | 3 | return self::oneKeyBatch('delete', $bucket, $keys); |
|
326 | } |
||
327 | |||
328 | |||
329 | 3 | public static function buildBatchStat($bucket, $keys) |
|
330 | { |
||
331 | 3 | return self::oneKeyBatch('stat', $bucket, $keys); |
|
332 | } |
||
333 | |||
334 | 6 | private static function oneKeyBatch($operation, $bucket, $keys) |
|
342 | |||
343 | 9 | private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket) |
|
344 | { |
||
345 | 9 | if ($target_bucket === null) { |
|
346 | $target_bucket = $source_bucket; |
||
347 | } |
||
348 | 9 | $data = array(); |
|
349 | 9 | foreach ($key_pairs as $from_key => $to_key) { |
|
350 | 9 | $from = \Qiniu\entry($source_bucket, $from_key); |
|
351 | 9 | $to = \Qiniu\entry($target_bucket, $to_key); |
|
352 | 9 | array_push($data, $operation . '/' . $from . '/' . $to); |
|
353 | 6 | } |
|
354 | 9 | return $data; |
|
355 | } |
||
356 | } |
||
357 |