全部产品
阿里云办公

JAVA SDK

更新时间:2018-06-26 10:56:54

您可以基于阿里云 SDK 通过编写代码的方式调用阿里云 API,进而实现对阿里云产品和服务的灵活部署和快速操作。

注意:使用过程中您会需要 AccessKey。您可以在 云账号 AccessKey 管理页面 创建并管理 AccessKey。

环境准备

从 Github 上下载源代码:https://github.com/aliyun/aliyun-openapi-java-sdk/tree/master/aliyun-java-sdk-cds

Maven 方式

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-core</artifactId>
  5. <optional>true</optional>
  6. <version>3.7.1</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>com.aliyun</groupId>
  10. <artifactId>aliyun-java-sdk-cds</artifactId>
  11. <version>1.8.1</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>junit</groupId>
  15. <artifactId>junit</artifactId>
  16. <version>4.12</version>
  17. </dependency>
  18. </dependencies>

Sample

  1. public static void main(String args[]) {
  2. System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
  3. System.setProperty("sun.net.client.defaultReadTimeout", "10000");
  4. try {
  5. DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", "Cds", "cds.cn-beijing.aliyuncs.com");
  6. } catch (ClientException e) {
  7. e.printStackTrace();
  8. }
  9. IClientProfile profile = DefaultProfile.getProfile("cn-beijing", "inputYourAccessKeyID", "inputYourAccessKeySecret");
  10. IAcsClient client = new DefaultAcsClient(profile);
  11. GetJobRequest getJobRequest = new GetJobRequest();
  12. try {
  13. getJobRequest.setAcceptFormat(FormatType.JSON);
  14. getJobRequest.setJobName("test");
  15. getJobRequest.setMethod(MethodType.GET);
  16. GetJobResponse getJobResponse = client.getAcsResponse(getJobRequest);
  17. Assert.assertEquals("test",getJobRequest.getJobName());
  18. } catch (ServerException e) {
  19. e.printStackTrace();
  20. Assert.fail();
  21. } catch (ClientException e) {
  22. e.printStackTrace();
  23. Assert.fail();
  24. }
  25. }
本文导读目录