标签:
进行阿里云sdk开发的前提是已经购买阿里云的相关服务才能调用阿里的相关接口进行开发。最近公司在做云管控的项目,于是进行下摘录总结。
一. 环境准备
阿里云针对不同的开发语言提供不同的sdk,由于项目用的是java语言进行开发,所以下载的是java sdk,介绍的也是java sdk的开发方式。
1.首先下载服务的SDK,阿里云Java SDK支持J2SE Development Kit (JDK) 1.5或以上版本。
2.安装步骤
二.快速入门
1.初始化一个AliyunClient
示例代码如下:
import com.aliyun.api.AliyunClient; import com.aliyun.api.DefaultAliyunClient; public class CreateAliyunClientDemo { private static AliyunClient client; static { String serverUrl = "<serverUrl>"; //例如: http://ecs.aliyuncs.com/ String accessKeyId = "<accessKeyId>"; String accessKeySecret = "<accessKeySecret>"; // 初始化一个AliyunClient client = new DefaultAliyunClient(serverUrl, accessKeyId, accessKeySecret); } public static void main(String[] args) { } }
2.SDK调用示例
调用步骤:
包名称介绍
下面以ECS API 2014-05-26版为例
请求类
响应类
示例代码
创建实例:
public void createInstance() { CreateInstanceRequest createInstanceRequest = new CreateInstanceRequest(); createInstanceRequest.setRegionId("<RegionId>"); createInstanceRequest.setImageId("<ImageId>"); createInstanceRequest.setInstanceType("<InstanceType>"); createInstanceRequest.setSecurityGroupId("<SecurityGroupId>"); try { CreateInstanceResponse createInstanceResponse = client.execute(createInstanceRequest); if (StringUtils.isEmpty(createInstanceResponse.getErrCode())) { //创建成功 String instanceId = createInstanceResponse.getInstanceId(); //获取实例ID } else { String errCode = createInstanceResponse.getErrorCode(); //获取错误码 String message = createInstanceResponse.getMessage(); //获取错误信息 } } catch (ApiException e) { // TODO: handle exception } }
查询实例信息:
public void describeInstanceAttribute(String instanceId) { DescribeInstanceAttributeRequest describeInstanceAttributeRequest = new DescribeInstanceAttributeRequest(); describeInstanceAttributeRequest.setInstanceId(instanceId); try { DescribeInstanceAttributeResponse describeInstanceAttributeResponse = client.execute(describeInstanceAttributeRequest); if (StringUtils.isEmpty(describeInstanceAttributeResponse.getErrorCode())) { //查询成功 //查看实例信息的代码 //...... } else { String errCode = describeInstanceAttributeResponse.getErrorCode(); //获取错误码 String message = describeInstanceAttributeResponse.getMessage(); //获取错误信息 } } catch (ApiException e) { // TODO: handle exception } }
删除实例:
public void deleteInstance(String instanceId) { DeleteInstanceRequest deleteInstanceRequest =new DeleteInstanceRequest(); deleteInstanceRequest.setInstanceId(instanceId); try { DeleteInstanceResponse deleteInstanceResponse=client.execute(deleteInstanceRequest); if (StringUtils.isEmpty(deleteInstanceResponse.getErrorCode())) { //删除成功 } else { //删除失败 String errorCode = deleteInstanceResponse.getErrorCode(); //获取错误码 String message = deleteInstanceResponse.getMessage(); //获取错误信息 } } catch (ApiException e) { // TODO: handle exception } }
标签:
原文地址:http://www.cnblogs.com/longshiyVip/p/4563746.html