From 5af2bd060e8f3280c6b31f597848b615981519f2 Mon Sep 17 00:00:00 2001 From: Marton Schneider Date: Sat, 18 Nov 2023 13:13:38 +0100 Subject: [PATCH 1/4] 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/4] 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 From 9df598b91a0eb0a5900752a59d929ace584a591b Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 22 Jan 2024 07:42:29 +0000 Subject: [PATCH 3/4] update list objects to reset headers --- src/Storage/Device/S3.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index f3b5c451..927e6719 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -496,6 +496,8 @@ private function listObjects(string $prefix = '', int $maxKeys = self::MAX_PAGE_ $uri = '/'; $prefix = ltrim($prefix, '/'); /** S3 specific requirement that prefix should never contain a leading slash */ + unset($this->amzHeaders['x-amz-acl']); + unset($this->amzHeaders['x-amz-content-sha256']); $this->headers['content-type'] = 'text/plain'; $this->headers['content-md5'] = \base64_encode(md5('', true)); From 3ad32bd705c53e5eda33f80e70b8ea305fd9cd8e Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 2 Apr 2024 08:50:52 +0000 Subject: [PATCH 4/4] fix double --- src/Storage/Device/S3.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 3fb9f59d..c319bc57 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -525,8 +525,6 @@ protected function listObjects(string $prefix = '', int $maxKeys = self::MAX_PAG $uri = '/'; $prefix = ltrim($prefix, '/'); /** S3 specific requirement that prefix should never contain a leading slash */ - unset($this->amzHeaders['x-amz-acl']); - unset($this->amzHeaders['x-amz-content-sha256']); $this->headers['content-type'] = 'text/plain'; $this->headers['content-md5'] = \base64_encode(md5('', true));