Skip to content

Commit

Permalink
further S3 compat updates (#2843)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamRemis authored Nov 28, 2023
1 parent b1b5e50 commit 1e66819
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/Identity/S3/S3ExpressIdentityProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,26 @@ class S3ExpressIdentityProvider
{

private $cache;
private $region;
private $config;
private $s3Client;
public function __construct($clientRegion, array $config = [])
{
$this->cache = new LruArrayCache(100);
$this->s3Client = isset($config['client'])
? $config['client'] // internal use only
: new Aws\S3\S3Client([
'region' => $clientRegion,
'disable_express_session_auth' => true
]);
$this->region = $clientRegion;
$this->config = $config;
}

public function __invoke($command)
{
$s3Client = $this->getS3Client();
$bucket = $command['Bucket'];
if ($identity = $this->cache->get($bucket)) {
if (!$identity->isExpired()) {
return Promise\Create::promiseFor($identity);
}
}
$response = $this->s3Client->createSession(['Bucket' => $bucket]);
$response = $s3Client->createSession(['Bucket' => $bucket]);
$identity = new Aws\Identity\S3\S3ExpressIdentity(
$response['Credentials']['AccessKeyId'],
$response['Credentials']['SecretAccessKey'],
Expand All @@ -39,4 +38,17 @@ public function __invoke($command)
$this->cache->set($bucket, $identity);
return Promise\Create::promiseFor($identity);
}

private function getS3Client()
{
if (is_null($this->s3Client)) {
$this->s3Client = isset($this->config['client'])
? $this->config['client'] // internal use only
: new Aws\S3\S3Client([
'region' => $this->region,
'disable_express_session_auth' => true
]);
}
return $this->s3Client;
}
}
4 changes: 3 additions & 1 deletion tests/Identity/S3Express/S3ExpressIdentityProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class S3ExpressIdentityProviderTest extends TestCase

const S3_TIMESTAMP_FORMAT = 'Y-m-d\TG:i:s\Z';

private function getCredentialResultFromTimestamp($timestamp) {
private function getCredentialResultFromTimestamp($timestamp)
{
$expiration = date(self::S3_TIMESTAMP_FORMAT, $timestamp);
return new Result([
'Credentials' => [
Expand All @@ -33,6 +34,7 @@ private function getCredentialResultFromTimestamp($timestamp) {
]
]);
}

public function testProvidesIdentity()
{
$expiration = time() + 5000;
Expand Down

0 comments on commit 1e66819

Please sign in to comment.