阿里云云服务器ECS开发者工具包(SDK)
您可以通过添加Maven依赖或下载Alibaba Cloud SDK for Java工具包的方式安装Alibaba Cloud SDK for Java,详情参见安装Alibaba Cloud SDK for Java。 本教程以调用ECS的Java SDK为例(使用3.7.0版本的Alibaba Cloud SDK for Java核心库和4.11.0版本的ECS Java SDK),您只需在 pom.xml文件中添加以下依赖即可:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ecs</artifactId>
<version>4.11.0</version>
</dependency>
以下代码示例展示了调用Alibaba Cloud SDK for Java的三个主要步骤:
package com.testprogram; import com.aliyuncs.profile.DefaultProfile; 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) { // 创建DefaultAcsClient实例并初始化 DefaultProfile profile = DefaultProfile.getProfile( "<your-region-id>", // 地域ID "<your-access-key-id>", // RAM账号的AccessKey ID "<your-access-key-secret>"); // RAM账号AccessKey Secret IAcsClient client = new DefaultAcsClient(profile); // 创建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(); } } }
项目地址: https://github.com/lihangqi/aliyun-java-sdk-ecs
原文:https://www.cnblogs.com/wellse/p/12198144.html