| 1 | <?php |
| 2 | namespace Aws\S3; |
| 3 | |
| 4 | use Aws\CacheInterface; |
| 5 | use Aws\CommandInterface; |
| 6 | use Aws\LruArrayCache; |
| 7 | use Aws\MultiRegionClient as BaseClient; |
| 8 | use Aws\Exception\AwsException; |
| 9 | use Aws\S3\Exception\PermanentRedirectException; |
| 10 | use GuzzleHttp\Promise; |
| 11 | |
| 12 | /** |
| 13 | * **Amazon Simple Storage Service** multi-region client. |
| 14 | * |
| 15 | * @method \Aws\Result abortMultipartUpload(array $args = []) |
| 16 | * @method \GuzzleHttp\Promise\Promise abortMultipartUploadAsync(array $args = []) |
| 17 | * @method \Aws\Result completeMultipartUpload(array $args = []) |
| 18 | * @method \GuzzleHttp\Promise\Promise completeMultipartUploadAsync(array $args = []) |
| 19 | * @method \Aws\Result copyObject(array $args = []) |
| 20 | * @method \GuzzleHttp\Promise\Promise copyObjectAsync(array $args = []) |
| 21 | * @method \Aws\Result createBucket(array $args = []) |
| 22 | * @method \GuzzleHttp\Promise\Promise createBucketAsync(array $args = []) |
| 23 | * @method \Aws\Result createBucketMetadataConfiguration(array $args = []) |
| 24 | * @method \GuzzleHttp\Promise\Promise createBucketMetadataConfigurationAsync(array $args = []) |
| 25 | * @method \Aws\Result createBucketMetadataTableConfiguration(array $args = []) |
| 26 | * @method \GuzzleHttp\Promise\Promise createBucketMetadataTableConfigurationAsync(array $args = []) |
| 27 | * @method \Aws\Result createMultipartUpload(array $args = []) |
| 28 | * @method \GuzzleHttp\Promise\Promise createMultipartUploadAsync(array $args = []) |
| 29 | * @method \Aws\Result createSession(array $args = []) |
| 30 | * @method \GuzzleHttp\Promise\Promise createSessionAsync(array $args = []) |
| 31 | * @method \Aws\Result deleteBucket(array $args = []) |
| 32 | * @method \GuzzleHttp\Promise\Promise deleteBucketAsync(array $args = []) |
| 33 | * @method \Aws\Result deleteBucketAnalyticsConfiguration(array $args = []) |
| 34 | * @method \GuzzleHttp\Promise\Promise deleteBucketAnalyticsConfigurationAsync(array $args = []) |
| 35 | * @method \Aws\Result deleteBucketCors(array $args = []) |
| 36 | * @method \GuzzleHttp\Promise\Promise deleteBucketCorsAsync(array $args = []) |
| 37 | * @method \Aws\Result deleteBucketEncryption(array $args = []) |
| 38 | * @method \GuzzleHttp\Promise\Promise deleteBucketEncryptionAsync(array $args = []) |
| 39 | * @method \Aws\Result deleteBucketIntelligentTieringConfiguration(array $args = []) |
| 40 | * @method \GuzzleHttp\Promise\Promise deleteBucketIntelligentTieringConfigurationAsync(array $args = []) |
| 41 | * @method \Aws\Result deleteBucketInventoryConfiguration(array $args = []) |
| 42 | * @method \GuzzleHttp\Promise\Promise deleteBucketInventoryConfigurationAsync(array $args = []) |
| 43 | * @method \Aws\Result deleteBucketLifecycle(array $args = []) |
| 44 | * @method \GuzzleHttp\Promise\Promise deleteBucketLifecycleAsync(array $args = []) |
| 45 | * @method \Aws\Result deleteBucketMetadataConfiguration(array $args = []) |
| 46 | * @method \GuzzleHttp\Promise\Promise deleteBucketMetadataConfigurationAsync(array $args = []) |
| 47 | * @method \Aws\Result deleteBucketMetadataTableConfiguration(array $args = []) |
| 48 | * @method \GuzzleHttp\Promise\Promise deleteBucketMetadataTableConfigurationAsync(array $args = []) |
| 49 | * @method \Aws\Result deleteBucketMetricsConfiguration(array $args = []) |
| 50 | * @method \GuzzleHttp\Promise\Promise deleteBucketMetricsConfigurationAsync(array $args = []) |
| 51 | * @method \Aws\Result deleteBucketOwnershipControls(array $args = []) |
| 52 | * @method \GuzzleHttp\Promise\Promise deleteBucketOwnershipControlsAsync(array $args = []) |
| 53 | * @method \Aws\Result deleteBucketPolicy(array $args = []) |
| 54 | * @method \GuzzleHttp\Promise\Promise deleteBucketPolicyAsync(array $args = []) |
| 55 | * @method \Aws\Result deleteBucketReplication(array $args = []) |
| 56 | * @method \GuzzleHttp\Promise\Promise deleteBucketReplicationAsync(array $args = []) |
| 57 | * @method \Aws\Result deleteBucketTagging(array $args = []) |
| 58 | * @method \GuzzleHttp\Promise\Promise deleteBucketTaggingAsync(array $args = []) |
| 59 | * @method \Aws\Result deleteBucketWebsite(array $args = []) |
| 60 | * @method \GuzzleHttp\Promise\Promise deleteBucketWebsiteAsync(array $args = []) |
| 61 | * @method \Aws\Result deleteObject(array $args = []) |
| 62 | * @method \GuzzleHttp\Promise\Promise deleteObjectAsync(array $args = []) |
| 63 | * @method \Aws\Result deleteObjectTagging(array $args = []) |
| 64 | * @method \GuzzleHttp\Promise\Promise deleteObjectTaggingAsync(array $args = []) |
| 65 | * @method \Aws\Result deleteObjects(array $args = []) |
| 66 | * @method \GuzzleHttp\Promise\Promise deleteObjectsAsync(array $args = []) |
| 67 | * @method \Aws\Result deletePublicAccessBlock(array $args = []) |
| 68 | * @method \GuzzleHttp\Promise\Promise deletePublicAccessBlockAsync(array $args = []) |
| 69 | * @method \Aws\Result getBucketAbac(array $args = []) |
| 70 | * @method \GuzzleHttp\Promise\Promise getBucketAbacAsync(array $args = []) |
| 71 | * @method \Aws\Result getBucketAccelerateConfiguration(array $args = []) |
| 72 | * @method \GuzzleHttp\Promise\Promise getBucketAccelerateConfigurationAsync(array $args = []) |
| 73 | * @method \Aws\Result getBucketAcl(array $args = []) |
| 74 | * @method \GuzzleHttp\Promise\Promise getBucketAclAsync(array $args = []) |
| 75 | * @method \Aws\Result getBucketAnalyticsConfiguration(array $args = []) |
| 76 | * @method \GuzzleHttp\Promise\Promise getBucketAnalyticsConfigurationAsync(array $args = []) |
| 77 | * @method \Aws\Result getBucketCors(array $args = []) |
| 78 | * @method \GuzzleHttp\Promise\Promise getBucketCorsAsync(array $args = []) |
| 79 | * @method \Aws\Result getBucketEncryption(array $args = []) |
| 80 | * @method \GuzzleHttp\Promise\Promise getBucketEncryptionAsync(array $args = []) |
| 81 | * @method \Aws\Result getBucketIntelligentTieringConfiguration(array $args = []) |
| 82 | * @method \GuzzleHttp\Promise\Promise getBucketIntelligentTieringConfigurationAsync(array $args = []) |
| 83 | * @method \Aws\Result getBucketInventoryConfiguration(array $args = []) |
| 84 | * @method \GuzzleHttp\Promise\Promise getBucketInventoryConfigurationAsync(array $args = []) |
| 85 | * @method \Aws\Result getBucketLifecycle(array $args = []) |
| 86 | * @method \GuzzleHttp\Promise\Promise getBucketLifecycleAsync(array $args = []) |
| 87 | * @method \Aws\Result getBucketLifecycleConfiguration(array $args = []) |
| 88 | * @method \GuzzleHttp\Promise\Promise getBucketLifecycleConfigurationAsync(array $args = []) |
| 89 | * @method \Aws\Result getBucketLocation(array $args = []) |
| 90 | * @method \GuzzleHttp\Promise\Promise getBucketLocationAsync(array $args = []) |
| 91 | * @method \Aws\Result getBucketLogging(array $args = []) |
| 92 | * @method \GuzzleHttp\Promise\Promise getBucketLoggingAsync(array $args = []) |
| 93 | * @method \Aws\Result getBucketMetadataConfiguration(array $args = []) |
| 94 | * @method \GuzzleHttp\Promise\Promise getBucketMetadataConfigurationAsync(array $args = []) |
| 95 | * @method \Aws\Result getBucketMetadataTableConfiguration(array $args = []) |
| 96 | * @method \GuzzleHttp\Promise\Promise getBucketMetadataTableConfigurationAsync(array $args = []) |
| 97 | * @method \Aws\Result getBucketMetricsConfiguration(array $args = []) |
| 98 | * @method \GuzzleHttp\Promise\Promise getBucketMetricsConfigurationAsync(array $args = []) |
| 99 | * @method \Aws\Result getBucketNotification(array $args = []) |
| 100 | * @method \GuzzleHttp\Promise\Promise getBucketNotificationAsync(array $args = []) |
| 101 | * @method \Aws\Result getBucketNotificationConfiguration(array $args = []) |
| 102 | * @method \GuzzleHttp\Promise\Promise getBucketNotificationConfigurationAsync(array $args = []) |
| 103 | * @method \Aws\Result getBucketOwnershipControls(array $args = []) |
| 104 | * @method \GuzzleHttp\Promise\Promise getBucketOwnershipControlsAsync(array $args = []) |
| 105 | * @method \Aws\Result getBucketPolicy(array $args = []) |
| 106 | * @method \GuzzleHttp\Promise\Promise getBucketPolicyAsync(array $args = []) |
| 107 | * @method \Aws\Result getBucketPolicyStatus(array $args = []) |
| 108 | * @method \GuzzleHttp\Promise\Promise getBucketPolicyStatusAsync(array $args = []) |
| 109 | * @method \Aws\Result getBucketReplication(array $args = []) |
| 110 | * @method \GuzzleHttp\Promise\Promise getBucketReplicationAsync(array $args = []) |
| 111 | * @method \Aws\Result getBucketRequestPayment(array $args = []) |
| 112 | * @method \GuzzleHttp\Promise\Promise getBucketRequestPaymentAsync(array $args = []) |
| 113 | * @method \Aws\Result getBucketTagging(array $args = []) |
| 114 | * @method \GuzzleHttp\Promise\Promise getBucketTaggingAsync(array $args = []) |
| 115 | * @method \Aws\Result getBucketVersioning(array $args = []) |
| 116 | * @method \GuzzleHttp\Promise\Promise getBucketVersioningAsync(array $args = []) |
| 117 | * @method \Aws\Result getBucketWebsite(array $args = []) |
| 118 | * @method \GuzzleHttp\Promise\Promise getBucketWebsiteAsync(array $args = []) |
| 119 | * @method \Aws\Result getObject(array $args = []) |
| 120 | * @method \GuzzleHttp\Promise\Promise getObjectAsync(array $args = []) |
| 121 | * @method \Aws\Result getObjectAcl(array $args = []) |
| 122 | * @method \GuzzleHttp\Promise\Promise getObjectAclAsync(array $args = []) |
| 123 | * @method \Aws\Result getObjectAttributes(array $args = []) |
| 124 | * @method \GuzzleHttp\Promise\Promise getObjectAttributesAsync(array $args = []) |
| 125 | * @method \Aws\Result getObjectLegalHold(array $args = []) |
| 126 | * @method \GuzzleHttp\Promise\Promise getObjectLegalHoldAsync(array $args = []) |
| 127 | * @method \Aws\Result getObjectLockConfiguration(array $args = []) |
| 128 | * @method \GuzzleHttp\Promise\Promise getObjectLockConfigurationAsync(array $args = []) |
| 129 | * @method \Aws\Result getObjectRetention(array $args = []) |
| 130 | * @method \GuzzleHttp\Promise\Promise getObjectRetentionAsync(array $args = []) |
| 131 | * @method \Aws\Result getObjectTagging(array $args = []) |
| 132 | * @method \GuzzleHttp\Promise\Promise getObjectTaggingAsync(array $args = []) |
| 133 | * @method \Aws\Result getObjectTorrent(array $args = []) |
| 134 | * @method \GuzzleHttp\Promise\Promise getObjectTorrentAsync(array $args = []) |
| 135 | * @method \Aws\Result getPublicAccessBlock(array $args = []) |
| 136 | * @method \GuzzleHttp\Promise\Promise getPublicAccessBlockAsync(array $args = []) |
| 137 | * @method \Aws\Result headBucket(array $args = []) |
| 138 | * @method \GuzzleHttp\Promise\Promise headBucketAsync(array $args = []) |
| 139 | * @method \Aws\Result headObject(array $args = []) |
| 140 | * @method \GuzzleHttp\Promise\Promise headObjectAsync(array $args = []) |
| 141 | * @method \Aws\Result listBucketAnalyticsConfigurations(array $args = []) |
| 142 | * @method \GuzzleHttp\Promise\Promise listBucketAnalyticsConfigurationsAsync(array $args = []) |
| 143 | * @method \Aws\Result listBucketIntelligentTieringConfigurations(array $args = []) |
| 144 | * @method \GuzzleHttp\Promise\Promise listBucketIntelligentTieringConfigurationsAsync(array $args = []) |
| 145 | * @method \Aws\Result listBucketInventoryConfigurations(array $args = []) |
| 146 | * @method \GuzzleHttp\Promise\Promise listBucketInventoryConfigurationsAsync(array $args = []) |
| 147 | * @method \Aws\Result listBucketMetricsConfigurations(array $args = []) |
| 148 | * @method \GuzzleHttp\Promise\Promise listBucketMetricsConfigurationsAsync(array $args = []) |
| 149 | * @method \Aws\Result listBuckets(array $args = []) |
| 150 | * @method \GuzzleHttp\Promise\Promise listBucketsAsync(array $args = []) |
| 151 | * @method \Aws\Result listDirectoryBuckets(array $args = []) |
| 152 | * @method \GuzzleHttp\Promise\Promise listDirectoryBucketsAsync(array $args = []) |
| 153 | * @method \Aws\Result listMultipartUploads(array $args = []) |
| 154 | * @method \GuzzleHttp\Promise\Promise listMultipartUploadsAsync(array $args = []) |
| 155 | * @method \Aws\Result listObjectVersions(array $args = []) |
| 156 | * @method \GuzzleHttp\Promise\Promise listObjectVersionsAsync(array $args = []) |
| 157 | * @method \Aws\Result listObjects(array $args = []) |
| 158 | * @method \GuzzleHttp\Promise\Promise listObjectsAsync(array $args = []) |
| 159 | * @method \Aws\Result listObjectsV2(array $args = []) |
| 160 | * @method \GuzzleHttp\Promise\Promise listObjectsV2Async(array $args = []) |
| 161 | * @method \Aws\Result listParts(array $args = []) |
| 162 | * @method \GuzzleHttp\Promise\Promise listPartsAsync(array $args = []) |
| 163 | * @method \Aws\Result putBucketAbac(array $args = []) |
| 164 | * @method \GuzzleHttp\Promise\Promise putBucketAbacAsync(array $args = []) |
| 165 | * @method \Aws\Result putBucketAccelerateConfiguration(array $args = []) |
| 166 | * @method \GuzzleHttp\Promise\Promise putBucketAccelerateConfigurationAsync(array $args = []) |
| 167 | * @method \Aws\Result putBucketAcl(array $args = []) |
| 168 | * @method \GuzzleHttp\Promise\Promise putBucketAclAsync(array $args = []) |
| 169 | * @method \Aws\Result putBucketAnalyticsConfiguration(array $args = []) |
| 170 | * @method \GuzzleHttp\Promise\Promise putBucketAnalyticsConfigurationAsync(array $args = []) |
| 171 | * @method \Aws\Result putBucketCors(array $args = []) |
| 172 | * @method \GuzzleHttp\Promise\Promise putBucketCorsAsync(array $args = []) |
| 173 | * @method \Aws\Result putBucketEncryption(array $args = []) |
| 174 | * @method \GuzzleHttp\Promise\Promise putBucketEncryptionAsync(array $args = []) |
| 175 | * @method \Aws\Result putBucketIntelligentTieringConfiguration(array $args = []) |
| 176 | * @method \GuzzleHttp\Promise\Promise putBucketIntelligentTieringConfigurationAsync(array $args = []) |
| 177 | * @method \Aws\Result putBucketInventoryConfiguration(array $args = []) |
| 178 | * @method \GuzzleHttp\Promise\Promise putBucketInventoryConfigurationAsync(array $args = []) |
| 179 | * @method \Aws\Result putBucketLifecycle(array $args = []) |
| 180 | * @method \GuzzleHttp\Promise\Promise putBucketLifecycleAsync(array $args = []) |
| 181 | * @method \Aws\Result putBucketLifecycleConfiguration(array $args = []) |
| 182 | * @method \GuzzleHttp\Promise\Promise putBucketLifecycleConfigurationAsync(array $args = []) |
| 183 | * @method \Aws\Result putBucketLogging(array $args = []) |
| 184 | * @method \GuzzleHttp\Promise\Promise putBucketLoggingAsync(array $args = []) |
| 185 | * @method \Aws\Result putBucketMetricsConfiguration(array $args = []) |
| 186 | * @method \GuzzleHttp\Promise\Promise putBucketMetricsConfigurationAsync(array $args = []) |
| 187 | * @method \Aws\Result putBucketNotification(array $args = []) |
| 188 | * @method \GuzzleHttp\Promise\Promise putBucketNotificationAsync(array $args = []) |
| 189 | * @method \Aws\Result putBucketNotificationConfiguration(array $args = []) |
| 190 | * @method \GuzzleHttp\Promise\Promise putBucketNotificationConfigurationAsync(array $args = []) |
| 191 | * @method \Aws\Result putBucketOwnershipControls(array $args = []) |
| 192 | * @method \GuzzleHttp\Promise\Promise putBucketOwnershipControlsAsync(array $args = []) |
| 193 | * @method \Aws\Result putBucketPolicy(array $args = []) |
| 194 | * @method \GuzzleHttp\Promise\Promise putBucketPolicyAsync(array $args = []) |
| 195 | * @method \Aws\Result putBucketReplication(array $args = []) |
| 196 | * @method \GuzzleHttp\Promise\Promise putBucketReplicationAsync(array $args = []) |
| 197 | * @method \Aws\Result putBucketRequestPayment(array $args = []) |
| 198 | * @method \GuzzleHttp\Promise\Promise putBucketRequestPaymentAsync(array $args = []) |
| 199 | * @method \Aws\Result putBucketTagging(array $args = []) |
| 200 | * @method \GuzzleHttp\Promise\Promise putBucketTaggingAsync(array $args = []) |
| 201 | * @method \Aws\Result putBucketVersioning(array $args = []) |
| 202 | * @method \GuzzleHttp\Promise\Promise putBucketVersioningAsync(array $args = []) |
| 203 | * @method \Aws\Result putBucketWebsite(array $args = []) |
| 204 | * @method \GuzzleHttp\Promise\Promise putBucketWebsiteAsync(array $args = []) |
| 205 | * @method \Aws\Result putObject(array $args = []) |
| 206 | * @method \GuzzleHttp\Promise\Promise putObjectAsync(array $args = []) |
| 207 | * @method \Aws\Result putObjectAcl(array $args = []) |
| 208 | * @method \GuzzleHttp\Promise\Promise putObjectAclAsync(array $args = []) |
| 209 | * @method \Aws\Result putObjectLegalHold(array $args = []) |
| 210 | * @method \GuzzleHttp\Promise\Promise putObjectLegalHoldAsync(array $args = []) |
| 211 | * @method \Aws\Result putObjectLockConfiguration(array $args = []) |
| 212 | * @method \GuzzleHttp\Promise\Promise putObjectLockConfigurationAsync(array $args = []) |
| 213 | * @method \Aws\Result putObjectRetention(array $args = []) |
| 214 | * @method \GuzzleHttp\Promise\Promise putObjectRetentionAsync(array $args = []) |
| 215 | * @method \Aws\Result putObjectTagging(array $args = []) |
| 216 | * @method \GuzzleHttp\Promise\Promise putObjectTaggingAsync(array $args = []) |
| 217 | * @method \Aws\Result putPublicAccessBlock(array $args = []) |
| 218 | * @method \GuzzleHttp\Promise\Promise putPublicAccessBlockAsync(array $args = []) |
| 219 | * @method \Aws\Result renameObject(array $args = []) |
| 220 | * @method \GuzzleHttp\Promise\Promise renameObjectAsync(array $args = []) |
| 221 | * @method \Aws\Result restoreObject(array $args = []) |
| 222 | * @method \GuzzleHttp\Promise\Promise restoreObjectAsync(array $args = []) |
| 223 | * @method \Aws\Result selectObjectContent(array $args = []) |
| 224 | * @method \GuzzleHttp\Promise\Promise selectObjectContentAsync(array $args = []) |
| 225 | * @method \Aws\Result updateBucketMetadataInventoryTableConfiguration(array $args = []) |
| 226 | * @method \GuzzleHttp\Promise\Promise updateBucketMetadataInventoryTableConfigurationAsync(array $args = []) |
| 227 | * @method \Aws\Result updateBucketMetadataJournalTableConfiguration(array $args = []) |
| 228 | * @method \GuzzleHttp\Promise\Promise updateBucketMetadataJournalTableConfigurationAsync(array $args = []) |
| 229 | * @method \Aws\Result updateObjectEncryption(array $args = []) |
| 230 | * @method \GuzzleHttp\Promise\Promise updateObjectEncryptionAsync(array $args = []) |
| 231 | * @method \Aws\Result uploadPart(array $args = []) |
| 232 | * @method \GuzzleHttp\Promise\Promise uploadPartAsync(array $args = []) |
| 233 | * @method \Aws\Result uploadPartCopy(array $args = []) |
| 234 | * @method \GuzzleHttp\Promise\Promise uploadPartCopyAsync(array $args = []) |
| 235 | * @method \Aws\Result writeGetObjectResponse(array $args = []) |
| 236 | * @method \GuzzleHttp\Promise\Promise writeGetObjectResponseAsync(array $args = []) |
| 237 | */ |
| 238 | class S3MultiRegionClient extends BaseClient implements S3ClientInterface |
| 239 | { |
| 240 | use S3ClientTrait; |
| 241 | |
| 242 | /** @var CacheInterface */ |
| 243 | private $cache; |
| 244 | |
| 245 | public static function getArguments() |
| 246 | { |
| 247 | $args = parent::getArguments(); |
| 248 | $regionDef = $args['region'] + ['default' => function (array &$args) { |
| 249 | $availableRegions = array_keys($args['partition']['regions']); |
| 250 | return end($availableRegions); |
| 251 | }]; |
| 252 | unset($args['region']); |
| 253 | |
| 254 | return $args + [ |
| 255 | 'bucket_region_cache' => [ |
| 256 | 'type' => 'config', |
| 257 | 'valid' => [CacheInterface::class], |
| 258 | 'doc' => 'Cache of regions in which given buckets are located.', |
| 259 | 'default' => function () { return new LruArrayCache; }, |
| 260 | ], |
| 261 | 'region' => $regionDef, |
| 262 | ]; |
| 263 | } |
| 264 | |
| 265 | public function __construct(array $args) |
| 266 | { |
| 267 | parent::__construct($args); |
| 268 | $this->cache = $this->getConfig('bucket_region_cache'); |
| 269 | |
| 270 | $this->getHandlerList()->prependInit( |
| 271 | $this->determineRegionMiddleware(), |
| 272 | 'determine_region' |
| 273 | ); |
| 274 | } |
| 275 | |
| 276 | private function determineRegionMiddleware() |
| 277 | { |
| 278 | $clientRef = \WeakReference::create($this); |
| 279 | return static function (callable $handler) use ($clientRef) { |
| 280 | return static function (CommandInterface $command) use ($handler, $clientRef) { |
| 281 | $client = $clientRef->get(); |
| 282 | $cacheKey = $client->getCacheKey($command['Bucket']); |
| 283 | if ( |
| 284 | empty($command['@region']) && |
| 285 | $region = $client->cache->get($cacheKey) |
| 286 | ) { |
| 287 | $command['@region'] = $region; |
| 288 | } |
| 289 | |
| 290 | return Promise\Coroutine::of(static function () use ( |
| 291 | $handler, |
| 292 | $command, |
| 293 | $cacheKey, |
| 294 | $clientRef |
| 295 | ) { |
| 296 | try { |
| 297 | yield $handler($command); |
| 298 | } catch (PermanentRedirectException $e) { |
| 299 | if (empty($command['Bucket'])) { |
| 300 | throw $e; |
| 301 | } |
| 302 | $client = $clientRef->get(); |
| 303 | $result = $e->getResult(); |
| 304 | $region = null; |
| 305 | if (isset($result['@metadata']['headers']['x-amz-bucket-region'])) { |
| 306 | $region = $result['@metadata']['headers']['x-amz-bucket-region']; |
| 307 | $client->cache->set($cacheKey, $region); |
| 308 | } else { |
| 309 | $region = (yield $client->determineBucketRegionAsync( |
| 310 | $command['Bucket'] |
| 311 | )); |
| 312 | } |
| 313 | |
| 314 | $command['@region'] = $region; |
| 315 | yield $handler($command); |
| 316 | } catch (AwsException $e) { |
| 317 | if ($e->getAwsErrorCode() === 'AuthorizationHeaderMalformed') { |
| 318 | $client = $clientRef->get(); |
| 319 | $region = $client->determineBucketRegionFromExceptionBody( |
| 320 | $e->getResponse() |
| 321 | ); |
| 322 | if (!empty($region)) { |
| 323 | $client->cache->set($cacheKey, $region); |
| 324 | |
| 325 | $command['@region'] = $region; |
| 326 | yield $handler($command); |
| 327 | } else { |
| 328 | throw $e; |
| 329 | } |
| 330 | } else { |
| 331 | throw $e; |
| 332 | } |
| 333 | } |
| 334 | }); |
| 335 | }; |
| 336 | }; |
| 337 | } |
| 338 | |
| 339 | public function createPresignedRequest(CommandInterface $command, $expires, array $options = []) |
| 340 | { |
| 341 | if (empty($command['Bucket'])) { |
| 342 | throw new \InvalidArgumentException('The S3\\MultiRegionClient' |
| 343 | . ' cannot create presigned requests for commands without a' |
| 344 | . ' specified bucket.'); |
| 345 | } |
| 346 | |
| 347 | /** @var S3ClientInterface $client */ |
| 348 | $client = $this->getClientFromPool( |
| 349 | $this->determineBucketRegion($command['Bucket']) |
| 350 | ); |
| 351 | return $client->createPresignedRequest( |
| 352 | $client->getCommand($command->getName(), $command->toArray()), |
| 353 | $expires, |
| 354 | $options |
| 355 | ); |
| 356 | } |
| 357 | |
| 358 | public function getObjectUrl($bucket, $key) |
| 359 | { |
| 360 | /** @var S3Client $regionalClient */ |
| 361 | $regionalClient = $this->getClientFromPool( |
| 362 | $this->determineBucketRegion($bucket) |
| 363 | ); |
| 364 | |
| 365 | return $regionalClient->getObjectUrl($bucket, $key); |
| 366 | } |
| 367 | |
| 368 | public function determineBucketRegionAsync($bucketName) |
| 369 | { |
| 370 | $cacheKey = $this->getCacheKey($bucketName); |
| 371 | if ($cached = $this->cache->get($cacheKey)) { |
| 372 | return Promise\Create::promiseFor($cached); |
| 373 | } |
| 374 | |
| 375 | /** @var S3ClientInterface $regionalClient */ |
| 376 | $regionalClient = $this->getClientFromPool(); |
| 377 | return $regionalClient->determineBucketRegionAsync($bucketName) |
| 378 | ->then( |
| 379 | function ($region) use ($cacheKey) { |
| 380 | $this->cache->set($cacheKey, $region); |
| 381 | |
| 382 | return $region; |
| 383 | } |
| 384 | ); |
| 385 | } |
| 386 | |
| 387 | private function getCacheKey($bucketName) |
| 388 | { |
| 389 | return "aws:s3:{$bucketName}:location"; |
| 390 | } |
| 391 | } |