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

前提条件

在使用本教程之前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-ecs-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.17.4</version>
        </dependency>
    </dependencies>

示例代码

本文操作示例主要以代码形式体现,具体代码如下:
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.*;
import com.google.gson.Gson;
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 DescribeDisksResponse describeDisks(FormatType formatType) {
            //构造请求
            DescribeDisksRequest request = new DescribeDisksRequest();
            request.setAcceptFormat(formatType);
            request.setInstanceId("<instance-id>");

            try {
                //获取返回对象
                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(new Gson().toJson(describeDisks(FormatType.JSON)));
        }

     }