Installation guide
// Execute an array of command objects to do them in parallel
$s3Client->execute($commands);
// Loop over the commands, which have now all been executed
foreach ($commands as $command) {
$result = $command->getResult();
// Do something with result
}
Error handling with parallel commands
When executing commands in parallel, error handling becomes a bit trickier. If an exception is thrown, then the SDK
(via Guzzle) will aggregate the exceptions together and throw a single
Guzzle\Service\Exception\CommandTransferException (see the API docs) once all of the commands
have completed execution. This exception class keeps track of which commands succeeded and which failed and
also allows you to fetch the original exceptions thrown for failed commands.
use Guzzle\Service\Exception\CommandTransferException;
try {
$succeeded = $client->execute($commands);
} catch (CommandTransferException $e) {
$succeeded = $e->getSuccessfulCommands();
echo "Failed Commands:\n";
foreach ($e->getFailedCommands() as $failedCommand) {
echo $e->getExceptionForFailedCommand($failedCommand)->getMessage() . "\n";
}
}
Waiters
Introduction
One of the higher-level abstractions provided by the SDK are waiters. Waiters help make it easier to work with
eventually consistent systems by providing an easy way to wait until a resource enters into a particular state by
polling the resource. You can find a list of the waiters supported by a client by viewing the API Documentation of a
service client. Any method with a name starting with "waitUntil" will create and invoke a waiter.
In the following example, the Amazon S3 Client is used to create a bucket. Then the waiter method is used to wait
until the bucket exists.
// Create a bucket
$s3Client->createBucket(array('Bucket' => 'my-bucket'));
// Wait until the created bucket is available
$s3Client->waitUntilBucketExists(array('Bucket' => 'my-bucket'));
If the waiter has to poll the bucket too many times, it will throw an
Aws\Common\Exception\RuntimeException exception.
The "waitUntil[…]" methods are all implemented via the __call magic method, and are a more discoverable
shortcut to using the concrete waitUntil() method, since many IDEs can auto-complete methods defined using
the @method annotation. The following code uses the waitUntil() method, but is equivalent to the previous
code sample.
$s3Client->waitUntil('BucketExists', array('Bucket' => 'my-bucket'));
Basic Configuration
You can tune the number of polling attempts issued by a waiter or the number of seconds to delay between each
poll by passing optional values prefixed with "waiter.":
Waiters
32