diff --git a/crmeb/crmeb/services/upload/extend/jdoss/Client.php b/crmeb/crmeb/services/upload/extend/jdoss/Client.php index 12b7f1746..cf1cdd336 100644 --- a/crmeb/crmeb/services/upload/extend/jdoss/Client.php +++ b/crmeb/crmeb/services/upload/extend/jdoss/Client.php @@ -14,6 +14,7 @@ namespace crmeb\services\upload\extend\jdoss; +use crmeb\exceptions\UploadException; use crmeb\services\upload\BaseClient; /** @@ -30,7 +31,7 @@ class Client extends BaseClient * AK * @var */ - protected $accessKeyId = 'FHZVOVXFEMUGWOVARS8H'; + protected $accessKeyId; /** * SK @@ -76,6 +77,27 @@ class Client extends BaseClient $this->uploadUrl = $config['uploadUrl'] ?? ''; } + /** + * 检测桶,不存在返回true + * @param string $bucket + * @param string $region + * @return array|bool|\crmeb\services\upload\extend\cos\SimpleXMLElement + * @author 等风来 + * @email 136327134@qq.com + * @date 2022/10/17 + */ + public function headBucket(string $bucket, string $region = '') + { + $url = $this->getRequestUrl($bucket, $region); + + $header = [ + 'Host' => $url + ]; + + return $this->request('https://' . $url, 'head', [], $header); + } + + /** * 获取桶列表 * @return array|\crmeb\services\upload\extend\cos\SimpleXMLElement @@ -96,6 +118,22 @@ class Client extends BaseClient return $res; } + public function createBucket($name, $region, $acl) + { + $url = $this->getRequestUrl($name, $region); + $header = [ + 'Host' => $url, + 'Date' => gmdate('D, d M Y H:i:s \G\M\T'), + 'name' => $name, + 'x-amz-acl' => $acl + ]; + + + $res = $this->request('https://' . $url . '/', 'PUT', [], $header); + + return $res; + } + /** * 获取请求域名 * @param string $bucket @@ -107,6 +145,13 @@ class Client extends BaseClient */ protected function getRequestUrl(string $bucket = '', string $region = self::DEFAULT_REGION) { + if (!$this->accessKeyId) { + throw new UploadException('请传入SecretId'); + } + if (!$this->secretKey) { + throw new UploadException('请传入SecretKey'); + } + return ($bucket ? $bucket . '.' : '') . 's3.' . $region . '.jdcloud-oss.com'; } diff --git a/crmeb/crmeb/services/upload/storage/Jdoss.php b/crmeb/crmeb/services/upload/storage/Jdoss.php index 106085445..dddf086a3 100644 --- a/crmeb/crmeb/services/upload/storage/Jdoss.php +++ b/crmeb/crmeb/services/upload/storage/Jdoss.php @@ -2,9 +2,15 @@ namespace crmeb\services\upload\storage; +use Aws\S3\S3Client; +use crmeb\exceptions\UploadException; use crmeb\services\upload\BaseUpload; -use crmeb\services\upload\extend\jdoss\Client as CrmebClient; +/** + * 京东云COS文件上传 + * Class Jdoss + * @package crmeb\services\upload\storage + */ class Jdoss extends BaseUpload { @@ -29,7 +35,7 @@ class Jdoss extends BaseUpload /** * 句柄 - * @var CrmebClient + * @var S3Client */ protected $handle; @@ -90,17 +96,25 @@ class Jdoss extends BaseUpload } /** - * 实例化cos - * @return CrmebClient + * @return S3Client + * + * @date 2023/06/05 + * @author yyw */ protected function app() { - $this->handle = new CrmebClient([ - 'accessKey' => $this->accessKey, - 'secretKey' => $this->secretKey, + if (!$this->accessKey || !$this->secretKey) { + throw new UploadException(400721); + } + $this->handle = new S3Client([ + 'version' => 'latest', 'region' => $this->storageRegion, - 'bucket' => $this->storageName, - 'uploadUrl' => $this->uploadUrl + 'endpoint' => "http://s3.{$this->storageRegion}.jdcloud-oss.com", + 'signature_version' => 'v4', + 'credentials' => [ + 'key' => $this->accessKey, + 'secret' => $this->secretKey, + ], ]); return $this->handle; } @@ -131,9 +145,41 @@ class Jdoss extends BaseUpload } } - public function createBucket(string $name, string $region) + public function createBucket(string $name, string $region = '', string $acl = 'public-read') { - // TODO: Implement createBucket() method. + $regionData = $this->getRegion(); + $regionData = array_column($regionData, 'value'); + if (!in_array($region, $regionData)) { + return $this->setError('COS:无效的区域!'); + } + $this->storageRegion = $region; + $app = $this->app(); + //检测桶 + try { + $app->headBucket([ + 'Bucket' => $name + ]); + } catch (\Throwable $e) { + //桶不存在返回404 + if (strstr('404', $e->getMessage())) { + return $this->setError('COS:' . $e->getMessage()); + } + } + //创建桶 + try { + $res = $app->createBucket([ + 'Bucket' => $name, + 'ACL' => $acl, + ]); + } catch (\Throwable $e) { + if (strstr('[curl] 6', $e->getMessage())) { + return $this->setError('COS:无效的区域!!'); + } else if (strstr('Access Denied.', $e->getMessage())) { + return $this->setError('COS:无权访问'); + } + return $this->setError('COS:' . $e->getMessage()); + } + return $res; } public function getRegion()