All Products
Search
Document Center

Java sample

Last Updated: May 28, 2018

Overview

Java SDK consists of the public and product components of Alibaba Cloud Java SDK. The public component relies on aliyun-java-sdk-core, and the product component relies on aliyun-java-sdk-cr. For more information about each interface, see API overview.

Installation

You can use Maven repository to add SDK or download SDK, and add to project dependencies.

Maven dependency example

  1. <!-- Check for the latest version of the SDK using the repository-->
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-cr</artifactId>
  5. <version>3.0.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.aliyun</groupId>
  9. <artifactId>aliyun-java-sdk-core</artifactId>
  10. <version>4.0.1</version>
  11. </dependency>

API call example

  1. import com.aliyuncs.DefaultAcsClient;
  2. import com.aliyuncs.cr.model.v20160607.GetImageLayerRequest;
  3. import com.aliyuncs.exceptions.ClientException;
  4. import com.aliyuncs.http.HttpResponse;
  5. import com.aliyuncs.profile.DefaultProfile;
  6. import com.aliyuncs.profile.IClientProfile;
  7. public class Sample {
  8. public static void main(String[] args) {
  9. try {
  10. // Set a client
  11. DefaultProfile.addEndpoint("cn-hanghzou", "cn-hangzhou", "cr", "cr.cn-hangzhou.aliyuncs.com");
  12. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "AccessKeyId", "AccessKeySecret");
  13. DefaultAcsClient client = new DefaultAcsClient(profile);
  14. // Build a request
  15. GetImageLayerRequest request = new GetImageLayerRequest();
  16. // Set the parameters
  17. request.setRepoName("repoName");
  18. request.setRepoNamespace("repoNamespace");
  19. request.setTag("tag");
  20. // Make a request
  21. HttpResponse response = client.doAction(request);
  22. // Process the results
  23. System.out.println(new String(response.getHttpContent()));
  24. } catch (ClientException e) {
  25. System.out.println("code: " + e.getErrCode());
  26. System.out.println("message: " + e.getErrMsg());
  27. }
  28. }
  29. }