From 5af2bd060e8f3280c6b31f597848b615981519f2 Mon Sep 17 00:00:00 2001 From: Marton Schneider Date: Sat, 18 Nov 2023 13:13:38 +0100 Subject: [PATCH 1/2] optionally can define endpoint URL --- src/Storage/Device/S3.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 79aadeff..0c25762f 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -148,7 +148,7 @@ class S3 extends Device * @param string $region * @param string $acl */ - public function __construct(string $root, string $accessKey, string $secretKey, string $bucket, string $region = self::US_EAST_1, string $acl = self::ACL_PRIVATE) + public function __construct(string $root, string $accessKey, string $secretKey, string $bucket, string $region = self::US_EAST_1, string $acl = self::ACL_PRIVATE, $endpointUrl = '') { $this->accessKey = $accessKey; $this->secretKey = $secretKey; @@ -158,10 +158,14 @@ public function __construct(string $root, string $accessKey, string $secretKey, $this->acl = $acl; $this->amzHeaders = []; - $host = match ($region) { - self::CN_NORTH_1, self::CN_NORTH_4, self::CN_NORTHWEST_1 => $bucket.'.s3.'.$region.'.amazonaws.cn', - default => $bucket.'.s3.'.$region.'.amazonaws.com' - }; + if (!empty($endpointUrl)) { + $host = $bucket.'.'.$endpointUrl; + } else { + $host = match ($region) { + self::CN_NORTH_1, self::CN_NORTH_4, self::CN_NORTHWEST_1 => $bucket.'.s3.'.$region.'.amazonaws.cn', + default => $bucket.'.s3.'.$region.'.amazonaws.com' + }; + } $this->headers['host'] = $host; } From 73f63443cfed5d9d5fe79c5ab0793220347399a4 Mon Sep 17 00:00:00 2001 From: Marton Schneider Date: Thu, 23 Nov 2023 18:49:39 +0100 Subject: [PATCH 2/2] fix lint errors --- src/Storage/Device/S3.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 0c25762f..f3b5c451 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -128,7 +128,8 @@ class S3 extends Device * @var array */ protected array $headers = [ - 'host' => '', 'date' => '', + 'host' => '', + 'date' => '', 'content-md5' => '', 'content-type' => '', ]; @@ -158,12 +159,12 @@ public function __construct(string $root, string $accessKey, string $secretKey, $this->acl = $acl; $this->amzHeaders = []; - if (!empty($endpointUrl)) { + if (! empty($endpointUrl)) { $host = $bucket.'.'.$endpointUrl; } else { $host = match ($region) { - self::CN_NORTH_1, self::CN_NORTH_4, self::CN_NORTHWEST_1 => $bucket.'.s3.'.$region.'.amazonaws.cn', - default => $bucket.'.s3.'.$region.'.amazonaws.com' + self::CN_NORTH_1, self::CN_NORTH_4, self::CN_NORTHWEST_1 => $bucket.'.s3.'.$region.'.amazonaws.cn', + default => $bucket.'.s3.'.$region.'.amazonaws.com' }; } @@ -769,8 +770,10 @@ private function getSignatureV4(string $method, string $uri, array $parameters = // stringToSign $stringToSignStr = \implode("\n", [ - $algorithm, $this->amzHeaders['x-amz-date'], - \implode('/', $credentialScope), \hash('sha256', $amzPayloadStr), + $algorithm, + $this->amzHeaders['x-amz-date'], + \implode('/', $credentialScope), + \hash('sha256', $amzPayloadStr), ]); // Make Signature