S3MultiRegionClient.php
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace Aws\S3;
use Aws\CacheInterface;
use Aws\CommandInterface;
use Aws\LruArrayCache;
use Aws\MultiRegionClient as BaseClient;
use Aws\S3\Exception\PermanentRedirectException;
use GuzzleHttp\Promise;
/**
* **Amazon Simple Storage Service** multi-region client.
*
* @method \Aws\Result abortMultipartUpload(array $args = [])
* @method \GuzzleHttp\Promise\Promise abortMultipartUploadAsync(array $args = [])
* @method \Aws\Result completeMultipartUpload(array $args = [])
* @method \GuzzleHttp\Promise\Promise completeMultipartUploadAsync(array $args = [])
* @method \Aws\Result copyObject(array $args = [])
* @method \GuzzleHttp\Promise\Promise copyObjectAsync(array $args = [])
* @method \Aws\Result createBucket(array $args = [])
* @method \GuzzleHttp\Promise\Promise createBucketAsync(array $args = [])
* @method \Aws\Result createMultipartUpload(array $args = [])
* @method \GuzzleHttp\Promise\Promise createMultipartUploadAsync(array $args = [])
* @method \Aws\Result deleteBucket(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketAsync(array $args = [])
* @method \Aws\Result deleteBucketCors(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketCorsAsync(array $args = [])
* @method \Aws\Result deleteBucketLifecycle(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketLifecycleAsync(array $args = [])
* @method \Aws\Result deleteBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketPolicyAsync(array $args = [])
* @method \Aws\Result deleteBucketReplication(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketReplicationAsync(array $args = [])
* @method \Aws\Result deleteBucketTagging(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketTaggingAsync(array $args = [])
* @method \Aws\Result deleteBucketWebsite(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketWebsiteAsync(array $args = [])
* @method \Aws\Result deleteObject(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteObjectAsync(array $args = [])
* @method \Aws\Result deleteObjects(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteObjectsAsync(array $args = [])
* @method \Aws\Result getBucketAccelerateConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketAccelerateConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketAcl(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketAclAsync(array $args = [])
* @method \Aws\Result getBucketCors(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketCorsAsync(array $args = [])
* @method \Aws\Result getBucketLifecycle(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketLifecycleAsync(array $args = [])
* @method \Aws\Result getBucketLifecycleConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketLifecycleConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketLocation(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketLocationAsync(array $args = [])
* @method \Aws\Result getBucketLogging(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketLoggingAsync(array $args = [])
* @method \Aws\Result getBucketNotification(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketNotificationAsync(array $args = [])
* @method \Aws\Result getBucketNotificationConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketNotificationConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketPolicyAsync(array $args = [])
* @method \Aws\Result getBucketReplication(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketReplicationAsync(array $args = [])
* @method \Aws\Result getBucketRequestPayment(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketRequestPaymentAsync(array $args = [])
* @method \Aws\Result getBucketTagging(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketTaggingAsync(array $args = [])
* @method \Aws\Result getBucketVersioning(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketVersioningAsync(array $args = [])
* @method \Aws\Result getBucketWebsite(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketWebsiteAsync(array $args = [])
* @method \Aws\Result getObject(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectAsync(array $args = [])
* @method \Aws\Result getObjectAcl(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectAclAsync(array $args = [])
* @method \Aws\Result getObjectTorrent(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectTorrentAsync(array $args = [])
* @method \Aws\Result headBucket(array $args = [])
* @method \GuzzleHttp\Promise\Promise headBucketAsync(array $args = [])
* @method \Aws\Result headObject(array $args = [])
* @method \GuzzleHttp\Promise\Promise headObjectAsync(array $args = [])
* @method \Aws\Result listBuckets(array $args = [])
* @method \GuzzleHttp\Promise\Promise listBucketsAsync(array $args = [])
* @method \Aws\Result listMultipartUploads(array $args = [])
* @method \GuzzleHttp\Promise\Promise listMultipartUploadsAsync(array $args = [])
* @method \Aws\Result listObjectVersions(array $args = [])
* @method \GuzzleHttp\Promise\Promise listObjectVersionsAsync(array $args = [])
* @method \Aws\Result listObjects(array $args = [])
* @method \GuzzleHttp\Promise\Promise listObjectsAsync(array $args = [])
* @method \Aws\Result listObjectsV2(array $args = [])
* @method \GuzzleHttp\Promise\Promise listObjectsV2Async(array $args = [])
* @method \Aws\Result listParts(array $args = [])
* @method \GuzzleHttp\Promise\Promise listPartsAsync(array $args = [])
* @method \Aws\Result putBucketAccelerateConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketAccelerateConfigurationAsync(array $args = [])
* @method \Aws\Result putBucketAcl(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketAclAsync(array $args = [])
* @method \Aws\Result putBucketCors(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketCorsAsync(array $args = [])
* @method \Aws\Result putBucketLifecycle(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketLifecycleAsync(array $args = [])
* @method \Aws\Result putBucketLifecycleConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketLifecycleConfigurationAsync(array $args = [])
* @method \Aws\Result putBucketLogging(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketLoggingAsync(array $args = [])
* @method \Aws\Result putBucketNotification(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketNotificationAsync(array $args = [])
* @method \Aws\Result putBucketNotificationConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketNotificationConfigurationAsync(array $args = [])
* @method \Aws\Result putBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketPolicyAsync(array $args = [])
* @method \Aws\Result putBucketReplication(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketReplicationAsync(array $args = [])
* @method \Aws\Result putBucketRequestPayment(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketRequestPaymentAsync(array $args = [])
* @method \Aws\Result putBucketTagging(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketTaggingAsync(array $args = [])
* @method \Aws\Result putBucketVersioning(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketVersioningAsync(array $args = [])
* @method \Aws\Result putBucketWebsite(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketWebsiteAsync(array $args = [])
* @method \Aws\Result putObject(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectAsync(array $args = [])
* @method \Aws\Result putObjectAcl(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectAclAsync(array $args = [])
* @method \Aws\Result restoreObject(array $args = [])
* @method \GuzzleHttp\Promise\Promise restoreObjectAsync(array $args = [])
* @method \Aws\Result uploadPart(array $args = [])
* @method \GuzzleHttp\Promise\Promise uploadPartAsync(array $args = [])
* @method \Aws\Result uploadPartCopy(array $args = [])
* @method \GuzzleHttp\Promise\Promise uploadPartCopyAsync(array $args = [])
*/
class S3MultiRegionClient extends BaseClient implements S3ClientInterface
{
use S3ClientTrait {
determineBucketRegionAsync as private lookupBucketRegion;
}
/** @var CacheInterface */
private $cache;
public static function getArguments()
{
$args = parent::getArguments();
$args['region']['default'] = 'us-east-1';
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; },
],
];
}
public function __construct(array $args)
{
parent::__construct($args);
$this->cache = $this->getConfig('bucket_region_cache');
}
public function executeAsync(CommandInterface $c)
{
return Promise\coroutine(function () use ($c) {
if ($region = $this->cache->get($this->getCacheKey($c['Bucket']))) {
$c = $this->getRegionalizedCommand($c, $region);
}
try {
yield parent::executeAsync($c);
} catch (PermanentRedirectException $e) {
if (empty($c['Bucket'])) {
throw $e;
}
$region = (yield $this->lookupBucketRegion($c['Bucket']));
$this->cache->set($this->getCacheKey($c['Bucket']), $region);
$c = $this->getRegionalizedCommand($c, $region);
yield parent::executeAsync($c);
}
});
}
public function createPresignedRequest(CommandInterface $command, $expires)
{
if (empty($command['Bucket'])) {
throw new \InvalidArgumentException('The S3\\MultiRegionClient'
. ' cannot create presigned requests for commands without a'
. ' specified bucket.');
}
/** @var S3ClientInterface $client */
$client = $this->getClientFromPool(
$this->determineBucketRegion($command['Bucket'])
);
return $client->createPresignedRequest(
$client->getCommand($command->getName(), $command->toArray()),
$expires
);
}
public function getObjectUrl($bucket, $key)
{
/** @var S3Client $regionalClient */
$regionalClient = $this->getClientFromPool(
$this->determineBucketRegion($bucket)
);
return $regionalClient->getObjectUrl($bucket, $key);
}
public function determineBucketRegionAsync($bucketName)
{
if ($cached = $this->cache->get($this->getCacheKey($bucketName))) {
return Promise\promise_for($cached);
}
return $this->lookupBucketRegion($bucketName)
->then(function ($region) use ($bucketName) {
$this->cache->set($this->getCacheKey($bucketName), $region);
return $region;
});
}
private function getRegionalizedCommand(CommandInterface $command, $region)
{
return $this->getClientFromPool($region)
->getCommand($command->getName(), $command->toArray());
}
private function getCacheKey($bucketName)
{
return "aws:s3:{$bucketName}:location";
}
}