Installation guide
Service builder
Another way to instantiate a service client is using the Aws\Common\Aws object (a.k.a the service builder). The
Aws object is essentially a service locator, and allows you to specify credentials and configuration settings such that
they can be shared across all client instances. Also, every time you fetch a client object from the Aws object, it will
be exactly the same instance.
use Aws\Common\Aws;
// Create a service locator using a configuration file
$aws = Aws::factory(array(
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
'region' => 'us-east-1',
));
// Get client instances from the service locator by name
$s3Client = $aws->get('s3');
$ec2Client = $aws->get('ec2');
// The service locator always returns the same instance
$anotherS3Client = $aws->get('s3');
assert('$s3Client === $anotherS3Client');
You can also declare your credentials and settings in a configuration file, and provide the path to that file (in either
php or json format) when you instantiate the Aws object.
// Create a `Aws` object using a configuration file
$aws = Aws::factory('/path/to/config.php');
// Get the client from the service locator by namespace
$s3Client = $aws->get('s3');
A simple configuration file should look something like this:
<?php return array(
'includes' => array('_aws'),
'services' => array(
'default_settings' => array(
'params' => array(
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
'region' => 'us-west-2'
)
)
)
);
For more information about configuration files, please see Configuring the SDK.
Performing service operations
You can perform a service operation by calling the method of the same name on the client object. For example, to
perform the Amazon DynamoDB DescribeTable operation, you must call the
Aws\DynamoDb\DynamoDbClient::describeTable() method. Operation methods, like describeTable(),
all accept a single argument that is an associative array of values representing the parameters to the operation. The
structure of this array is defined for each operation in the SDK's API Documentation (e.g., see the API docs for
describeTable()).
$result = $dynamoDbClient->describeTable(array(
'TableName' => 'YourTableName',
));
To learn about performing operations in more detail, including using command objects, see Command Objects.
Getting Started Guide
6