| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Identity\S3; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\LruArrayCache; |
| 7 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise; |
| 8 |
class S3ExpressIdentityProvider |
| 9 |
{ |
| 10 |
private $cache; |
| 11 |
private $region; |
| 12 |
private $config; |
| 13 |
private $s3Client; |
| 14 |
public function __construct($clientRegion, array $config = []) |
| 15 |
{ |
| 16 |
$this->cache = new LruArrayCache(100); |
| 17 |
$this->region = $clientRegion; |
| 18 |
$this->config = $config; |
| 19 |
} |
| 20 |
public function __invoke($command) |
| 21 |
{ |
| 22 |
$s3Client = $this->getS3Client(); |
| 23 |
$bucket = $command['Bucket']; |
| 24 |
if ($identity = $this->cache->get($bucket)) { |
| 25 |
if (!$identity->isExpired()) { |
| 26 |
return Promise\Create::promiseFor($identity); |
| 27 |
} |
| 28 |
} |
| 29 |
$response = $s3Client->createSession(['Bucket' => $bucket]); |
| 30 |
$identity = new Aws\Identity\S3\S3ExpressIdentity($response['Credentials']['AccessKeyId'], $response['Credentials']['SecretAccessKey'], $response['Credentials']['SessionToken'], $response['Credentials']['Expiration']->getTimestamp()); |
| 31 |
$this->cache->set($bucket, $identity); |
| 32 |
return Promise\Create::promiseFor($identity); |
| 33 |
} |
| 34 |
private function getS3Client() |
| 35 |
{ |
| 36 |
if (\is_null($this->s3Client)) { |
| 37 |
$this->s3Client = $this->config['client'] ?? new Aws\S3\S3Client(['region' => $this->region, 'disable_express_session_auth' => \true]); |
| 38 |
} |
| 39 |
return $this->s3Client; |
| 40 |
} |
| 41 |
} |
| 42 |
|