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() |
|
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 | 12 | 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 | 24 | private function rsPost($path, $body = null) |
|
277 | |||
278 | 9 | private function rsGet($path) |
|
283 | |||
284 | private function ioPost($path, $body = null) |
||
289 | |||
290 | 12 | private function get($url) |
|
299 | |||
300 | 30 | private function post($url, $body) |
|
310 | |||
311 | 3 | public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket) |
|
315 | |||
316 | |||
317 | 3 | public static function buildBatchRename($bucket, $key_pairs) |
|
321 | |||
322 | |||
323 | 6 | public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket) |
|
327 | |||
328 | |||
329 | 3 | public static function buildBatchDelete($bucket, $keys) |
|
333 | |||
334 | |||
335 | 3 | public static function buildBatchStat($bucket, $keys) |
|
339 | |||
340 | 6 | private static function oneKeyBatch($operation, $bucket, $keys) |
|
348 | |||
349 | 9 | private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket) |
|
362 | } |
||
363 |