123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- namespace Aws\S3;
- use Aws\CacheInterface;
- use Aws\CommandInterface;
- use Aws\LruArrayCache;
- use Aws\MultiRegionClient as BaseClient;
- use Aws\Exception\AwsException;
- use Aws\S3\Exception\PermanentRedirectException;
- use GuzzleHttp\Promise;
- class S3MultiRegionClient extends BaseClient implements S3ClientInterface
- {
- use S3ClientTrait;
-
- private $cache;
- public static function getArguments()
- {
- $args = parent::getArguments();
- $regionDef = $args['region'] + ['default' => function (array &$args) {
- $availableRegions = array_keys($args['partition']['regions']);
- return end($availableRegions);
- }];
- unset($args['region']);
- return $args + [
- 'bucket_region_cache' => [
- 'type' => 'config',
- 'valid' => [CacheInterface::class],
- 'doc' => 'Cache of regions in which given buckets are located.',
- 'default' => function () { return new LruArrayCache; },
- ],
- 'region' => $regionDef,
- ];
- }
- public function __construct(array $args)
- {
- parent::__construct($args);
- $this->cache = $this->getConfig('bucket_region_cache');
- $this->getHandlerList()->prependInit(
- $this->determineRegionMiddleware(),
- 'determine_region'
- );
- }
- private function determineRegionMiddleware()
- {
- return function (callable $handler) {
- return function (CommandInterface $command) use ($handler) {
- $cacheKey = $this->getCacheKey($command['Bucket']);
- if (
- empty($command['@region']) &&
- $region = $this->cache->get($cacheKey)
- ) {
- $command['@region'] = $region;
- }
- return Promise\Coroutine::of(function () use (
- $handler,
- $command,
- $cacheKey
- ) {
- try {
- yield $handler($command);
- } catch (PermanentRedirectException $e) {
- if (empty($command['Bucket'])) {
- throw $e;
- }
- $result = $e->getResult();
- $region = null;
- if (isset($result['@metadata']['headers']['x-amz-bucket-region'])) {
- $region = $result['@metadata']['headers']['x-amz-bucket-region'];
- $this->cache->set($cacheKey, $region);
- } else {
- $region = (yield $this->determineBucketRegionAsync(
- $command['Bucket']
- ));
- }
- $command['@region'] = $region;
- yield $handler($command);
- } catch (AwsException $e) {
- if ($e->getAwsErrorCode() === 'AuthorizationHeaderMalformed') {
- $region = $this->determineBucketRegionFromExceptionBody(
- $e->getResponse()
- );
- if (!empty($region)) {
- $this->cache->set($cacheKey, $region);
- $command['@region'] = $region;
- yield $handler($command);
- } else {
- throw $e;
- }
- } else {
- throw $e;
- }
- }
- });
- };
- };
- }
- public function createPresignedRequest(CommandInterface $command, $expires, array $options = [])
- {
- if (empty($command['Bucket'])) {
- throw new \InvalidArgumentException('The S3\\MultiRegionClient'
- . ' cannot create presigned requests for commands without a'
- . ' specified bucket.');
- }
-
- $client = $this->getClientFromPool(
- $this->determineBucketRegion($command['Bucket'])
- );
- return $client->createPresignedRequest(
- $client->getCommand($command->getName(), $command->toArray()),
- $expires
- );
- }
- public function getObjectUrl($bucket, $key)
- {
-
- $regionalClient = $this->getClientFromPool(
- $this->determineBucketRegion($bucket)
- );
- return $regionalClient->getObjectUrl($bucket, $key);
- }
- public function determineBucketRegionAsync($bucketName)
- {
- $cacheKey = $this->getCacheKey($bucketName);
- if ($cached = $this->cache->get($cacheKey)) {
- return Promise\Create::promiseFor($cached);
- }
-
- $regionalClient = $this->getClientFromPool();
- return $regionalClient->determineBucketRegionAsync($bucketName)
- ->then(
- function ($region) use ($cacheKey) {
- $this->cache->set($cacheKey, $region);
- return $region;
- }
- );
- }
- private function getCacheKey($bucketName)
- {
- return "aws:s3:{$bucketName}:location";
- }
- }
|