AuthUsingAcctOrUserCredRuby
Making Requests Using AWS Account or IAM User Credentials - AWS SDK for Ruby
Before you can use version 3 of the AWS SDK for Ruby to make calls to Amazon S3, you must set the AWS access credentials that the SDK uses to verify your access to your buckets and objects. If you have shared credentials set up in the AWS credentials profile on your local system, version 3 of the SDK for Ruby can use those credentials without your having to declare them in your code. For more information about setting up shared credentials, see Making Requests Using AWS Account or IAM User Credentials.
The following Ruby code snippet uses the credentials in a shared AWS credentials file on a local computer to authenticate a request to get all of the object key names in a specific bucket. It does the following:
Creates an instance of the
Aws::S3::Resource
class.Makes a request to Amazon S3 by enumerating objects in a bucket using the
bucket
method ofAws::S3::Resource
. The client generates the necessary signature value from the credentials in the AWS credentials file on your computer, and includes it in the request it sends to Amazon S3.Prints the array of object key names to the terminal.
Example
If you don't have a local AWS credentials file, you can still create the Aws::S3::Resource
resource and execute code against Amazon S3 buckets and objects. Requests that are sent using version 3 of the SDK for Ruby are anonymous, with no signature by default. Amazon S3 returns an error if you send anonymous requests for a resource that's not publicly available.
You can use and expand the previous code snippet for SDK for Ruby applications, as in the following more robust example. The credentials that are used for this example come from a local AWS credentials file on the computer that is running this application. The credentials are for an IAM user who can list objects in the bucket that the user specifies when they run the application.