Installation guide
// Instantiate the S3 client with your AWS credentials
$s3Client = S3Client::factory(array(
'credentials' => $credentials
));
You may also want to read the section in the Getting Started Guide about using a client's factory method for more
details.
Setting credentials after instantiation
At any time after instantiating the client, you can set the credentials the client should use with the
setCredentials() method.
<?php
use Aws\S3\S3Client;
use Aws\Common\Credentials\Credentials
$s3Client = S3Client::factory();
$credentials = new Credentials('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY');
$s3Client->setCredentials($credentials);
This can be used to change the credentials, set temporary credentials, refresh expired credentials, etc.
Using the setCredentials() method will also trigger a client.credentials_changed event, so you can
program other parts of your application to react to the change. To do this, you just need to add a listener to the client
object.
use Aws\S3\S3Client;
use Aws\Common\Credentials\Credentials
// Create 2 sets of credentials
$credentials1 = new Credentials('ACCESS_KEY_1', 'SECRET_KEY_1');
$credentials2 = new Credentials('ACCESS_KEY_2', 'SECRET_KEY_2');
// Instantiate the client with the first credential set
$s3Client = S3Client::factory(array('credentials' => $credentials1));
// Get the event dispatcher and register a listener for the credential change
$dispatcher = $s3Client->getEventDispatcher();
$dispatcher->addListener('client.credentials_changed', function ($event) {
$formerAccessKey = $event['former_credentials']->getAccessKey();
$currentAccessKey = $event['credentials']->getAccessKey();
echo "Access key has changed from {$formerAccessKey} to {$currentAccessKey}.\n";
});
// Change the credentials to the second set to trigger the event
$s3Client->setCredentials($credentials2);
//> Access key has changed from ACCESS_KEY_1 to ACCESS_KEY_2.
Using temporary credentials from AWS STS
AWS Security Token Service (AWS STS) enables you to request limited-privilege, temporary credentials for AWS
IAM users or for users that you authenticate via identity federation. One common use case for using temporary
credentials is to grant mobile or client-side applications access to AWS resources by authenticating users through
third-party identity providers (read more about Web Identity Federation).
Providing Credentials to the SDK
23