Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awsdocs
GitHub Repository: awsdocs/amazon-s3-developer-guide
Path: blob/master/doc_source/AuthUsingAcctOrUserCredRuby.md
4080 views

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:

  1. Creates an instance of the Aws::S3::Client class.

  2. Makes a request to Amazon S3 by enumerating objects in a bucket using the list_objects_v2 method of Aws::S3::Client. 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.

  3. Prints the array of object key names to the terminal.

Example

require 'aws-sdk-s3' # Prints the list of objects in an Amazon S3 bucket. # # @param s3_client [Aws::S3::Client] An initialized Amazon S3 client. # @param bucket_name [String] The bucket's name. # @return [Boolean] true if all operations succeed; otherwise, false. # @example # s3_client = Aws::S3::Client.new(region: 'us-east-1') # exit 1 unless list_bucket_objects?(s3_client, 'doc-example-bucket') def list_bucket_objects?(s3_client, bucket_name) puts "Accessing the bucket named '#{bucket_name}'..." objects = s3_client.list_objects_v2( bucket: bucket_name, max_keys: 50 ) if objects.count.positive? puts 'The object keys in this bucket are (first 50 objects):' objects.contents.each do |object| puts object.key end else puts 'No objects found in this bucket.' end return true rescue StandardError => e puts "Error while accessing the bucket named '#{bucket_name}': #{e.message}" return false end

If you don't have a local AWS credentials file, you can still create the Aws::S3::Client resource and run 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.

require 'aws-sdk-s3' # Prints a list of objects in an Amazon S3 bucket. # # Prerequisites: # # - An Amazon S3 bucket. # # @param s3_client [Aws::S3::Client] An initialized Amazon S3 client. # @param bucket_name [String] The bucket's name. # @return [Boolean] true if all operations succeed; otherwise, false. # @example # s3_client = Aws::S3::Client.new(region: 'us-east-1') # exit 1 unless can_list_bucket_objects?(s3_client, 'doc-example-bucket') def list_bucket_objects?(s3_client, bucket_name) puts "Accessing the bucket named '#{bucket_name}'..." objects = s3_client.list_objects_v2( bucket: bucket_name, max_keys: 50 ) if objects.count.positive? puts 'The object keys in this bucket are (first 50 objects):' objects.contents.each do |object| puts object.key end else puts 'No objects found in this bucket.' end return true rescue StandardError => e puts "Error while accessing the bucket named '#{bucket_name}': #{e.message}" end