S3Client.php
30.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<?php
namespace Aws\S3;
use Aws\Api\ApiProvider;
use Aws\Api\DocModel;
use Aws\Api\Service;
use Aws\AwsClient;
use Aws\ClientResolver;
use Aws\Command;
use Aws\Exception\AwsException;
use Aws\HandlerList;
use Aws\Middleware;
use Aws\RetryMiddleware;
use Aws\ResultInterface;
use Aws\CommandInterface;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
/**
* Client used to interact with **Amazon Simple Storage Service (Amazon S3)**.
*
* @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 deleteBucketAnalyticsConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketAnalyticsConfigurationAsync(array $args = [])
* @method \Aws\Result deleteBucketCors(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketCorsAsync(array $args = [])
* @method \Aws\Result deleteBucketEncryption(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketEncryptionAsync(array $args = [])
* @method \Aws\Result deleteBucketInventoryConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketInventoryConfigurationAsync(array $args = [])
* @method \Aws\Result deleteBucketLifecycle(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketLifecycleAsync(array $args = [])
* @method \Aws\Result deleteBucketMetricsConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketMetricsConfigurationAsync(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 deleteObjectTagging(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteObjectTaggingAsync(array $args = [])
* @method \Aws\Result deleteObjects(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteObjectsAsync(array $args = [])
* @method \Aws\Result deletePublicAccessBlock(array $args = [])
* @method \GuzzleHttp\Promise\Promise deletePublicAccessBlockAsync(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 getBucketAnalyticsConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketAnalyticsConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketCors(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketCorsAsync(array $args = [])
* @method \Aws\Result getBucketEncryption(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketEncryptionAsync(array $args = [])
* @method \Aws\Result getBucketInventoryConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketInventoryConfigurationAsync(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 getBucketMetricsConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketMetricsConfigurationAsync(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 getBucketPolicyStatus(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketPolicyStatusAsync(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 getObjectLegalHold(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectLegalHoldAsync(array $args = [])
* @method \Aws\Result getObjectLockConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectLockConfigurationAsync(array $args = [])
* @method \Aws\Result getObjectRetention(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectRetentionAsync(array $args = [])
* @method \Aws\Result getObjectTagging(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectTaggingAsync(array $args = [])
* @method \Aws\Result getObjectTorrent(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectTorrentAsync(array $args = [])
* @method \Aws\Result getPublicAccessBlock(array $args = [])
* @method \GuzzleHttp\Promise\Promise getPublicAccessBlockAsync(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 listBucketAnalyticsConfigurations(array $args = [])
* @method \GuzzleHttp\Promise\Promise listBucketAnalyticsConfigurationsAsync(array $args = [])
* @method \Aws\Result listBucketInventoryConfigurations(array $args = [])
* @method \GuzzleHttp\Promise\Promise listBucketInventoryConfigurationsAsync(array $args = [])
* @method \Aws\Result listBucketMetricsConfigurations(array $args = [])
* @method \GuzzleHttp\Promise\Promise listBucketMetricsConfigurationsAsync(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 putBucketAnalyticsConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketAnalyticsConfigurationAsync(array $args = [])
* @method \Aws\Result putBucketCors(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketCorsAsync(array $args = [])
* @method \Aws\Result putBucketEncryption(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketEncryptionAsync(array $args = [])
* @method \Aws\Result putBucketInventoryConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketInventoryConfigurationAsync(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 putBucketMetricsConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketMetricsConfigurationAsync(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 putObjectLegalHold(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectLegalHoldAsync(array $args = [])
* @method \Aws\Result putObjectLockConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectLockConfigurationAsync(array $args = [])
* @method \Aws\Result putObjectRetention(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectRetentionAsync(array $args = [])
* @method \Aws\Result putObjectTagging(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectTaggingAsync(array $args = [])
* @method \Aws\Result putPublicAccessBlock(array $args = [])
* @method \GuzzleHttp\Promise\Promise putPublicAccessBlockAsync(array $args = [])
* @method \Aws\Result restoreObject(array $args = [])
* @method \GuzzleHttp\Promise\Promise restoreObjectAsync(array $args = [])
* @method \Aws\Result selectObjectContent(array $args = [])
* @method \GuzzleHttp\Promise\Promise selectObjectContentAsync(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 S3Client extends AwsClient implements S3ClientInterface
{
use S3ClientTrait;
public static function getArguments()
{
$args = parent::getArguments();
$args['retries']['fn'] = [__CLASS__, '_applyRetryConfig'];
$args['api_provider']['fn'] = [__CLASS__, '_applyApiProvider'];
return $args + [
'bucket_endpoint' => [
'type' => 'config',
'valid' => ['bool'],
'doc' => 'Set to true to send requests to a hardcoded '
. 'bucket endpoint rather than create an endpoint as a '
. 'result of injecting the bucket into the URL. This '
. 'option is useful for interacting with CNAME endpoints.',
],
'use_accelerate_endpoint' => [
'type' => 'config',
'valid' => ['bool'],
'doc' => 'Set to true to send requests to an S3 Accelerate'
. ' endpoint by default. Can be enabled or disabled on'
. ' individual operations by setting'
. ' \'@use_accelerate_endpoint\' to true or false. Note:'
. ' you must enable S3 Accelerate on a bucket before it can'
. ' be accessed via an Accelerate endpoint.',
'default' => false,
],
'use_dual_stack_endpoint' => [
'type' => 'config',
'valid' => ['bool'],
'doc' => 'Set to true to send requests to an S3 Dual Stack'
. ' endpoint by default, which enables IPv6 Protocol.'
. ' Can be enabled or disabled on individual operations by setting'
. ' \'@use_dual_stack_endpoint\' to true or false.',
'default' => false,
],
'use_path_style_endpoint' => [
'type' => 'config',
'valid' => ['bool'],
'doc' => 'Set to true to send requests to an S3 path style'
. ' endpoint by default.'
. ' Can be enabled or disabled on individual operations by setting'
. ' \'@use_path_style_endpoint\' to true or false.',
'default' => false,
],
];
}
/**
* {@inheritdoc}
*
* In addition to the options available to
* {@see Aws\AwsClient::__construct}, S3Client accepts the following
* options:
*
* - bucket_endpoint: (bool) Set to true to send requests to a
* hardcoded bucket endpoint rather than create an endpoint as a result
* of injecting the bucket into the URL. This option is useful for
* interacting with CNAME endpoints.
* - calculate_md5: (bool) Set to false to disable calculating an MD5
* for all Amazon S3 signed uploads.
* - use_accelerate_endpoint: (bool) Set to true to send requests to an S3
* Accelerate endpoint by default. Can be enabled or disabled on
* individual operations by setting '@use_accelerate_endpoint' to true or
* false. Note: you must enable S3 Accelerate on a bucket before it can be
* accessed via an Accelerate endpoint.
* - use_dual_stack_endpoint: (bool) Set to true to send requests to an S3
* Dual Stack endpoint by default, which enables IPv6 Protocol.
* Can be enabled or disabled on individual operations by setting
* '@use_dual_stack_endpoint\' to true or false. Note:
* you cannot use it together with an accelerate endpoint.
* - use_path_style_endpoint: (bool) Set to true to send requests to an S3
* path style endpoint by default.
* Can be enabled or disabled on individual operations by setting
* '@use_path_style_endpoint\' to true or false. Note:
* you cannot use it together with an accelerate endpoint.
*
* @param array $args
*/
public function __construct(array $args)
{
parent::__construct($args);
$stack = $this->getHandlerList();
$stack->appendInit(SSECMiddleware::wrap($this->getEndpoint()->getScheme()), 's3.ssec');
$stack->appendBuild(ApplyChecksumMiddleware::wrap(), 's3.checksum');
$stack->appendBuild(
Middleware::contentType(['PutObject', 'UploadPart']),
's3.content_type'
);
// Use the bucket style middleware when using a "bucket_endpoint" (for cnames)
if ($this->getConfig('bucket_endpoint')) {
$stack->appendBuild(BucketEndpointMiddleware::wrap(), 's3.bucket_endpoint');
} else {
$stack->appendBuild(
S3EndpointMiddleware::wrap(
$this->getRegion(),
[
'dual_stack' => $this->getConfig('use_dual_stack_endpoint'),
'accelerate' => $this->getConfig('use_accelerate_endpoint'),
'path_style' => $this->getConfig('use_path_style_endpoint')
]
),
's3.endpoint_middleware'
);
}
$stack->appendSign(PutObjectUrlMiddleware::wrap(), 's3.put_object_url');
$stack->appendSign(PermanentRedirectMiddleware::wrap(), 's3.permanent_redirect');
$stack->appendInit(Middleware::sourceFile($this->getApi()), 's3.source_file');
$stack->appendInit($this->getSaveAsParameter(), 's3.save_as');
$stack->appendInit($this->getLocationConstraintMiddleware(), 's3.location');
$stack->appendInit($this->getEncodingTypeMiddleware(), 's3.auto_encode');
$stack->appendInit($this->getHeadObjectMiddleware(), 's3.head_object');
}
/**
* Determine if a string is a valid name for a DNS compatible Amazon S3
* bucket.
*
* DNS compatible bucket names can be used as a subdomain in a URL (e.g.,
* "<bucket>.s3.amazonaws.com").
*
* @param string $bucket Bucket name to check.
*
* @return bool
*/
public static function isBucketDnsCompatible($bucket)
{
$bucketLen = strlen($bucket);
return ($bucketLen >= 3 && $bucketLen <= 63) &&
// Cannot look like an IP address
!filter_var($bucket, FILTER_VALIDATE_IP) &&
preg_match('/^[a-z0-9]([a-z0-9\-\.]*[a-z0-9])?$/', $bucket);
}
public function createPresignedRequest(CommandInterface $command, $expires)
{
$command = clone $command;
$command->getHandlerList()->remove('signer');
/** @var \Aws\Signature\SignatureInterface $signer */
$signer = call_user_func(
$this->getSignatureProvider(),
$this->getConfig('signature_version'),
$this->getConfig('signing_name'),
$this->getConfig('signing_region')
);
return $signer->presign(
\Aws\serialize($command),
$this->getCredentials()->wait(),
$expires
);
}
public function getObjectUrl($bucket, $key)
{
$command = $this->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $key
]);
return (string) \Aws\serialize($command)->getUri();
}
/**
* Raw URL encode a key and allow for '/' characters
*
* @param string $key Key to encode
*
* @return string Returns the encoded key
*/
public static function encodeKey($key)
{
return str_replace('%2F', '/', rawurlencode($key));
}
/**
* Provides a middleware that removes the need to specify LocationConstraint on CreateBucket.
*
* @return \Closure
*/
private function getLocationConstraintMiddleware()
{
$region = $this->getRegion();
return static function (callable $handler) use ($region) {
return function (Command $command, $request = null) use ($handler, $region) {
if ($command->getName() === 'CreateBucket') {
$locationConstraint = isset($command['CreateBucketConfiguration']['LocationConstraint'])
? $command['CreateBucketConfiguration']['LocationConstraint']
: null;
if ($locationConstraint === 'us-east-1') {
unset($command['CreateBucketConfiguration']);
} elseif ('us-east-1' !== $region && empty($locationConstraint)) {
$command['CreateBucketConfiguration'] = ['LocationConstraint' => $region];
}
}
return $handler($command, $request);
};
};
}
/**
* Provides a middleware that supports the `SaveAs` parameter.
*
* @return \Closure
*/
private function getSaveAsParameter()
{
return static function (callable $handler) {
return function (Command $command, $request = null) use ($handler) {
if ($command->getName() === 'GetObject' && isset($command['SaveAs'])) {
$command['@http']['sink'] = $command['SaveAs'];
unset($command['SaveAs']);
}
return $handler($command, $request);
};
};
}
/**
* Provides a middleware that disables content decoding on HeadObject
* commands.
*
* @return \Closure
*/
private function getHeadObjectMiddleware()
{
return static function (callable $handler) {
return function (
CommandInterface $command,
RequestInterface $request = null
) use ($handler) {
if ($command->getName() === 'HeadObject'
&& !isset($command['@http']['decode_content'])
) {
$command['@http']['decode_content'] = false;
}
return $handler($command, $request);
};
};
}
/**
* Provides a middleware that autopopulates the EncodingType parameter on
* ListObjects commands.
*
* @return \Closure
*/
private function getEncodingTypeMiddleware()
{
return static function (callable $handler) {
return function (Command $command, $request = null) use ($handler) {
$autoSet = false;
if ($command->getName() === 'ListObjects'
&& empty($command['EncodingType'])
) {
$command['EncodingType'] = 'url';
$autoSet = true;
}
return $handler($command, $request)
->then(function (ResultInterface $result) use ($autoSet) {
if ($result['EncodingType'] === 'url' && $autoSet) {
static $topLevel = [
'Delimiter',
'Marker',
'NextMarker',
'Prefix',
];
static $nested = [
['Contents', 'Key'],
['CommonPrefixes', 'Prefix'],
];
foreach ($topLevel as $key) {
if (isset($result[$key])) {
$result[$key] = urldecode($result[$key]);
}
}
foreach ($nested as $steps) {
if (isset($result[$steps[0]])) {
foreach ($result[$steps[0]] as $key => $part) {
if (isset($part[$steps[1]])) {
$result[$steps[0]][$key][$steps[1]]
= urldecode($part[$steps[1]]);
}
}
}
}
}
return $result;
});
};
};
}
/** @internal */
public static function _applyRetryConfig($value, $_, HandlerList $list)
{
if (!$value) {
return;
}
$decider = RetryMiddleware::createDefaultDecider($value);
$decider = function ($retries, $command, $request, $result, $error) use ($decider, $value) {
$maxRetries = null !== $command['@retries']
? $command['@retries']
: $value;
if ($decider($retries, $command, $request, $result, $error)) {
return true;
}
if ($error instanceof AwsException
&& $retries < $maxRetries
) {
if ($error->getResponse()
&& $error->getResponse()->getStatusCode() >= 400
) {
return strpos(
$error->getResponse()->getBody(),
'Your socket connection to the server'
) !== false;
}
if ($error->getPrevious() instanceof RequestException) {
// All commands except CompleteMultipartUpload are
// idempotent and may be retried without worry if a
// networking error has occurred.
return $command->getName() !== 'CompleteMultipartUpload';
}
}
return false;
};
$delay = [RetryMiddleware::class, 'exponentialDelay'];
$list->appendSign(Middleware::retry($decider, $delay), 'retry');
}
/** @internal */
public static function _applyApiProvider($value, array &$args, HandlerList $list)
{
ClientResolver::_apply_api_provider($value, $args, $list);
$args['parser'] = new GetBucketLocationParser(
new AmbiguousSuccessParser(
new RetryableMalformedResponseParser(
$args['parser'],
$args['exception_class']
),
$args['error_parser'],
$args['exception_class']
)
);
}
/**
* @internal
* @codeCoverageIgnore
*/
public static function applyDocFilters(array $api, array $docs)
{
$b64 = '<div class="alert alert-info">This value will be base64 encoded on your behalf.</div>';
$opt = '<div class="alert alert-info">This value will be computed for you it is not supplied.</div>';
// Add the SourceFile parameter.
$docs['shapes']['SourceFile']['base'] = 'The path to a file on disk to use instead of the Body parameter.';
$api['shapes']['SourceFile'] = ['type' => 'string'];
$api['shapes']['PutObjectRequest']['members']['SourceFile'] = ['shape' => 'SourceFile'];
$api['shapes']['UploadPartRequest']['members']['SourceFile'] = ['shape' => 'SourceFile'];
// Add the ContentSHA256 parameter.
$docs['shapes']['ContentSHA256']['base'] = 'A SHA256 hash of the body content of the request.';
$api['shapes']['ContentSHA256'] = ['type' => 'string'];
$api['shapes']['PutObjectRequest']['members']['ContentSHA256'] = ['shape' => 'ContentSHA256'];
$api['shapes']['UploadPartRequest']['members']['ContentSHA256'] = ['shape' => 'ContentSHA256'];
unset($api['shapes']['PutObjectRequest']['members']['ContentMD5']);
unset($api['shapes']['UploadPartRequest']['members']['ContentMD5']);
$docs['shapes']['ContentSHA256']['append'] = $opt;
// Add the SaveAs parameter.
$docs['shapes']['SaveAs']['base'] = 'The path to a file on disk to save the object data.';
$api['shapes']['SaveAs'] = ['type' => 'string'];
$api['shapes']['GetObjectRequest']['members']['SaveAs'] = ['shape' => 'SaveAs'];
// Several SSECustomerKey documentation updates.
$docs['shapes']['SSECustomerKey']['append'] = $b64;
$docs['shapes']['CopySourceSSECustomerKey']['append'] = $b64;
$docs['shapes']['SSECustomerKeyMd5']['append'] = $opt;
// Add the ObjectURL to various output shapes and documentation.
$docs['shapes']['ObjectURL']['base'] = 'The URI of the created object.';
$api['shapes']['ObjectURL'] = ['type' => 'string'];
$api['shapes']['PutObjectOutput']['members']['ObjectURL'] = ['shape' => 'ObjectURL'];
$api['shapes']['CopyObjectOutput']['members']['ObjectURL'] = ['shape' => 'ObjectURL'];
$api['shapes']['CompleteMultipartUploadOutput']['members']['ObjectURL'] = ['shape' => 'ObjectURL'];
// Fix references to Location Constraint.
unset($api['shapes']['CreateBucketRequest']['payload']);
$api['shapes']['BucketLocationConstraint']['enum'] = [
"ap-northeast-1",
"ap-southeast-2",
"ap-southeast-1",
"cn-north-1",
"eu-central-1",
"eu-west-1",
"us-east-1",
"us-west-1",
"us-west-2",
"sa-east-1",
];
// Add a note that the ContentMD5 is optional.
$docs['shapes']['ContentMD5']['append'] = '<div class="alert alert-info">The value will be computed on '
. 'your behalf.</div>';
return [
new Service($api, ApiProvider::defaultProvider()),
new DocModel($docs)
];
}
}