ruby/example_code/s3/auth_session_token_request_test.rb
# Full example call:
# Replace us-west-2 with the AWS Region you're using for Amazon S3.
Full example call:
Replace us-west-2 with the AWS Region you're using for Amazon S3.
Full example call : Replace us - west - 2 with the AWS Region you ' re using for Amazon S3 .
def run_me
  user_name = 'my-user'
  region = 'us-west-2'
  iam_client = Aws::IAM::Client.new(region: region)
  user = ''
  role_name = 'AmazonS3ReadOnly'
  role_arn = 'arn:aws:iam::111111111111:role/' + role_name
  role_session_name = 'ReadAmazonS3Bucket'
  duration_seconds = 3600
  sts_client = Aws::STS::Client.new(region: region)
  bucket_name = 'doc-example-bucket'
  puts "Getting or creating user '#{user_name}'..."
  if user_exists?(iam_client, user_name)
    user = get_user(iam_client, user_name)
  else
    user = create_user(iam_client, user_name)
  end
  if user.empty?
    puts "Cannot get or create user '#{user_name}'. Stopping program."
    exit 1
  else
    puts "Got or created user '#{user_name}'."
  end
  puts "Checking whether the role '#{role_name}' exists..."
  if role_exists?(iam_client, role_name)
    puts "The role '#{role_name}' exists."
  else
    puts "The role '#{role_name}' does not exist. Stopping program."
  end
  puts 'Getting credentials...'
  credentials = get_credentials(sts_client, role_arn, role_session_name, duration_seconds)
  if credentials.nil?
    puts 'Cannot get credentials. Stopping program.'
    exit 1
  else
    puts 'Got credentials.'
  end
  s3_client = Aws::S3::Client.new(
    region: region,
    credentials: credentials
  )
  puts "Checking whether the bucket '#{bucket_name}' exists..."
  if bucket_exists?(s3_client, bucket_name)
    puts "The bucket '#{bucket_name}' exists."
  else
    puts "The role '#{bucket_name}' does not exist. Stopping program."
    exit 1
  end
  unless list_objects_in_bucket?(s3_client, bucket_name)
    puts "Cannot access the bucket '#{bucket_name}'. Stopping program."
    exit 1
  end
end
def run_me user_name = 'my-user' region = 'us-west-2' iam_client = Aws :: IAM :: Client . new ( region : region ) user = '' role_name = 'AmazonS3ReadOnly' role_arn = 'arn:aws:iam::111111111111:role/' + role_name role_session_name = 'ReadAmazonS3Bucket' duration_seconds = 3600 sts_client = Aws :: STS :: Client . new ( region : region ) bucket_name = 'doc-example-bucket' puts "Getting or creating user '#{user_name}'..." if user_exists? ( iam_client , user_name ) user = get_user ( iam_client , user_name ) else user = create_user ( iam_client , user_name ) end if user . empty? puts "Cannot get or create user '#{user_name}'. Stopping program." exit 1 else puts "Got or created user '#{user_name}'." end puts "Checking whether the role '#{role_name}' exists..." if role_exists? ( iam_client , role_name ) puts "The role '#{role_name}' exists." else puts "The role '#{role_name}' does not exist. Stopping program." end puts 'Getting credentials...' credentials = get_credentials ( sts_client , role_arn , role_session_name , duration_seconds ) if credentials . nil? puts 'Cannot get credentials. Stopping program.' exit 1 else puts 'Got credentials.' end s3_client = Aws :: S3 :: Client . new ( region : region , credentials : credentials ) puts "Checking whether the bucket '#{bucket_name}' exists..." if bucket_exists? ( s3_client , bucket_name ) puts "The bucket '#{bucket_name}' exists." else puts "The role '#{bucket_name}' does not exist. Stopping program." exit 1 end unless list_objects_in_bucket? ( s3_client , bucket_name ) puts "Cannot access the bucket '#{bucket_name}'. Stopping program." exit 1 end end
Full example call:
Replace us-west-2 with the AWS Region you're using for Amazon S3.
Full example call : Replace us - west - 2 with the AWS Region you ' re using for Amazon S3 .