
Note
You must have specified default credentials in at least one of these ways, or the attempt will fail.
For more information, see Setting up AWS Credentials (p. 5).
Specifying a Credentials Provider
If you don't want to use the default credentials provider, you can specify your preferred credentials provider.
For example, you might want to use a nondefault profile from the credentials file, which is handled by
SharedCredentialFileProvider.You can specify a credentials provider as follows:
• To specify a credentials provider for a particular service, pass a provider object to the service's client
constructor, which must take a class with the Provider interface as input. A provider typically has options
that you can use to specify a particular set of credentials.
The following example directs the Amazon S3 client constructor to get its credentials from the
SharedCredentialFileProvider object, which loads credentials from the credentials file.
# Create a new S3 object using a specific provider
s3 = AWS::S3.new(
:credential_provider => AWS::Core::CredentialProviders::SharedCredentialFile
Provider.new)
Creating the provider object with the default constructor specifies the credentials file's default profile.
You can also use the constructor's profile_name option to specify a profile by name or its path
option to specify a nondefault credentials file location.
• You can specify a credential provider that will be used by all service clients by passing a provider object
to the AWS.config method. The following example specifies the default profile from the credentials file
for all services.
AWS.config(
:credential_provider => AWS::Core::CredentialProviders::SharedCredentialFile
Provider.new)
# Create a new S3 object
s3 = AWS::S3.new
You can then use the Amazon S3 client object's default constructor, which automatically loads the
appropriate profile.
For the full list of SDK for Ruby credential provider classes, see AWS::Core::CredentialProviders. In the
SDK for Ruby, all provider classes mix in the Provider interface.
Tip
You can also use this approach to supply your own credentials provider. Just implement a
credentials provider class that mixes in the Provider interface.You can then pass an instance
of the class to a service's client class constructor or to AWS.config, as described above.
Explicitly Specifying Credentials
You can specify credentials explicitly as follows.
• Specify credentials for a particular service by using the client class constructor's :access_key_id
and :secret_access_key options.
The following example specifies an explicit set of credentials for the Amazon S3 client object.
Version v1.0.0
7
AWS SDK for Ruby Developer Guide
Specifying a Credentials Provider
Comentários a estes Manuais