|
1
|
|
|
<?php |
|
2
|
|
|
//使用时请在项目中引用该php文件 |
|
3
|
|
|
//设置默认时区 |
|
4
|
|
|
date_default_timezone_set('Asia/Shanghai'); |
|
5
|
|
|
|
|
6
|
|
|
//检测API路径 |
|
7
|
|
|
if (!defined("KS3_API_PATH")) { |
|
8
|
|
|
define("KS3_API_PATH", __DIR__); |
|
9
|
|
|
} |
|
10
|
|
|
//是否使用VHOST |
|
11
|
|
|
if (!defined("KS3_API_VHOST")) { |
|
12
|
|
|
define("KS3_API_VHOST", FALSE); |
|
13
|
|
|
} |
|
14
|
|
|
//是否开启日志(写入日志文件) |
|
15
|
|
|
if (!defined("KS3_API_LOG")) { |
|
16
|
|
|
define("KS3_API_LOG", FALSE); |
|
17
|
|
|
} |
|
18
|
|
|
//是否显示日志(直接输出日志) |
|
19
|
|
|
if (!defined("KS3_API_DISPLAY_LOG")) { |
|
20
|
|
|
define("KS3_API_DISPLAY_LOG", FALSE); |
|
21
|
|
|
} |
|
22
|
|
|
//定义日志目录(默认是该项目log下) |
|
23
|
|
|
if (!defined("KS3_API_LOG_PATH")) { |
|
24
|
|
|
define("KS3_API_LOG_PATH", ""); |
|
25
|
|
|
} |
|
26
|
|
|
//是否使用HTTPS |
|
27
|
|
|
if (!defined("KS3_API_USE_HTTPS")) { |
|
28
|
|
|
define("KS3_API_USE_HTTPS", FALSE); |
|
29
|
|
|
} |
|
30
|
|
|
//是否开启curl debug模式 |
|
31
|
|
|
if (!defined("KS3_API_DEBUG_MODE")) { |
|
32
|
|
|
define("KS3_API_DEBUG_MODE", FALSE); |
|
33
|
|
|
} |
|
34
|
|
|
define("KS3_API_Author", "[email protected]"); |
|
35
|
|
|
define("KS3_API_Version", "1.2"); |
|
36
|
|
|
|
|
37
|
|
|
require_once KS3_API_PATH . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "Consts.php"; |
|
38
|
|
|
require_once KS3_API_PATH . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "API.php"; |
|
39
|
|
|
require_once KS3_API_PATH . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "Signers.php"; |
|
40
|
|
|
require_once KS3_API_PATH . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "Ks3Request.class.php"; |
|
41
|
|
|
require_once KS3_API_PATH . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "Handlers.php"; |
|
42
|
|
|
require_once KS3_API_PATH . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "Builders.php"; |
|
43
|
|
|
require_once KS3_API_PATH . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "Logger.php"; |
|
44
|
|
|
require_once KS3_API_PATH . DIRECTORY_SEPARATOR . "core" . DIRECTORY_SEPARATOR . "MessageHolder.php"; |
|
45
|
|
|
require_once KS3_API_PATH . DIRECTORY_SEPARATOR . "lib" . DIRECTORY_SEPARATOR . "RequestCore.class.php"; |
|
46
|
|
|
require_once KS3_API_PATH . DIRECTORY_SEPARATOR . "exceptions" . DIRECTORY_SEPARATOR . "Exceptions.php"; |
|
47
|
|
|
|
|
48
|
|
|
if (function_exists('get_loaded_extensions')) { |
|
49
|
|
|
//检测curl扩展 |
|
50
|
|
|
$extensions = get_loaded_extensions(); |
|
51
|
|
|
if ($extensions) { |
|
|
|
|
|
|
52
|
|
|
if (!in_array('curl', $extensions, true)) { |
|
53
|
|
|
throw new Ks3ClientException("please install curl extension"); |
|
54
|
|
|
} |
|
55
|
|
|
if (!in_array('mbstring', $extensions, true)) { |
|
56
|
|
|
throw new Ks3ClientException("please install mbstring extension"); |
|
57
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} else { |
|
60
|
|
|
throw new Ks3ClientException("please install extensions"); |
|
61
|
|
|
} |
|
62
|
|
|
} else { |
|
63
|
|
|
throw new Ks3ClientException(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
class Ks3Client |
|
67
|
|
|
{ |
|
68
|
|
|
private $accessKey; |
|
69
|
|
|
private $secretKey; |
|
70
|
|
|
private $endpoint; |
|
71
|
|
|
|
|
72
|
|
|
public function __construct($accessKey = NULL, $secretKey = NULL, $endpoint = NULL) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->accessKey = $accessKey; |
|
75
|
|
|
$this->secretKey = $secretKey; |
|
76
|
|
|
|
|
77
|
|
|
if (empty($endpoint)) { |
|
78
|
|
|
throw new Ks3ClientException("must set endpoint, please see http://ks3.ksyun.com/doc/api/index.html Region part"); |
|
79
|
|
|
} |
|
80
|
|
|
$this->endpoint = $endpoint; |
|
81
|
|
|
|
|
82
|
|
|
$this->signers = array(); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* 方法列表:(具体使用请参考readme.md) |
|
87
|
|
|
* listBuckets,罗列bucket |
|
88
|
|
|
* deleteBucket,删除bucket |
|
89
|
|
|
* deleteBucketCORS,删除bucket跨域配置 |
|
90
|
|
|
* createBucket,新建bucket |
|
91
|
|
|
* setBucketAcl,设置bucket访问权限 |
|
92
|
|
|
* setBucketCORS,设置bucket跨域配置 |
|
93
|
|
|
* setBucketLogging,设置bucket日志配置 |
|
94
|
|
|
* listObjects,罗列object |
|
95
|
|
|
* getBucketAcl,获取bucket访问权限 |
|
96
|
|
|
* getBucketCORS,获取bucket跨域配置 |
|
97
|
|
|
* getBucketLocation,获取bucket地点配置 |
|
98
|
|
|
* getBucketLogging,获取bucket日志配置 |
|
99
|
|
|
* bucketExists,判断bucket是否存在 |
|
100
|
|
|
* listMutipartUploads,罗列当前bucket下尚未结束的分块上传 |
|
101
|
|
|
* putObjectByContent,上传文件 |
|
102
|
|
|
* putObjectByFile,上传文件 |
|
103
|
|
|
* setObjectAcl,设置object访问权限 |
|
104
|
|
|
* copyObject,复制object |
|
105
|
|
|
* getObjectMeta,获取object元数据 |
|
106
|
|
|
* objectExists,判断object是否存在 |
|
107
|
|
|
* deleteObject,删除object |
|
108
|
|
|
* deleteObjects,删除多个object |
|
109
|
|
|
* getObject,下载object |
|
110
|
|
|
* getObjectAcl,获取object访问权限 |
|
111
|
|
|
* initMultipartUpload,初始化分块上传 |
|
112
|
|
|
* uploadPart,上传块 |
|
113
|
|
|
* abortMultipartUpload,终止分块上传 |
|
114
|
|
|
* listParts,罗列已经上传的块 |
|
115
|
|
|
* completeMultipartUpload,完成分块上传 |
|
116
|
|
|
* generatePresignedUrl,生成文件外链 |
|
117
|
|
|
* putAdp,添加异步数据处理任务 |
|
118
|
|
|
* getAdp,查询异步数据处理任务 |
|
119
|
|
|
* @param $method |
|
120
|
|
|
* @param array $args |
|
121
|
|
|
* @return ResponseCore|string|string[]|null |
|
122
|
|
|
* @throws Exception |
|
123
|
|
|
*/ |
|
124
|
|
|
public function __call($method, $args = array()) |
|
125
|
|
|
{ |
|
126
|
|
|
$holder = new MessageHolder(); |
|
127
|
|
|
|
|
128
|
|
|
$holder->msg = "------------------Logging Start-------------------------\r\n"; |
|
129
|
|
|
$holder->msg .= "method->" . $method . " args->" . serialize($args) . "\r\n"; |
|
130
|
|
|
$ex = NULL; |
|
131
|
|
|
try { |
|
132
|
|
|
$result = $this->invoke($method, $args, $holder); |
|
133
|
|
|
} catch (Exception $e) { |
|
134
|
|
|
$holder->msg .= $e . "\r\n"; |
|
135
|
|
|
$ex = $e; |
|
136
|
|
|
} |
|
137
|
|
|
$holder->msg .= "------------------Logging End-------------------------\r\n"; |
|
138
|
|
|
if ($ex != NULL) { |
|
139
|
|
|
throw $ex; |
|
140
|
|
|
} |
|
141
|
|
|
return $result; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
private function invoke($method, $args = array(), $holder, $location = NULL) |
|
145
|
|
|
{ |
|
146
|
|
|
$api = API::$API[$method]; |
|
147
|
|
|
if (!$api) { |
|
148
|
|
|
throw new Ks3ClientException($method . " Not Found API"); |
|
149
|
|
|
} |
|
150
|
|
|
if (count($args) !== 0) { |
|
151
|
|
|
if (count($args) > 1 || !is_array($args[0])) { |
|
152
|
|
|
throw new Ks3ClientException("this method only needs one array argument"); |
|
153
|
|
|
} |
|
154
|
|
|
$args = $args[0]; |
|
155
|
|
|
} |
|
156
|
|
|
if (isset($api["redirect"])) { |
|
157
|
|
|
$api = API::$API[$api["redirect"]]; |
|
158
|
|
|
} |
|
159
|
|
|
$request = new Ks3Request(); |
|
160
|
|
|
if (empty($args["Bucket"])) { |
|
161
|
|
|
if ($api["needBucket"]) { |
|
162
|
|
|
throw new Ks3ClientException($method . " this api need bucket"); |
|
163
|
|
|
} |
|
164
|
|
|
} else { |
|
165
|
|
|
$request->bucket = $args["Bucket"]; |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
$position = $api["objectPostion"] ?? "Key"; |
|
168
|
|
|
if (empty($args[$position])) { |
|
169
|
|
|
if ($api["needObject"]) { |
|
170
|
|
|
throw new Ks3ClientException($method . " this api need " . $position); |
|
171
|
|
|
} |
|
172
|
|
|
} else { |
|
173
|
|
|
$key = $args[$position]; |
|
174
|
|
|
$preEncoding = mb_detect_encoding($key, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5")); |
|
175
|
|
|
$holder->msg .= "key encoding " . $preEncoding . "\r\n"; |
|
176
|
|
|
if (strtolower($preEncoding) !== "utf-8") { |
|
177
|
|
|
$key = iconv($preEncoding, "UTF-8", $key); |
|
178
|
|
|
} |
|
179
|
|
|
$request->key = $key; |
|
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
$method = $api["method"]; |
|
182
|
|
|
if ($method === "Method") { |
|
183
|
|
|
if (empty($args["Method"])) { |
|
184
|
|
|
$request->method = "GET"; |
|
|
|
|
|
|
185
|
|
|
} else { |
|
186
|
|
|
$request->method = $args["Method"]; |
|
187
|
|
|
} |
|
188
|
|
|
} else { |
|
189
|
|
|
$request->method = $api["method"]; |
|
190
|
|
|
} |
|
191
|
|
|
if (KS3_API_USE_HTTPS) { |
|
192
|
|
|
$request->scheme = "https://"; |
|
|
|
|
|
|
193
|
|
|
} else { |
|
194
|
|
|
$request->scheme = "http://"; |
|
195
|
|
|
} |
|
196
|
|
|
$request->endpoint = $this->endpoint; |
|
|
|
|
|
|
197
|
|
|
//add subresource |
|
198
|
|
|
if (!empty($api["subResource"])) { |
|
199
|
|
|
$request->subResource = $api["subResource"]; |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
//add query params |
|
202
|
|
|
if (isset($api["queryParams"])) { |
|
203
|
|
|
foreach ($api["queryParams"] as $key => $value) { |
|
204
|
|
|
$required = FALSE; |
|
205
|
|
|
if (strpos($value, "!") === 0) { |
|
206
|
|
|
$required = TRUE; |
|
207
|
|
|
$value = substr($value, 1); |
|
208
|
|
|
} |
|
209
|
|
|
$index = explode("->", $value); |
|
210
|
|
|
$curIndexArg = $args; |
|
211
|
|
|
$add = TRUE; |
|
212
|
|
|
$curkey = ""; |
|
213
|
|
|
foreach ($index as $key1 => $value1) { |
|
214
|
|
|
if (!isset($curIndexArg[$value1]) && $value1 !== "*") { |
|
215
|
|
|
$add = FALSE; |
|
216
|
|
|
} else { |
|
217
|
|
|
$curkey = $value1; |
|
218
|
|
|
//星号表示所有,按照暂时的业务,默认星号后面就没了 |
|
219
|
|
|
if ($curkey === "*") { |
|
220
|
|
|
foreach ($curIndexArg as $queryK => $queryV) { |
|
221
|
|
|
if (!is_array($queryV)) { |
|
222
|
|
|
$request->addQueryParams($queryK, $queryV); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
$add = FALSE; |
|
226
|
|
|
$required = FALSE; |
|
227
|
|
|
break; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$curIndexArg = $curIndexArg[$value1]; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
if (!empty($curIndexArg) && $add) { |
|
234
|
|
|
$request->addQueryParams($curkey, $curIndexArg); |
|
235
|
|
|
continue; |
|
236
|
|
|
} |
|
237
|
|
|
if ($required) { |
|
238
|
|
|
throw new Ks3ClientException($method . " param " . $value . " is required"); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
if (isset($api["body"])) { |
|
243
|
|
|
if (isset($api["body"]["builder"])) { |
|
244
|
|
|
$builderName = $api["body"]["builder"]; |
|
245
|
|
|
$builder = new $builderName(); |
|
246
|
|
|
$request->body = $builder->build($args); |
|
|
|
|
|
|
247
|
|
|
} else if (isset($api["body"]["position"])) { |
|
248
|
|
|
$position = $api["body"]["position"]; |
|
249
|
|
|
$index = explode("->", $position); |
|
250
|
|
|
$curIndexArg = $args; |
|
251
|
|
|
$add = TRUE; |
|
252
|
|
|
$curkey = ""; |
|
|
|
|
|
|
253
|
|
|
foreach ($index as $key1 => $value1) { |
|
254
|
|
|
if (!isset($curIndexArg[$value1])) { |
|
255
|
|
|
$add = FALSE; |
|
256
|
|
|
} else { |
|
257
|
|
|
$curIndexArg = $curIndexArg[$value1]; |
|
258
|
|
|
$curkey = $value1; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
if (!empty($curIndexArg) && $add) { |
|
262
|
|
|
$request->body = $curIndexArg; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
//add ext headers |
|
268
|
|
|
//TODO |
|
269
|
|
|
//sign request |
|
270
|
|
|
$signer = NULL; |
|
271
|
|
|
if (isset($api["signer"])) { |
|
272
|
|
|
$signers = explode("->", $api["signer"]); |
|
273
|
|
|
foreach ($signers as $key => $value) { |
|
274
|
|
|
$signer = new $value(); |
|
275
|
|
|
$log = $signer->sign($request, array("accessKey" => $this->accessKey, "secretKey" => $this->secretKey, "args" => $args)); |
|
276
|
|
|
if (!empty($log)) { |
|
277
|
|
|
$holder->msg .= $log . "\r\n"; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
if ($signer === NULL || !($signer instanceof QueryAuthSigner)) { |
|
283
|
|
|
$url = $request->toUrl($this->endpoint); |
|
284
|
|
|
if ($location != NULL) { |
|
285
|
|
|
$url = $location; |
|
286
|
|
|
} |
|
287
|
|
|
$httpRequest = new RequestCore($url); |
|
288
|
|
|
if (KS3_API_DEBUG_MODE === TRUE) { |
|
|
|
|
|
|
289
|
|
|
$httpRequest->debug_mode = TRUE; |
|
290
|
|
|
} |
|
291
|
|
|
$httpRequest->set_method($request->method); |
|
292
|
|
|
foreach ($request->headers as $key => $value) { |
|
|
|
|
|
|
293
|
|
|
$httpRequest->add_header($key, $value); |
|
294
|
|
|
} |
|
295
|
|
|
$httpRequest->request_body = $request->body; |
|
296
|
|
|
|
|
297
|
|
|
if (isset($args["writeCallBack"])) { |
|
298
|
|
|
$httpRequest->register_streaming_write_callback($args["writeCallBack"]); |
|
299
|
|
|
} |
|
300
|
|
|
if (isset($args["readCallBack"])) { |
|
301
|
|
|
$httpRequest->register_streaming_read_callback($args["readCallBack"]); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
$read_stream = $request->read_stream; |
|
|
|
|
|
|
305
|
|
|
$read_length = $request->getHeader(Headers::$ContentLength); |
|
306
|
|
|
$seek_position = $request->seek_position; |
|
|
|
|
|
|
307
|
|
|
if (isset($read_stream)) { |
|
308
|
|
|
$httpRequest->set_read_stream($read_stream, $read_length); |
|
309
|
|
|
$httpRequest->set_seek_position($seek_position); |
|
310
|
|
|
$httpRequest->remove_header(Headers::$ContentLength); |
|
311
|
|
|
} |
|
312
|
|
|
$write_stream = $request->write_stream; |
|
|
|
|
|
|
313
|
|
|
if (isset($write_stream)) { |
|
314
|
|
|
$httpRequest->set_write_stream($write_stream); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
$holder->msg .= "request url->" . serialize($httpRequest->request_url) . "\r\n"; |
|
318
|
|
|
$holder->msg .= "request headers->" . serialize($httpRequest->request_headers) . "\r\n"; |
|
319
|
|
|
$holder->msg .= "request body->" . $httpRequest->request_body . "\r\n"; |
|
320
|
|
|
$holder->msg .= "request read stream length->" . $read_length . "\r\n"; |
|
321
|
|
|
$holder->msg .= "request read stream seek position->" . $seek_position . "\r\n"; |
|
322
|
|
|
$httpRequest->send_request(); |
|
323
|
|
|
//print_r($httpRequest); |
|
324
|
|
|
$body = $httpRequest->get_response_body(); |
|
325
|
|
|
$data = new ResponseCore ($httpRequest->get_response_header(), Utils::replaceNS2($body), $httpRequest->get_response_code()); |
|
|
|
|
|
|
326
|
|
|
|
|
327
|
|
|
if ($data->status == 307) { |
|
328
|
|
|
$respHeaders = $httpRequest->get_response_header(); |
|
329
|
|
|
$location = $respHeaders["location"]; |
|
330
|
|
|
if (strpos($location, "http") === 0) { |
|
331
|
|
|
$holder->msg .= "response code->" . $httpRequest->get_response_code() . "\r\n"; |
|
332
|
|
|
$holder->msg .= "response headers->" . serialize($httpRequest->get_response_header()) . "\r\n"; |
|
333
|
|
|
$holder->msg .= "response body->" . $body . "\r\n"; |
|
334
|
|
|
$holder->msg .= "retry request to " . $location . "\r\n"; |
|
335
|
|
|
//array($args)详见invoke开头 |
|
336
|
|
|
return $this->invoke($method, array($args), $holder, $location); |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
$holder->msg .= "response code->" . $httpRequest->get_response_code() . "\r\n"; |
|
340
|
|
|
$holder->msg .= "response headers->" . serialize($httpRequest->get_response_header()) . "\r\n"; |
|
341
|
|
|
$holder->msg .= "response body->" . $body . "\r\n"; |
|
342
|
|
|
$handlers = explode("->", $api["handler"]); |
|
343
|
|
|
foreach ($handlers as $key => $value) { |
|
344
|
|
|
$handler = new $value(); |
|
345
|
|
|
$data = $handler->handle($data); |
|
346
|
|
|
} |
|
347
|
|
|
return $data; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
$url = $request->toUrl($this->endpoint); |
|
351
|
|
|
$holder->msg .= $url . "\r\n"; |
|
352
|
|
|
return $url; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
//用于生产表单上传时的签名信息 |
|
356
|
|
|
public function postObject($bucket, $postFormData = array(), $unknowValueFormFiled = array(), $filename = NULL, $expire = 18000): array |
|
357
|
|
|
{ |
|
358
|
|
|
$policy = array(); |
|
359
|
|
|
|
|
360
|
|
|
$expireTime = Utils::iso8601(time() + $expire); |
|
361
|
|
|
$policy["expiration"] = $expireTime; |
|
362
|
|
|
$postFormData["bucket"] = $bucket; |
|
363
|
|
|
$conditions = array(); |
|
364
|
|
|
foreach ($postFormData as $key => $value) { |
|
365
|
|
|
$condition = array(); |
|
366
|
|
|
$condition[$key] = str_replace("\${filename}", $filename, $value); |
|
367
|
|
|
$conditions[] = $condition; |
|
368
|
|
|
} |
|
369
|
|
|
foreach ($unknowValueFormFiled as $key => $value) { |
|
370
|
|
|
$condition = array(); |
|
371
|
|
|
$condition[] = "starts-with"; |
|
372
|
|
|
$condition[] = "\$" . $value; |
|
373
|
|
|
$condition[] = ""; |
|
374
|
|
|
$conditions[] = $condition; |
|
375
|
|
|
} |
|
376
|
|
|
$policy["conditions"] = $conditions; |
|
377
|
|
|
$json = json_encode($policy); |
|
378
|
|
|
$signature = base64_encode(hash_hmac('sha1', base64_encode($json), $this->secretKey, true)); |
|
379
|
|
|
$result = array(); |
|
380
|
|
|
$result["Policy"] = base64_encode($json); |
|
381
|
|
|
$result["Signature"] = $signature; |
|
382
|
|
|
$result["KSSAccessKeyId"] = $this->accessKey; |
|
383
|
|
|
return $result; |
|
384
|
|
|
} |
|
385
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.