AliyunTest.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import com.aliyun.sts20150401.models.AssumeRoleResponse;
  2. import com.aliyun.sts20150401.models.AssumeRoleResponseBody;
  3. import com.aliyun.tea.TeaException;
  4. /**
  5. * @author reghao
  6. * @date 2024-08-30 13:48:52
  7. */
  8. public class AliyunTest {
  9. /**
  10. * <b>description</b> :
  11. * <p>使用AK&amp;SK初始化账号Client</p>
  12. * @return Client
  13. *
  14. * @throws Exception
  15. */
  16. public static com.aliyun.sts20150401.Client createClient() throws Exception {
  17. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  18. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
  19. com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
  20. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  21. .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
  22. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  23. .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
  24. // Endpoint 请参考 https://api.aliyun.com/product/Sts
  25. config.endpoint = "sts.cn-chengdu.aliyuncs.com";
  26. return new com.aliyun.sts20150401.Client(config);
  27. }
  28. public static void test(String... args0) throws Exception {
  29. java.util.List<String> args = java.util.Arrays.asList(args0);
  30. com.aliyun.sts20150401.Client client = createClient();
  31. com.aliyun.sts20150401.models.AssumeRoleRequest assumeRoleRequest = new com.aliyun.sts20150401.models.AssumeRoleRequest();
  32. String roleArn = "acs:ram::1727220085568559:role/hao-sts";
  33. assumeRoleRequest.setRoleArn(roleArn);
  34. assumeRoleRequest.setRoleSessionName("role_session_test");
  35. com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
  36. try {
  37. // 复制代码运行请自行打印 API 的返回值
  38. AssumeRoleResponse roleResponse = client.assumeRoleWithOptions(assumeRoleRequest, runtime);
  39. AssumeRoleResponseBody.AssumeRoleResponseBodyCredentials credentials = roleResponse.getBody().getCredentials();
  40. String id = credentials.getAccessKeyId();
  41. String secret = credentials.getAccessKeySecret();
  42. String expiration = credentials.getExpiration();
  43. String token = credentials.getSecurityToken();
  44. System.out.println();
  45. } catch (TeaException error) {
  46. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  47. // 错误 message
  48. System.out.println(error.getMessage());
  49. // 诊断地址
  50. System.out.println(error.getData().get("Recommend"));
  51. com.aliyun.teautil.Common.assertAsString(error.message);
  52. } catch (Exception _error) {
  53. TeaException error = new TeaException(_error.getMessage(), _error);
  54. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  55. // 错误 message
  56. System.out.println(error.getMessage());
  57. // 诊断地址
  58. System.out.println(error.getData().get("Recommend"));
  59. com.aliyun.teautil.Common.assertAsString(error.message);
  60. }
  61. }
  62. }