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