From 285b3ca92b88ff59cebc3a583028b2e3cd03cb8d Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Thu, 8 Jun 2023 14:04:18 +0800 Subject: [PATCH 1/7] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=8D=8E=E4=B8=BA?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=A1=B6=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/v1/setting/SystemStorage.php | 5 ++++- crmeb/crmeb/services/upload/extend/obs/Client.php | 14 +++++++++----- crmeb/crmeb/services/upload/storage/Obs.php | 12 +++++------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemStorage.php b/crmeb/app/adminapi/controller/v1/setting/SystemStorage.php index cdbefab4a..853bbea62 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemStorage.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemStorage.php @@ -166,7 +166,10 @@ class SystemStorage extends AuthController //设置跨域规则 try { $upload = UploadService::init($info->type); - $upload->setBucketCors($info->name, $info->region); + $res = $upload->setBucketCors($info->name, $info->region); + if (false === $res) { + return app('json')->fail($upload->getError()); + } } catch (\Throwable $e) { } diff --git a/crmeb/crmeb/services/upload/extend/obs/Client.php b/crmeb/crmeb/services/upload/extend/obs/Client.php index 5cdfd7cd0..b33ba9bb6 100644 --- a/crmeb/crmeb/services/upload/extend/obs/Client.php +++ b/crmeb/crmeb/services/upload/extend/obs/Client.php @@ -252,10 +252,12 @@ class Client extends BaseClient $header = [ 'x-obs-acl' => $acl, 'Host' => $this->getRequestUrl($bucket, $region), + "Content-Type" => "application/xml" ]; - + $xml = "{$region}"; $res = $this->request('https://' . $header['Host'] . '/', 'PUT', [ - 'Location' => $region + 'bucket' => $bucket, + 'body' => $xml ], $header); return $this->response($res); @@ -333,12 +335,14 @@ class Client extends BaseClient */ public function putBucketCors(string $bucket, string $region, array $data = []) { + $xml = $this->xmlBuild($data, 'CORSConfiguration', 'CORSRule'); $header = [ 'Host' => $this->getRequestUrl($bucket, $region), - 'content-md5' => base64_encode(md5(json_encode($data), true)) + 'Content-Type' => 'application/xml', + 'Content-Length' => strlen($xml), + 'Content-MD5' => base64_encode(md5($xml, true)) ]; - $xml = $this->xmlBuild($data, 'CORSConfiguration', 'CORSRule'); - $res = $this->request('https://' . $header['Host'] . '/cors ', 'PUT', [ + $res = $this->request('https://' . $header['Host'] . '/cors', 'PUT', [ 'bucket' => $bucket, 'body' => $xml ], $header); diff --git a/crmeb/crmeb/services/upload/storage/Obs.php b/crmeb/crmeb/services/upload/storage/Obs.php index 643f53b67..686b5c63f 100644 --- a/crmeb/crmeb/services/upload/storage/Obs.php +++ b/crmeb/crmeb/services/upload/storage/Obs.php @@ -6,6 +6,7 @@ use crmeb\exceptions\AdminException; use crmeb\exceptions\UploadException; use crmeb\services\upload\extend\obs\Client as TyClient; use crmeb\services\upload\BaseUpload; +use GuzzleHttp\Psr7\Utils; class Obs extends BaseUpload { @@ -70,7 +71,8 @@ class Obs extends BaseUpload } $key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension()); - $body = $fileHandle->getRealPath(); + $body = fopen($fileHandle->getRealPath(), 'rb'); + $body = (string)Utils::streamFor($body); } else { $key = $file; $body = $fileContent; @@ -78,7 +80,7 @@ class Obs extends BaseUpload $key = $this->getUploadPath($key); try { - $uploadInfo = $this->app()->putObject($key, $body); + $uploadInfo = $this->app()->putObject($key, $body, 'application/octet-stream'); $this->fileInfo->uploadInfo = $uploadInfo; $this->fileInfo->realName = $fileHandle->getOriginalName(); $this->fileInfo->filePath = ($this->cdn ?: $this->uploadUrl) . '/' . $key; @@ -205,17 +207,13 @@ class Obs extends BaseUpload { $parseDomin = parse_url($domain); try { - $this->app()->putBucketDomain($name, '', [ + $this->app()->putBucketDomain($name, $region, [ 'domainname' => $parseDomin['host'], ]); return true; } catch (\Throwable $e) { - if ($message = $this->setMessage($e->getMessage())) { - return $this->setError($message); - } return $this->setError($e->getMessage()); } - return false; } public function setBucketCors(string $name, string $region) From 0d57902b66257ec54e39106f9718ca249ec4caa5 Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Thu, 8 Jun 2023 15:03:56 +0800 Subject: [PATCH 2/7] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0cdn=E5=9F=9F?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adminapi/controller/v1/setting/SystemStorage.php | 3 ++- crmeb/app/services/other/UploadService.php | 1 + .../services/system/config/SystemStorageServices.php | 11 ++++++++--- crmeb/crmeb/services/upload/extend/obs/Client.php | 4 +--- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemStorage.php b/crmeb/app/adminapi/controller/v1/setting/SystemStorage.php index 853bbea62..9d1c3696a 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemStorage.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemStorage.php @@ -202,6 +202,7 @@ class SystemStorage extends AuthController public function updateDomain($id) { $domain = $this->request->post('domain', ''); + $cdn = $this->request->post('cdn', ''); $data = $this->request->postMore([ ['pri', ''], ['ca', ''] @@ -217,7 +218,7 @@ class SystemStorage extends AuthController // return app('json')->fail('域名为HTTPS访问时,必须填写证书'); // } - $this->services->updateDomain($id, $domain); + $this->services->updateDomain($id, $domain, ['cdn' => $cdn]); return app('json')->success(100001); } diff --git a/crmeb/app/services/other/UploadService.php b/crmeb/app/services/other/UploadService.php index 45822ad2c..5e0fff9f6 100644 --- a/crmeb/app/services/other/UploadService.php +++ b/crmeb/app/services/other/UploadService.php @@ -95,6 +95,7 @@ class UploadService $config['uploadUrl'] = $res['domain']; $config['storageName'] = $res['name']; $config['storageRegion'] = $res['region']; + $config['cdn'] = $res['cdn']; } $thumb = SystemConfigService::more(['thumb_big_height', 'thumb_big_width', 'thumb_mid_height', 'thumb_mid_width', 'thumb_small_height', 'thumb_small_width',]); diff --git a/crmeb/app/services/system/config/SystemStorageServices.php b/crmeb/app/services/system/config/SystemStorageServices.php index dfd61fcd6..20da7bb27 100644 --- a/crmeb/app/services/system/config/SystemStorageServices.php +++ b/crmeb/app/services/system/config/SystemStorageServices.php @@ -425,11 +425,11 @@ class SystemStorageServices extends BaseServices */ public function getConfig(int $type) { - $res = ['name' => '', 'region' => '', 'domain' => '']; + $res = ['name' => '', 'region' => '', 'domain' => '', 'cdn' => '']; try { $config = $this->dao->get(['type' => $type, 'status' => 1, 'is_delete' => 0]); if ($config) { - return ['name' => $config->name, 'region' => $config->region, 'domain' => $config->domain]; + return ['name' => $config->name, 'region' => $config->region, 'domain' => $config->domain, 'cdn' => $config->cdn]; } } catch (\Throwable $e) { } @@ -448,6 +448,7 @@ class SystemStorageServices extends BaseServices $domain = $this->dao->value(['id' => $id], 'domain'); $rule = [ FormBuilder::input('domain', '空间域名', $domain), + FormBuilder::input('cdn', 'cdn域名', $domain), ]; return create_form('修改空间域名', $rule, '/system/config/storage/domain/' . $id); } @@ -488,7 +489,11 @@ class SystemStorageServices extends BaseServices $resDomain = $upload->getDomianInfo($domain); $info->cname = $resDomain['cname'] ?? ''; } - return $info->save(); + $info->save(); + } + if ($info->cdn != $data['cdn']) { + $info->cdn = $data['cdn']; + $info->save(); } $this->cacheDriver()->clear(); diff --git a/crmeb/crmeb/services/upload/extend/obs/Client.php b/crmeb/crmeb/services/upload/extend/obs/Client.php index b33ba9bb6..3fec47ae5 100644 --- a/crmeb/crmeb/services/upload/extend/obs/Client.php +++ b/crmeb/crmeb/services/upload/extend/obs/Client.php @@ -175,10 +175,8 @@ class Client extends BaseClient */ public function putObject(string $key, $body, string $contentType = 'image/jpeg') { - $url = $this->uploadUrl ?: $this->getRequestUrl($this->bucketName, $this->region); - $header = [ - 'Host' => $url, + 'Host' => $this->getRequestUrl($this->bucketName, $this->region), 'Content-Type' => $contentType, 'Content-Length' => strlen($body), ]; From 0b9483266b0889d2e60ae465ad964bf817c95d19 Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Thu, 8 Jun 2023 15:08:45 +0800 Subject: [PATCH 3/7] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9cdn=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E8=A1=A8=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/app/services/system/config/SystemStorageServices.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crmeb/app/services/system/config/SystemStorageServices.php b/crmeb/app/services/system/config/SystemStorageServices.php index 20da7bb27..97f22d1be 100644 --- a/crmeb/app/services/system/config/SystemStorageServices.php +++ b/crmeb/app/services/system/config/SystemStorageServices.php @@ -445,10 +445,10 @@ class SystemStorageServices extends BaseServices */ public function getUpdateDomainForm(int $id) { - $domain = $this->dao->value(['id' => $id], 'domain'); + $storage = $this->dao->get(['id' => $id], ['domain', 'cdn']); $rule = [ - FormBuilder::input('domain', '空间域名', $domain), - FormBuilder::input('cdn', 'cdn域名', $domain), + FormBuilder::input('domain', '空间域名', $storage['domain']), + FormBuilder::input('cdn', 'cdn域名', $storage['cnd']), ]; return create_form('修改空间域名', $rule, '/system/config/storage/domain/' . $id); } From 31b092c3111f568471dcec3201c8f92205aaf51b Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Thu, 8 Jun 2023 15:58:23 +0800 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20=E5=8D=8E=E4=B8=BA=E4=BA=91=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=B8=B8=E8=A7=81=E6=A1=B6=E7=AD=96=E7=95=A5=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/config/SystemStorageServices.php | 2 +- .../services/upload/extend/obs/Client.php | 47 ++++++++++++++++++- crmeb/crmeb/services/upload/storage/Obs.php | 13 +++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/crmeb/app/services/system/config/SystemStorageServices.php b/crmeb/app/services/system/config/SystemStorageServices.php index 97f22d1be..b4861ced5 100644 --- a/crmeb/app/services/system/config/SystemStorageServices.php +++ b/crmeb/app/services/system/config/SystemStorageServices.php @@ -448,7 +448,7 @@ class SystemStorageServices extends BaseServices $storage = $this->dao->get(['id' => $id], ['domain', 'cdn']); $rule = [ FormBuilder::input('domain', '空间域名', $storage['domain']), - FormBuilder::input('cdn', 'cdn域名', $storage['cnd']), + FormBuilder::input('cdn', 'cdn域名', $storage['cdn']), ]; return create_form('修改空间域名', $rule, '/system/config/storage/domain/' . $id); } diff --git a/crmeb/crmeb/services/upload/extend/obs/Client.php b/crmeb/crmeb/services/upload/extend/obs/Client.php index 3fec47ae5..fb3129c3b 100644 --- a/crmeb/crmeb/services/upload/extend/obs/Client.php +++ b/crmeb/crmeb/services/upload/extend/obs/Client.php @@ -235,6 +235,30 @@ class Client extends BaseClient return $this->response($res); } + /** + * 设置桶的策略 + * @param string $bucket + * @param string $region + * @param array $data + * @return mixed + * + * @date 2023/06/08 + * @author yyw + */ + public function putPolicy(string $bucket, string $region, array $data) + { + $header = [ + 'Host' => $this->getRequestUrl($bucket, $region), + "Content-Type" => "application/json" + ]; + $res = $this->request('https://' . $header['Host'] . '/?policy', 'PUT', [ + 'bucket' => $bucket, + 'json' => $data + ], $header); + + return $this->response($res); + } + /** * 创建桶 * @param string $bucket @@ -340,7 +364,7 @@ class Client extends BaseClient 'Content-Length' => strlen($xml), 'Content-MD5' => base64_encode(md5($xml, true)) ]; - $res = $this->request('https://' . $header['Host'] . '/cors', 'PUT', [ + $res = $this->request('https://' . $header['Host'] . '/?cors', 'PUT', [ 'bucket' => $bucket, 'body' => $xml ], $header); @@ -348,6 +372,27 @@ class Client extends BaseClient return $this->response($res); } + /** + * 删除跨域 + * @param string $bucket + * @param string $region + * @return mixed + * + * @date 2023/06/08 + * @author yyw + */ + public function deleteBucketCors(string $bucket, string $region) + { + $header = [ + 'Host' => $this->getRequestUrl($bucket, $region), + ]; + $res = $this->request('https://' . $header['Host'] . '/?cors', 'DELETE', [ + 'bucket' => $bucket, + ], $header); + + return $this->response($res); + } + /** * @param $res * @return mixed diff --git a/crmeb/crmeb/services/upload/storage/Obs.php b/crmeb/crmeb/services/upload/storage/Obs.php index 686b5c63f..8d1bfcf53 100644 --- a/crmeb/crmeb/services/upload/storage/Obs.php +++ b/crmeb/crmeb/services/upload/storage/Obs.php @@ -166,6 +166,19 @@ class Obs extends BaseUpload //创建桶 try { $app->createBucket($name, $region, $acl); + $data = [ + 'Statement' => [ + 'Sid' => '公共读' . $name, + 'Effect' => 'Allow1', + 'Principal' => [ + 'ID' => ['*'] + ], + 'Action' => ['HeadBucket', 'GetBucketLocation', 'ListBucketVersions', 'GetObject', 'RestoreObject', 'GetObjectVersion'], + 'Resource' => [$name, $name . '/*'] + ] + ]; + + $app->putPolicy($name, $region, $data); } catch (\Throwable $e) { return $this->setError('COS:' . $e->getMessage()); } From 7e9ed57c40c921848f3e3a72c9c9561e01654c27 Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Thu, 8 Jun 2023 16:33:02 +0800 Subject: [PATCH 5/7] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=BA=91=E5=82=A8?= =?UTF-8?q?=E5=AD=98=E5=90=8C=E6=AD=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/config/SystemStorageServices.php | 37 ++++++++++++++++++ crmeb/crmeb/services/upload/storage/Jdoss.php | 39 +++---------------- crmeb/crmeb/services/upload/storage/Obs.php | 4 +- 3 files changed, 44 insertions(+), 36 deletions(-) diff --git a/crmeb/app/services/system/config/SystemStorageServices.php b/crmeb/app/services/system/config/SystemStorageServices.php index b4861ced5..6fff59dbe 100644 --- a/crmeb/app/services/system/config/SystemStorageServices.php +++ b/crmeb/app/services/system/config/SystemStorageServices.php @@ -368,6 +368,34 @@ class SystemStorageServices extends BaseServices } } break; + case 5:// cos 京东云 + case 6:// cos 华为云 + case 7:// cos 天翼云 + $upload = UploadService::init($type); + $list = $upload->listbuckets(); + if (!empty($list['Name'])) { + $newList = $list; + $list = []; + $list[] = $newList; + } + $config = $this->getStorageConfig($type); + foreach ($list as $item) { + if (!$this->dao->count(['name' => $item['Name'], 'access_key' => $config['accessKey']])) { + $data[] = [ + 'type' => $type, + 'access_key' => $config['accessKey'], + 'name' => $item['Name'], + 'region' => $item['Location'], + 'acl' => 'public-read', + 'status' => 0, + 'domain' => sys_config('tengxun_appid') ? $this->getDomain($type, $item['Name'], $item['Location']) : '', + 'is_delete' => 0, + 'add_time' => strtotime($item['CreationDate']), + 'update_time' => time() + ]; + } + } + break; } if ($data) { $this->dao->saveAll($data); @@ -413,6 +441,15 @@ class SystemStorageServices extends BaseServices case 4:// cos 腾讯云 $domainName = 'https://' . $name . ($appid ? '-' . $appid : '') . '.cos.' . $reagion . '.myqcloud.com'; break; + case 5:// cos 京东云 + $domainName = 'https://' . $name . '.cos.' . $reagion . '.jdcloud-oss.com'; + break; + case 6:// cos 华为云 + $domainName = 'https://' . $name . '.cos.' . $reagion . '.myhuaweicloud.com'; + break; + case 7:// cos 天翼云 + $domainName = 'https://' . $name . '.cos.' . $reagion . '.ctyun.cn'; + break; } return $domainName; } diff --git a/crmeb/crmeb/services/upload/storage/Jdoss.php b/crmeb/crmeb/services/upload/storage/Jdoss.php index e4663634e..867a63275 100644 --- a/crmeb/crmeb/services/upload/storage/Jdoss.php +++ b/crmeb/crmeb/services/upload/storage/Jdoss.php @@ -147,7 +147,7 @@ class Jdoss extends BaseUpload 'Key' => $key, 'SourceFile' => $fileHandle->getRealPath() ]); - if (!isset($uploadInfo['info']['url'])) { + if (!isset($uploadInfo['ObjectURL'])) { return $this->setError('Upload failure'); } $this->fileInfo->uploadInfo = $uploadInfo; @@ -157,7 +157,7 @@ class Jdoss extends BaseUpload $this->fileInfo->filePathWater = $this->water($this->fileInfo->filePath); $this->authThumb && $this->thumb($this->fileInfo->filePath); return $this->fileInfo; - } catch (UploadException $e) { + } catch (\Throwable $e) { return $this->setError($e->getMessage()); } } @@ -185,7 +185,7 @@ class Jdoss extends BaseUpload $this->fileInfo->filePathWater = $this->water($this->fileInfo->filePath); $this->thumb($this->fileInfo->filePath); return $this->fileInfo; - } catch (UploadException $e) { + } catch (\Throwable $e) { return $this->setError($e->getMessage()); } } @@ -207,7 +207,7 @@ class Jdoss extends BaseUpload { try { $res = $this->app()->listBuckets(); - return $res['Buckets']['Bucket'] ?? []; + return $res['ListAllMyBucketsResult']['Buckets'] ?? []; } catch (\Throwable $e) { return []; } @@ -290,7 +290,7 @@ class Jdoss extends BaseUpload { try { $this->storageRegion = $region; - $res = $this->app()->getBucketWebsite([ + $res = $this->app()->getBucketPolicy([ 'Bucket' => $name ]); return $res['DomainName'] ?? []; @@ -429,33 +429,4 @@ class Jdoss extends BaseUpload } return $waterPath; } - - public function setError(?string $error = null) - { - $this->error = $error ?: '未知错误'; - if (env('APP_DEBUG')) { - throw new UploadException($this->xmlToArr($this->error)); - } else { - return false; - } - } - - function xmlToArr($errorXml) - { - - $pattern = '/(\d+)<\/statusCode>(.*?)<\/Code>(.*?)<\/Message>(.*?)<\/Resource>(.*?)<\/RequestId>/'; - - preg_match($pattern, $errorXml, $matches); - - $error = [ - 'statusCode' => $matches[1], - 'Code' => $matches[2], - 'Message' => $matches[3], - 'Resource' => $matches[4], - 'RequestId' => $matches[5] - ]; - - return $error; - - } } diff --git a/crmeb/crmeb/services/upload/storage/Obs.php b/crmeb/crmeb/services/upload/storage/Obs.php index 8d1bfcf53..860bc4756 100644 --- a/crmeb/crmeb/services/upload/storage/Obs.php +++ b/crmeb/crmeb/services/upload/storage/Obs.php @@ -148,7 +148,7 @@ class Obs extends BaseUpload try { $this->storageRegion = $region; $res = $this->app()->listBuckets(); - return $res['Buckets']['Bucket'] ?? []; + return $res['Buckets'] ?? []; } catch (\Throwable $e) { return []; } @@ -169,7 +169,7 @@ class Obs extends BaseUpload $data = [ 'Statement' => [ 'Sid' => '公共读' . $name, - 'Effect' => 'Allow1', + 'Effect' => 'Allow', 'Principal' => [ 'ID' => ['*'] ], From 1e6ccb1c7c188c0f1a555a1582c3bed68ef6cd3a Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Thu, 8 Jun 2023 17:11:08 +0800 Subject: [PATCH 6/7] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=BA=91=E5=82=A8?= =?UTF-8?q?=E5=AD=98=E5=90=8C=E6=AD=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/config/SystemStorageServices.php | 7 +------ .../services/upload/extend/obs/Client.php | 21 +++++++++++-------- crmeb/crmeb/services/upload/storage/Obs.php | 2 +- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/crmeb/app/services/system/config/SystemStorageServices.php b/crmeb/app/services/system/config/SystemStorageServices.php index 6fff59dbe..85d0c3f7e 100644 --- a/crmeb/app/services/system/config/SystemStorageServices.php +++ b/crmeb/app/services/system/config/SystemStorageServices.php @@ -373,11 +373,6 @@ class SystemStorageServices extends BaseServices case 7:// cos 天翼云 $upload = UploadService::init($type); $list = $upload->listbuckets(); - if (!empty($list['Name'])) { - $newList = $list; - $list = []; - $list[] = $newList; - } $config = $this->getStorageConfig($type); foreach ($list as $item) { if (!$this->dao->count(['name' => $item['Name'], 'access_key' => $config['accessKey']])) { @@ -388,7 +383,7 @@ class SystemStorageServices extends BaseServices 'region' => $item['Location'], 'acl' => 'public-read', 'status' => 0, - 'domain' => sys_config('tengxun_appid') ? $this->getDomain($type, $item['Name'], $item['Location']) : '', + 'domain' => $this->getDomain($type, $item['Name'], $item['Location']), 'is_delete' => 0, 'add_time' => strtotime($item['CreationDate']), 'update_time' => time() diff --git a/crmeb/crmeb/services/upload/extend/obs/Client.php b/crmeb/crmeb/services/upload/extend/obs/Client.php index fb3129c3b..222b04b8f 100644 --- a/crmeb/crmeb/services/upload/extend/obs/Client.php +++ b/crmeb/crmeb/services/upload/extend/obs/Client.php @@ -220,7 +220,7 @@ class Client extends BaseClient public function listBuckets() { $header = [ - 'Host' => $this->getRequestUrl($this->bucketName, $this->region), + 'Host' => $this->getRequestUrl('', $this->region), ]; $res = $this->request('https://' . $header['Host'] . '/', 'GET', [], []); return $this->response($res); @@ -413,19 +413,22 @@ class Client extends BaseClient * @param string $bucket * @param string $region * @return string - * @author 等风来 - * @email 136327134@qq.com - * @date 2023/5/18 + * + * @date 2023/06/08 + * @author yyw */ - protected function getRequestUrl(string $bucket, string $region) + protected function getRequestUrl(string $bucket = '', string $region = '') { if ($this->type == 'hw') { - return $bucket . '.obs.' . $region . '.myhuaweicloud.com'; // 华为 + $url = '.myhuaweicloud.com'; // 华为 } else { - return $bucket . '.obs.' . $region . '.ctyun.cn'; // 天翼 + $url = $region . '.ctyun.cn'; // 天翼 + } + if ($bucket) { + return $bucket . '.obs.' . $region . $url; + } else { + return 'obs.' . $region . $url; } - - } /** diff --git a/crmeb/crmeb/services/upload/storage/Obs.php b/crmeb/crmeb/services/upload/storage/Obs.php index 860bc4756..fc0428a9b 100644 --- a/crmeb/crmeb/services/upload/storage/Obs.php +++ b/crmeb/crmeb/services/upload/storage/Obs.php @@ -148,7 +148,7 @@ class Obs extends BaseUpload try { $this->storageRegion = $region; $res = $this->app()->listBuckets(); - return $res['Buckets'] ?? []; + return $res['Buckets']['Bucket'] ?? []; } catch (\Throwable $e) { return []; } From 39368b2e3d4bbbb6a0b5dde1e5fd16c17ddb5ecc Mon Sep 17 00:00:00 2001 From: Gosowong <1329847115@qq.com> Date: Thu, 8 Jun 2023 17:38:08 +0800 Subject: [PATCH 7/7] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=A4=A9=E7=BF=BC?= =?UTF-8?q?=E4=BA=91=E4=BA=91=E5=82=A8=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/crmeb/services/upload/storage/Obs.php | 7 +- crmeb/crmeb/services/upload/storage/Tyoss.php | 222 +++++++++++++----- 2 files changed, 165 insertions(+), 64 deletions(-) diff --git a/crmeb/crmeb/services/upload/storage/Obs.php b/crmeb/crmeb/services/upload/storage/Obs.php index fc0428a9b..c5a9910f5 100644 --- a/crmeb/crmeb/services/upload/storage/Obs.php +++ b/crmeb/crmeb/services/upload/storage/Obs.php @@ -136,17 +136,16 @@ class Obs extends BaseUpload $this->handle = new TyClient([ 'accessKey' => $this->accessKey, 'secretKey' => $this->secretKey, - 'region' => $this->storageRegion ?: 'oos-hazz', + 'region' => $this->storageRegion ?: 'cn-north-1', 'bucket' => $this->storageName, 'uploadUrl' => $this->uploadUrl ]); return $this->handle; } - public function listbuckets(string $region, bool $line = false, bool $shared = false) + public function listbuckets(string $region = null, bool $line = false, bool $shared = false) { try { - $this->storageRegion = $region; $res = $this->app()->listBuckets(); return $res['Buckets']['Bucket'] ?? []; } catch (\Throwable $e) { @@ -205,7 +204,7 @@ class Obs extends BaseUpload try { $res = $this->app()->GetBucketDomain($name, $region); if ($res) { - $domainRules = $res->toArray()['ListBucketCustomDomainsResult ']; + $domainRules = $res->toArray()['ListBucketCustomDomainsResult']; return array_column($domainRules, 'DomainName'); } else { return []; diff --git a/crmeb/crmeb/services/upload/storage/Tyoss.php b/crmeb/crmeb/services/upload/storage/Tyoss.php index 74175be2e..61e9b862c 100644 --- a/crmeb/crmeb/services/upload/storage/Tyoss.php +++ b/crmeb/crmeb/services/upload/storage/Tyoss.php @@ -5,6 +5,7 @@ namespace crmeb\services\upload\storage; use crmeb\exceptions\AdminException; use crmeb\services\upload\extend\obs\Client as TyClient; use crmeb\services\upload\BaseUpload; +use GuzzleHttp\Psr7\Utils; class Tyoss extends BaseUpload { @@ -69,7 +70,8 @@ class Tyoss extends BaseUpload } $key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension()); - $body = $fileHandle->getRealPath(); + $body = fopen($fileHandle->getRealPath(), 'rb'); + $body = (string)Utils::streamFor($body); } else { $key = $file; $body = $fileContent; @@ -77,7 +79,7 @@ class Tyoss extends BaseUpload $key = $this->getUploadPath($key); try { - $uploadInfo = $this->app()->putObject($key, $body); + $uploadInfo = $this->app()->putObject($key, $body, 'application/octet-stream'); $this->fileInfo->uploadInfo = $uploadInfo; $this->fileInfo->realName = $fileHandle->getOriginalName(); $this->fileInfo->filePath = ($this->cdn ?: $this->uploadUrl) . '/' . $key; @@ -133,7 +135,7 @@ class Tyoss extends BaseUpload $this->handle = new TyClient([ 'accessKey' => $this->accessKey, 'secretKey' => $this->secretKey, - 'region' => $this->storageRegion ?: 'oos-hazz', + 'region' => $this->storageRegion ?: 'cn-qhxn1', 'bucket' => $this->storageName, 'uploadUrl' => $this->uploadUrl, 'type' => 'ty' @@ -141,10 +143,9 @@ class Tyoss extends BaseUpload return $this->handle; } - public function listbuckets(string $region, bool $line = false, bool $shared = false) + public function listbuckets(string $region = null, bool $line = false, bool $shared = false) { try { - $this->storageRegion = $region; $res = $this->app()->listBuckets(); return $res['Buckets']['Bucket'] ?? []; } catch (\Throwable $e) { @@ -163,79 +164,156 @@ class Tyoss extends BaseUpload $app = $this->app(); //创建桶 try { - $res = $app->createBucket($name, $region, $acl); + $app->createBucket($name, $region, $acl); + $data = [ + 'Statement' => [ + 'Sid' => '公共读' . $name, + 'Effect' => 'Allow', + 'Principal' => [ + 'ID' => ['*'] + ], + 'Action' => ['HeadBucket', 'GetBucketLocation', 'ListBucketVersions', 'GetObject', 'RestoreObject', 'GetObjectVersion'], + 'Resource' => [$name, $name . '/*'] + ] + ]; + + $app->putPolicy($name, $region, $data); } catch (\Throwable $e) { return $this->setError('COS:' . $e->getMessage()); } - return $res; + return true; } public function getRegion() { return [ [ - 'value' => 'oos-hazz', + 'value' => 'cn-gz1', + 'label' => '贵州' + ], + [ + 'value' => 'cn-fz1', + 'label' => '福州' + ], + [ + 'value' => 'cn-hz1', + 'label' => '杭州' + ], + [ + 'value' => 'cn-sz1', + 'label' => '深圳' + ], + [ + 'value' => 'cn-gdgz1', + 'label' => '广州' + ], + [ + 'value' => 'cn-jssz1', + 'label' => '苏州' + ], + [ + 'value' => 'cn-sh1', + 'label' => '上海' + ], + [ + 'value' => 'cn-ahwh1', + 'label' => '芜湖' + ], + [ + 'value' => 'cn-bj1', + 'label' => '北京' + ], + [ + 'value' => 'cn-sccd1', + 'label' => '成都' + ], + [ + 'value' => 'cn-hazz1', 'label' => '郑州' ], [ - 'value' => 'oos-lnsy', - 'label' => '沈阳' + 'value' => 'cn-hncs1', + 'label' => '长沙' ], [ - 'value' => 'oos-sccd', - 'label' => '四川成都' + 'value' => 'cn-gxnn1', + 'label' => '南宁' ], [ - 'value' => 'oos-xjwlmq', - 'label' => '乌鲁木齐' + 'value' => 'cn-jxnc1', + 'label' => '南昌' ], [ - 'value' => 'oos-xjwlmq', - 'label' => '乌鲁木齐' - ], - [ - 'value' => 'oos-gslz', - 'label' => '甘肃兰州' - ], - [ - 'value' => 'oos-sdqd', - 'label' => '山东青岛' - ], - [ - 'value' => 'oos-gzgy', - 'label' => '贵州贵阳' - ], - [ - 'value' => 'oos-hbwh', - 'label' => '湖北武汉' - ], - [ - 'value' => 'oos-xzls', - 'label' => '西藏拉萨' - ], - [ - 'value' => 'oos-hbwh', - 'label' => '湖北武汉' - ], - [ - 'value' => 'oos-ahwh', - 'label' => '安徽芜湖' - ], - [ - 'value' => 'oos-gdsz', - 'label' => '广东深圳' - ], - [ - 'value' => 'oos-jssz', - 'label' => '江苏苏州' - ], - [ - 'value' => 'oos-sh2', - 'label' => '上海2' + 'value' => 'cn-sdqd1', + 'label' => '青岛' ], [ 'value' => 'cn-snxy1', - 'label' => '西安2' + 'label' => '咸阳' + ], + [ + 'value' => 'cn-xjcj1', + 'label' => '新疆' + ], + [ + 'value' => 'cn-ynkm1', + 'label' => '昆明' + ], + [ + 'value' => 'cn-hihk1', + 'label' => '海口' + ], + [ + 'value' => 'cn-hbwh1', + 'label' => '武汉' + ], + [ + 'value' => 'cn-cq1', + 'label' => '重庆' + ], + [ + 'value' => 'cn-qhxn1', + 'label' => '西宁' + ], + [ + 'value' => 'cn-gslz1', + 'label' => '兰州' + ], + [ + 'value' => 'cn-nxyc1', + 'label' => '银川' + ], + [ + 'value' => 'cn-sxty1', + 'label' => '太原' + ], + [ + 'value' => 'cn-hesjz1', + 'label' => '石家庄' + ], + [ + 'value' => 'cn-tj1', + 'label' => '天津' + ], + [ + 'value' => 'cn-jlcc1', + 'label' => '长春' + ], + [ + 'value' => 'cn-hlhrb1', + 'label' => '哈尔滨' + ], + [ + 'value' => 'cn-nmhh1', + 'label' => '内蒙古' + ], + [ + 'value' => 'cn-lnsy1', + 'label' => '沈阳' + ], + [ + 'value' => 'cn-north1', + 'label' => '华北' ] ]; } @@ -250,20 +328,44 @@ class Tyoss extends BaseUpload } } + public function getDomian($name, $region) + { + try { + $res = $this->app()->GetBucketDomain($name, $region); + if ($res) { + $domainRules = $res->toArray()['ListBucketCustomDomainsResult']; + return array_column($domainRules, 'DomainName'); + } else { + return []; + } + + } catch (\Throwable $e) { + } + return []; + } + public function bindDomian(string $name, string $domain, string $region = null) { - // TODO: Implement bindDomian() method. + $parseDomin = parse_url($domain); + try { + $this->app()->putBucketDomain($name, $region, [ + 'domainname' => $parseDomin['host'], + ]); + return true; + } catch (\Throwable $e) { + return $this->setError($e->getMessage()); + } } public function setBucketCors(string $name, string $region) { try { - $res = $this->app()->PutBucketCors($name, $region, [ + $this->app()->PutBucketCors($name, $region, [ 'AllowedHeaders' => ['*'], 'AllowedMethods' => ['PUT', 'GET', 'POST', 'DELETE', 'HEAD'], 'AllowedOrigins' => ['*'], 'ExposeHeaders' => ['ETag', 'Content-Length', 'x-cos-request-id'], - 'MaxAgeSeconds' => 12 + 'MaxAgeSeconds' => 100 ]); return true; } catch (\Throwable $e) {