AWS Official Blog

Version 2 of the AWS SDK for PHP

by Jeff Barr | on | in AWS SDK for PHP, Developer Tools | | Comments

I’m happy to announce that version 2 of the AWS SDK for PHP is now available. To take advantage of new features in PHP 5.3 and to conform to the recommendations of the PHP Framework Interop Group, we rebuilt the SDK from the ground up. We made these changes to give PHP developers a firm foundation for the future by aligning with a number of other toolkits and projects in the PHP ecosystem that are heading in the same direction.

Highlights
The new SDK is built on top of the Guzzle HTTP client framework, which provides increased performance and enables event-driven customization. Each AWS service client extends the Guzzle client and describes operations on the service using a service description file. The SDK now manages persistent connections for both serial and parallel requests. It detects transient network failures, with automatic retries using truncated exponential backoff. Support for event hooks (via the Symfony2 EventDispatcher) allows you to implement custom, event-driven behavior.

Internally, the SDK now uses a more robust and flexible object-oriented architecture. It makes use of design patterns, event dispatching, and dependency injection. Dependency management and resolution are handled by Composer, simplifying the process of both installing the SDK and auto-loading classes. You can still install the SDK via PEAR or try the downloadable .phar (PHP archive). The Aws\Common namespace contains the core of the SDK; each service client is contained in its own separate namespace (e.g. Aws\DynamoDb).

Goodies
As you would expect, the SDK provides you with access to AWS services. It also includes a number of higher-level functions and other goodies to simplify and streamline your code. For example:

  • “Waiter” objects allow you to poll a resource until it exist in the expected state.
  • Iterator classes allow you to traverse the results of AWS’ list and describe operations. You no longer have to write code to perform multiple requests in a loop or track tokens or markers between calls.
  • Requests can be batched for parallel execution.
  • Batched writes to Amazon DynamoDB are now supported with a powerful new batch write system.
  • Multi-part uploads to Amazon S3 and Amazon Glacier are now supported with a multi-part upload system that can be paused and resumed.

PHP SDK in Action
Let’s walk through some code samples so that you can get a taste of the new SDK. You need to load Composer’s autoload.php file:

require __DIR__ . ‘/vendor/autoload.php’ ;

Introduce the AWS namespace:

use Aws\Common\Aws ;
 

Then you can create the Aws object, which you can think of as a service builder or dependency injection container:

$aws = Aws :: factory ( array (
    ‘key’     => ‘my-access-key-id’ ,
    ‘secret’ => ‘my-secret-access-key’ ,
    ‘region’ => Region :: US_WEST_2
) ) ;

Then you get the S3 client from the service builder:

$s3 = $aws -> get ( ‘s3′ ) ;

And use it to create a ListObjects Command:

$listObjectsCommand = $s3 -> getCommand ( ‘ListObjects’ , array (
    ‘Bucket’ => ‘MyBucketName’
) ) ;

Finally, you create an Iterator and use it to list the contents of the bucket:

$iterator = $s3 -> getIterator ( $listObjectsCommand ) ;

foreach ($iterator as $object) {
    echo $object[‘Key’] . ‘ – ‘ . $object[‘Size’] . PHP_EOL;
}

SDK Status
The SDK is available now and supports the following AWS services: 

  • Amazon S3
  • Amazon Glacier
  • Amazon DynamoDB
  • Amazon CloudFront

 We’ll continue to broaden support in upcoming releases. Until then, you can install the previous SDK side-by-side with this version by following the directions in our Side-by-Side Guide.

Because of the design and architectural changes, Version 2 of the SDK is not backwards compatible with its predecessor. Our Migration Guide has details on whats different between the two versions.

SDK Resources
Here are some resources to get you started:

Go Forth and Code
I hope that you enjoy building awesome AWS apps using the new SDK. Let me know what you think!

— Jeff;