默认凭证提供程序链为您的应用程序查找可用的凭证。
操作步骤
- 系统属性:
在系统属性里寻找环境凭证,如果定义了alibabacloud.accessKeyId和alibabacloud.accessKeySecret系统属性且不为空,程序将使用它们创建默认凭证。
- 环境变量:
在环境变量里寻找环境凭证,如果定义了ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET环境变量且不为空,程序将使用它们创建默认凭证。
- 配置文件:
如果用户主目录存在默认文件~/.alibabacloud/credentials(Windows为C:\Users\USER_NAME\.alibabacloud\credentials),程序会自动创建指定类型和名称的凭证。默认文件可以不存在,但解析错误会抛出异常。不同的项目、工具之间可以共用这个配置文件,因为不在项目之内,也不会被意外提交到版本控制。
[default] # 默认配置 enable = true # 启用,没有该选项默认不启用 type = access_key # 认证方式为 AccessKey access_key_id = foo # AccessKey ID access_key_secret = bar # AccessKey Secret [client1] # 命名为 `client1` 的配置 type = ecs_ram_role # 认证方式为 EcsRamRole role_name = EcsRamRoleTest # Role Name [client2] # 命名为 `client2` 的配置 enable = false # 不启用 type = ram_role_arn # 认证方式为 RamRoleArn region_id = cn-test # 获取session用的region policy = test # 选填 指定权限 access_key_id = foo access_key_secret = bar role_arn = role_arn role_session_name = session_name # 选填 [client3] # 命名为 `client3` 的配置 type = rsa_key_pair # 认证方式为 RsaKeyPair public_key_id = publicKeyId # Public Key ID private_key_file = /your/pk.pem # Private Key 文件
- 实例RAM角色:
如果定义了环境变量ALIBABA_CLOUD_ECS_METADATA且不为空,程序会将该变量的值作为角色名称,请求
http://100.100.100.200/latest/meta-data/Ram/security-credentials/EcsRamRoleDocumentTesting
获取临时安全凭证。import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.ecs.model.v20140526.*; public class Main { public static void main(String[] args) { IAcsClient client = new DefaultAcsClient("your-region-id"); // 创建API请求并设置参数 DescribeInstancesRequest request = new DescribeInstancesRequest(); request.setPageSize(10); // 发起请求并处理应答或异常 DescribeInstancesResponse response; try { response = client.getAcsResponse(request); for (DescribeInstancesResponse.Instance instance:response.getInstances()) { System.out.println(instance.getPublicIpAddress()); } } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }
在文档使用中是否遇到以下问题
更多建议
匿名提交