From fe399c4875e58abb88d33a49c2f639033e5bd13f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Thu, 26 Nov 2020 01:59:15 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 + src/common.php | 2 +- src/service/aliyun/OssService.php | 9 +- src/service/baidu/BosService.php | 7 +- src/service/huaweicloud/ObsService.php | 6 +- src/service/ksyun/Ks3Service.php | 3 +- src/service/ksyun/bin/Ks3Client.class.php | 142 +++++++++++----------- src/service/qiniu/KodoService.php | 10 +- src/service/tencent/CosService.php | 3 +- src/service/upyun/UssService.php | 7 +- 10 files changed, 95 insertions(+), 97 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afbe4c5..c09bc5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v6.0.130 / 2020-11-26 +- 优化服务 + ## v6.0.129 / 2020-11-26 - 优化服务 diff --git a/src/common.php b/src/common.php index 1fb9ecd..3f99e8f 100644 --- a/src/common.php +++ b/src/common.php @@ -25,7 +25,7 @@ /** * 定义当前版本 */ -const VERSION = '6.0.129'; +const VERSION = '6.0.130'; if (!function_exists('get_ip_info')) { /** diff --git a/src/service/aliyun/OssService.php b/src/service/aliyun/OssService.php index b92ac73..ee54666 100644 --- a/src/service/aliyun/OssService.php +++ b/src/service/aliyun/OssService.php @@ -79,15 +79,16 @@ public function bucket(string $bucket): self /** * @param string $object * @param string $filePath - * @return string|null + * @return bool */ - public function upload(string $object, string $filePath): ?string + public function upload(string $object, string $filePath): bool { try { $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint); - return $ossClient->uploadFile($this->bucket, $object, $filePath); + $ossClient->uploadFile($this->bucket, $object, $filePath); + return true; } catch (OssException $e) { - return $e->getMessage(); + return false; } } } diff --git a/src/service/baidu/BosService.php b/src/service/baidu/BosService.php index 547e6b5..dd0afcc 100644 --- a/src/service/baidu/BosService.php +++ b/src/service/baidu/BosService.php @@ -79,10 +79,10 @@ public function bucket(string $bucket) /** * @param string $object * @param string $filePath - * @return mixed + * @return bool * @throws Exception */ - public function upload(string $object, string $filePath) + public function upload(string $object, string $filePath): bool { // 设置BosClient的Access Key ID、Secret Access Key和ENDPOINT $BOS_TEST_CONFIG = array( @@ -94,6 +94,7 @@ public function upload(string $object, string $filePath) ); $client = new BosClient($BOS_TEST_CONFIG); // 从文件中直接上传Object - return $client->putObjectFromFile($this->bucket, $object, $filePath); + $client->putObjectFromFile($this->bucket, $object, $filePath); + return true; } } diff --git a/src/service/huaweicloud/ObsService.php b/src/service/huaweicloud/ObsService.php index 492cbab..e22be6f 100644 --- a/src/service/huaweicloud/ObsService.php +++ b/src/service/huaweicloud/ObsService.php @@ -78,9 +78,9 @@ public function bucket(string $bucket): self /** * @param string $object * @param string $filePath - * @return bool|\Obs\Internal\Common\Model + * @return bool */ - public function upload(string $object, string $filePath) + public function upload(string $object, string $filePath): bool { // 创建ObsClient实例 $obsClient = new ObsClient([ @@ -94,7 +94,7 @@ public function upload(string $object, string $filePath) 'SourceFile' => $filePath // localfile为待上传的本地文件路径,需要指定到具体的文件名 ]); if (isset($resp['RequestId'])) { - return $resp; + return true; } return false; diff --git a/src/service/ksyun/Ks3Service.php b/src/service/ksyun/Ks3Service.php index 05c86b6..de5c2a9 100644 --- a/src/service/ksyun/Ks3Service.php +++ b/src/service/ksyun/Ks3Service.php @@ -106,7 +106,8 @@ public function upload(string $object, string $filePath): ?bool ] ]; try { - return $client->putObjectByFile($args); + $client->putObjectByFile($args); + return true; } catch (Ks3ServiceException $e) { return false; } diff --git a/src/service/ksyun/bin/Ks3Client.class.php b/src/service/ksyun/bin/Ks3Client.class.php index 2841ea0..d759eb8 100644 --- a/src/service/ksyun/bin/Ks3Client.class.php +++ b/src/service/ksyun/bin/Ks3Client.class.php @@ -5,31 +5,31 @@ //检测API路径 if (!defined("KS3_API_PATH")) { - define("KS3_API_PATH", __DIR__); + define("KS3_API_PATH", __DIR__); } //是否使用VHOST if (!defined("KS3_API_VHOST")) { - define("KS3_API_VHOST", FALSE); + define("KS3_API_VHOST", FALSE); } //是否开启日志(写入日志文件) if (!defined("KS3_API_LOG")) { - define("KS3_API_LOG", FALSE); + define("KS3_API_LOG", FALSE); } //是否显示日志(直接输出日志) if (!defined("KS3_API_DISPLAY_LOG")) { - define("KS3_API_DISPLAY_LOG", FALSE); + define("KS3_API_DISPLAY_LOG", FALSE); } //定义日志目录(默认是该项目log下) if (!defined("KS3_API_LOG_PATH")) { - define("KS3_API_LOG_PATH", ""); + define("KS3_API_LOG_PATH", ""); } //是否使用HTTPS if (!defined("KS3_API_USE_HTTPS")) { - define("KS3_API_USE_HTTPS", FALSE); + define("KS3_API_USE_HTTPS", FALSE); } //是否开启curl debug模式 if (!defined("KS3_API_DEBUG_MODE")) { - define("KS3_API_DEBUG_MODE", FALSE); + define("KS3_API_DEBUG_MODE", FALSE); } define("KS3_API_Author", "lijunwei@kingsoft.com"); define("KS3_API_Version", "1.2"); @@ -68,7 +68,6 @@ class Ks3Client private $accessKey; private $secretKey; private $endpoint; - private $log; public function __construct($accessKey = NULL, $secretKey = NULL, $endpoint = NULL) { @@ -81,48 +80,47 @@ public function __construct($accessKey = NULL, $secretKey = NULL, $endpoint = NU $this->endpoint = $endpoint; $this->signers = array(); - $this->log = new Logger(); } - /** - * 方法列表:(具体使用请参考readme.md) - * listBuckets,罗列bucket - * deleteBucket,删除bucket - * deleteBucketCORS,删除bucket跨域配置 - * createBucket,新建bucket - * setBucketAcl,设置bucket访问权限 - * setBucketCORS,设置bucket跨域配置 - * setBucketLogging,设置bucket日志配置 - * listObjects,罗列object - * getBucketAcl,获取bucket访问权限 - * getBucketCORS,获取bucket跨域配置 - * getBucketLocation,获取bucket地点配置 - * getBucketLogging,获取bucket日志配置 - * bucketExists,判断bucket是否存在 - * listMutipartUploads,罗列当前bucket下尚未结束的分块上传 - * putObjectByContent,上传文件 - * putObjectByFile,上传文件 - * setObjectAcl,设置object访问权限 - * copyObject,复制object - * getObjectMeta,获取object元数据 - * objectExists,判断object是否存在 - * deleteObject,删除object - * deleteObjects,删除多个object - * getObject,下载object - * getObjectAcl,获取object访问权限 - * initMultipartUpload,初始化分块上传 - * uploadPart,上传块 - * abortMultipartUpload,终止分块上传 - * listParts,罗列已经上传的块 - * completeMultipartUpload,完成分块上传 - * generatePresignedUrl,生成文件外链 - * putAdp,添加异步数据处理任务 - * getAdp,查询异步数据处理任务 - * @param $method - * @param array $args - * @return ResponseCore|string|string[]|null - * @throws Exception - */ + /** + * 方法列表:(具体使用请参考readme.md) + * listBuckets,罗列bucket + * deleteBucket,删除bucket + * deleteBucketCORS,删除bucket跨域配置 + * createBucket,新建bucket + * setBucketAcl,设置bucket访问权限 + * setBucketCORS,设置bucket跨域配置 + * setBucketLogging,设置bucket日志配置 + * listObjects,罗列object + * getBucketAcl,获取bucket访问权限 + * getBucketCORS,获取bucket跨域配置 + * getBucketLocation,获取bucket地点配置 + * getBucketLogging,获取bucket日志配置 + * bucketExists,判断bucket是否存在 + * listMutipartUploads,罗列当前bucket下尚未结束的分块上传 + * putObjectByContent,上传文件 + * putObjectByFile,上传文件 + * setObjectAcl,设置object访问权限 + * copyObject,复制object + * getObjectMeta,获取object元数据 + * objectExists,判断object是否存在 + * deleteObject,删除object + * deleteObjects,删除多个object + * getObject,下载object + * getObjectAcl,获取object访问权限 + * initMultipartUpload,初始化分块上传 + * uploadPart,上传块 + * abortMultipartUpload,终止分块上传 + * listParts,罗列已经上传的块 + * completeMultipartUpload,完成分块上传 + * generatePresignedUrl,生成文件外链 + * putAdp,添加异步数据处理任务 + * getAdp,查询异步数据处理任务 + * @param $method + * @param array $args + * @return ResponseCore|string|string[]|null + * @throws Exception + */ public function __call($method, $args = array()) { $holder = new MessageHolder(); @@ -137,10 +135,9 @@ public function __call($method, $args = array()) $ex = $e; } $holder->msg .= "------------------Logging End-------------------------\r\n"; - $this->log->info($holder->msg); if ($ex != NULL) { - throw $ex; - } + throw $ex; + } return $result; } @@ -167,8 +164,8 @@ private function invoke($method, $args = array(), $holder, $location = NULL) } else { $request->bucket = $args["Bucket"]; } - $position = $api["objectPostion"] ?? "Key"; - if (empty($args[$position])) { + $position = $api["objectPostion"] ?? "Key"; + if (empty($args[$position])) { if ($api["needObject"]) { throw new Ks3ClientException($method . " this api need " . $position); } @@ -192,11 +189,10 @@ private function invoke($method, $args = array(), $holder, $location = NULL) $request->method = $api["method"]; } if (KS3_API_USE_HTTPS) { - $request->scheme = "https://"; - } - else { - $request->scheme = "http://"; - } + $request->scheme = "https://"; + } else { + $request->scheme = "http://"; + } $request->endpoint = $this->endpoint; //add subresource if (!empty($api["subResource"])) { @@ -231,16 +227,16 @@ private function invoke($method, $args = array(), $holder, $location = NULL) break; } - $curIndexArg = $curIndexArg[$value1]; - } + $curIndexArg = $curIndexArg[$value1]; + } } if (!empty($curIndexArg) && $add) { $request->addQueryParams($curkey, $curIndexArg); continue; } if ($required) { - throw new Ks3ClientException($method . " param " . $value . " is required"); - } + throw new Ks3ClientException($method . " param " . $value . " is required"); + } } } if (isset($api["body"])) { @@ -286,12 +282,12 @@ private function invoke($method, $args = array(), $holder, $location = NULL) if ($signer === NULL || !($signer instanceof QueryAuthSigner)) { $url = $request->toUrl($this->endpoint); if ($location != NULL) { - $url = $location; - } + $url = $location; + } $httpRequest = new RequestCore($url); if (KS3_API_DEBUG_MODE === TRUE) { - $httpRequest->debug_mode = TRUE; - } + $httpRequest->debug_mode = TRUE; + } $httpRequest->set_method($request->method); foreach ($request->headers as $key => $value) { $httpRequest->add_header($key, $value); @@ -351,14 +347,14 @@ private function invoke($method, $args = array(), $holder, $location = NULL) return $data; } - $url = $request->toUrl($this->endpoint); - $holder->msg .= $url . "\r\n"; - return $url; - } + $url = $request->toUrl($this->endpoint); + $holder->msg .= $url . "\r\n"; + return $url; + } //用于生产表单上传时的签名信息 public function postObject($bucket, $postFormData = array(), $unknowValueFormFiled = array(), $filename = NULL, $expire = 18000): array - { + { $policy = array(); $expireTime = Utils::iso8601(time() + $expire); @@ -386,6 +382,4 @@ public function postObject($bucket, $postFormData = array(), $unknowValueFormFil $result["KSSAccessKeyId"] = $this->accessKey; return $result; } -} - - +} \ No newline at end of file diff --git a/src/service/qiniu/KodoService.php b/src/service/qiniu/KodoService.php index 10da7a2..5cbf84e 100644 --- a/src/service/qiniu/KodoService.php +++ b/src/service/qiniu/KodoService.php @@ -70,10 +70,10 @@ public function bucket(string $bucket): self /** * @param string $object * @param string $filePath - * @return bool|mixed + * @return bool * @throws Exception */ - public function upload(string $object, string $filePath) + public function upload(string $object, string $filePath): bool { // 初始化签权对象 $auth = new Auth($this->accessKey, $this->secretKey); @@ -83,11 +83,7 @@ public function upload(string $object, string $filePath) $uploadMgr = new UploadManager(); // 调用 UploadManager 的 putFile 方法进行文件的上传。 [$ret, $err] = $uploadMgr->putFile($token, $object, $filePath); - if ($err !== null) { - return false; - } - - return $ret; + return !($err !== null); } } diff --git a/src/service/tencent/CosService.php b/src/service/tencent/CosService.php index ae80468..1f24a73 100644 --- a/src/service/tencent/CosService.php +++ b/src/service/tencent/CosService.php @@ -93,11 +93,12 @@ public function upload(string $object, string $filePath): bool $key = $object; $file = fopen($filePath, "rb"); if ($file) { - return $cosClient->putObject([ + $cosClient->putObject([ 'Bucket' => $this->bucket, 'Key' => $key, 'Body' => $file ]); + return true; } return false; diff --git a/src/service/upyun/UssService.php b/src/service/upyun/UssService.php index b056229..d19fa69 100644 --- a/src/service/upyun/UssService.php +++ b/src/service/upyun/UssService.php @@ -70,14 +70,15 @@ public function operatorPassword(string $operatorPassword) /** * @param string $object * @param string $filePath - * @return array|bool + * @return bool * @throws Exception */ - public function upload(string $object, string $filePath) + public function upload(string $object, string $filePath): bool { $serviceConfig = new Config($this->serviceName, $this->operatorName, $this->operatorPassword); $client = new Upyun($serviceConfig); $file = fopen($filePath, 'r'); - return $client->write($object, $file); + $client->write($object, $file); + return true; } }