Skip to content

Commit

Permalink
Merge pull request #106 from utopia-php/feat-generic-s3
Browse files Browse the repository at this point in the history
optionally can define endpoint URL
  • Loading branch information
eldadfux committed Apr 2, 2024
2 parents 94ab875 + 3ad32bd commit 9c89ea2
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/Storage/Device/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ class S3 extends Device
* @var array
*/
protected array $headers = [
'host' => '', 'date' => '',
'host' => '',
'date' => '',
'content-md5' => '',
'content-type' => '',
];
Expand All @@ -163,7 +164,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;
Expand All @@ -173,10 +174,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;
}
Expand Down Expand Up @@ -797,8 +802,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
Expand Down

0 comments on commit 9c89ea2

Please sign in to comment.