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] =?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) {