全部产品
云市场

Java示例

更新时间:2018-05-23 00:30:52

简介

Java SDK包含阿里云Java SDK公共部分和产品部分,公共部分依赖aliyun-java-sdk-core, 产品部分依赖aliyun-java-sdk-cr。每个接口的详细使用方法请参考API概览

安装

您可以使用Maven仓库引入SDK或下载SDK后添加到项目依赖中。

Maven依赖示例

  1. <!-- 请通过仓库查询SDK最新版本号-->
  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调用示例

  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. // 设置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. // 构造请求
  15. GetImageLayerRequest request = new GetImageLayerRequest();
  16. // 设置参数
  17. request.setRepoName("repoName");
  18. request.setRepoNamespace("repoNamespace");
  19. request.setTag("tag");
  20. // 发起请求
  21. HttpResponse response = client.doAction(request);
  22. // 处理结果
  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. }