为 IAM 用户创建用户访问密钥 - 适用于 Ruby 的 Amazon 开发工具包
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

为 IAM 用户创建用户访问密钥

以下示例创建 IAM 用户的访问密钥和私有密钥my_groovy_user中的us-west-2区域。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX - License - Identifier: Apache - 2.0 require 'aws-sdk-iam' # Creates an access key for a user in AWS Identity and Access Management (IAM). # # Prerequisites: # - The user in IAM. # # @param iam [Aws::IAM::Client] An initialized IAM client. # @param user_name [String] The name of the user. # @example # create_access_key(Aws::IAM::Client.new, 'my-user') def create_access_key(iam, user_name) response = iam.create_access_key(user_name: user_name) access_key = response.access_key puts 'Access key created:' puts " Access key ID: #{access_key.access_key_id}" puts " Secret access key: #{access_key.secret_access_key}" puts 'Keep a record of this information in a secure location. ' \ 'This will be the only time you will be able to view the ' \ 'secret access key.' rescue Aws::IAM::Errors::LimitExceeded puts 'Error creating access key: limit exceeded. Cannot create any more. ' \ 'To create more, delete an existing access key, and then try again.' rescue StandardError => e puts "Error creating access key: #{e.message}" end # Full example call: def run_me iam = Aws::IAM::Client.new user_name = 'my-user' puts 'Attempting to create an access key...' create_access_key(iam, user_name) end run_me if $PROGRAM_NAME == __FILE__