User guide

Specifying a Credential Provider or Provider Chain
If you want to specify a different credential provider than the default credential provider chain, you can
specify it in the client constructor.
To specify a specific credentials provider
Provide an instance of a credentials provider or provider chain to a service client constructor that takes
an AWSCredentialsProvider interface as input. For example, to use environment credentials specifically:
AmazonS3 s3Client = new AmazonS3Client(new EnvironmentVariableCredentialsPro
vider());
For the full list of SDK for Java-supplied credential providers and provider chains, see the list of "All known
implementing classes" in the reference topic for AWSCredentialsProvider.
Tip
You can use this technique to supply credential providers or provider chains that you create, by
implementing your own credential provider that implements the AWSCredentialsProvider
interface, or by sub-classing the AWSCredentialsProviderChain class.
Explicitly Specifying Credentials
If neither the default credential chain or a specific or custom provider or provider chain works for your
code, you can set credentials explicitly by supplying them yourself. If you have retrieved temporary
credentials using AWS STS, use this method to specify the credentials for AWS access.
To explicitly supply credentials to an AWS client:
Instantiate a class that provides the AWSCredentials interface, such as BasicAWSCredentials, supplying
it with the AWS access key and secret key you will use for the connection.
Provide the class instance to a service client constructor that takes an AWSCredentials interface as input.
For example:
BasicAWSCredentials awsCreds = new BasicAWSCredentials(access_key_id,
secret_access_key)
AmazonS3 s3Client = new AmazonS3Client(awsCreds);
When using temporary credentials obtained from AWS STS (p. 61), create a BasicSessionCredentials
object, passing it the STS-supplied credentials and session token:
BasicSessionCredentials basic_session_creds = new BasicSessionCredentials(
session_creds.getAccessKeyId(),
session_creds.getSecretAccessKey(),
session_creds.getSessionToken());
AmazonS3Client s3 = new AmazonS3Client(basic_session_creds);
Version v1.0.0
12
AWS SDK for Java Developer Guide
Specifying a Credential Provider or Provider Chain