Skip to content

Commit

Permalink
Merge pull request #103 from schneidermr/main
Browse files Browse the repository at this point in the history
optionally can define endpoint URL
  • Loading branch information
lohanidamodar authored Jan 22, 2024
2 parents faa0279 + 73f6344 commit c9138e5
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 @@ -128,7 +128,8 @@ class S3 extends Device
* @var array
*/
protected array $headers = [
'host' => '', 'date' => '',
'host' => '',
'date' => '',
'content-md5' => '',
'content-type' => '',
];
Expand All @@ -148,7 +149,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 @@ -158,10 +159,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 @@ -765,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
Expand Down

0 comments on commit c9138e5

Please sign in to comment.