S3ExpressIdentityProvider.php 1.5 KB

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