本示例介绍如何使用Alibaba Cloud SDK for Java调用DescribeDisks接口获取磁盘信息。

示例代码

package TEST.TEST;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.ecs.model.v20140526.*;
 public class DescribeDisks {
	 

		private static final String ACCESS_KEY = "<your-access-key>";
		private static final String ACCESS_SECRET = "<your-access-key_Secret>";
		private static IAcsClient client = null;

		static {
			// 创建profile
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY, ACCESS_SECRET);
			// 创建Acs client
			client = new DefaultAcsClient(profile);
		}
		
		public static FormatType describeDisks(FormatType formatType) {
			//构造请求
			DescribeDisksRequest request = new DescribeDisksRequest();
			request.setAcceptFormat(formatType);
			request.setInstanceId("<instance-ID");
	                request.setRegionId("regionID");
			try {
				//获取返回内容
				return client.doAction(request).getHttpContentType();
				//获取返回对象
				//return client.getAcsResponse(request);
			} catch (ServerException e) {
				e.printStackTrace();
				return null;
			} catch (ClientException e) {
				e.printStackTrace();
				return null;
			}
		}
		
		public static void main(String[] args) {
			System.out.println(describeDisks(FormatType.JSON));
		}
		
     }