Installation guide
The easiest way to add a cache to your IAM role credentials is to specify a credentials cache using the
credentials.cache option in a client's factory method or in a service builder configuration file. The
credentials.cache configuration setting should be set to an object that implements Guzzle's
Guzzle\Cache\CacheAdapterInterface (see Guzzle cache adapters). This interface provides an abstraction
layer over various cache backends, including Doctrine Cache, Zend Framework 2 cache, etc.
<?php
require 'vendor/autoload.php';
use Doctrine\Common\Cache\FilesystemCache;
use Guzzle\Cache\DoctrineCacheAdapter;
// Create a cache adapter that stores data on the filesystem
$cacheAdapter = new DoctrineCacheAdapter(new FilesystemCache('/tmp/cache'));
// Provide a credentials.cache to cache credentials to the file system
$s3Client = Aws\S3\S3Client::factory(array(
'credentials.cache' => $cacheAdapter
));
In the preceding example, the addition of credentials.cache causes credentials to be cached to the local
filesystem using Doctrine's caching system. Every request that uses this cache adapter first checks if the credentials
are in the cache. If the credentials are found in the cache, the client then ensures that the credentials are not
expired. In the event that cached credentials become expired, the client automatically refreshes the credentials on
the next request and populates the cache with the updated credentials.
A credentials cache can also be used in a service builder configuration:
<?php
// File saved as /path/to/custom/config.php
use Doctrine\Common\Cache\FilesystemCache;
use Guzzle\Cache\DoctrineCacheAdapter;
$cacheAdapter = new DoctrineCacheAdapter(new FilesystemCache('/tmp/cache'));
return array(
'includes' => array('_aws'),
'services' => array(
'default_settings' => array(
'params' => array(
'credentials.cache' => $cacheAdapter
)
)
)
);
If you were to use the above configuration file with a service builder, then all of the clients created through the
service builder would utilize a shared credentials cache object.
Setting credentials explicitly in your code
The SDK allows you to explicitly set your credentials in your project in a few different ways. These techniques are
useful for rapid development, integrating with existing configurations systems (e.g., your PHP framework of choice),
and handling temporary credentials. However, be careful to not hard-code your credentials inside of your
applications. Hard-coding your credentials can be dangerous, because it is easy to accidentally commit your
credentials into an SCM repository, potentially exposing your credentials to more people than intended. It can also
make it difficult to rotate credentials in the future.
Using a configuration file with the service builder
Providing Credentials to the SDK
21