本示例介绍如何使用Alibaba Cloud SDK for Java获取实例中的磁盘信息。

前提条件

在使用本教程之前,请确保已完成以下操作:
  • 使用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.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.DescribeDisksRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeDisksResponse;
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.List;

/**
 * DescribeInstances    查询一台或多台实例的详细信息
 * DescribeDisks    查询您已经创建的磁盘。
 */
public class DescribeDisks {

    // 实例名称,支持通配符*
    private static String instanceName = "*001";

    public static void main(String[] args) {
        IAcsClient client = Initialization();
        // 按条件查询实例的详细信息
        List<DescribeInstancesResponse.Instance> instances = DescribeInstances(client);
        // 获取其中需要查询磁盘信息的实例对象
        DescribeInstancesResponse.Instance instance = instances.get(0);
        // 获取实例ID
        String instanceId = instance.getInstanceId();
        TestDescribeDisks(client,instanceId);
    }

    private static void TestDescribeDisks(IAcsClient client, String instanceId) {
        DescribeDisksRequest request = new DescribeDisksRequest();
        request.setInstanceId(instanceId);
        try {
            DescribeDisksResponse response = client.getAcsResponse(request);
            System.out.println("--------------------查询实例下磁盘详细信息--------------------");
            System.out.println(new Gson().toJson(response));
        } catch (ClientException e) {
            System.out.println(e.getErrCode());
            System.out.println(e.getErrMsg());
            System.out.println(e.getRequestId());
        }
    }

    private static List<DescribeInstancesResponse.Instance> DescribeInstances(IAcsClient client) {
        DescribeInstancesRequest request = new DescribeInstancesRequest();
        // 实例名称,支持使用通配符*进行模糊搜索
        request.setInstanceName("*001");
        try {
            DescribeInstancesResponse response = client.getAcsResponse(request);
            System.out.println("--------------------查询实例的详细信息--------------------");
            System.out.println(new Gson().toJson(response));
            return response.getInstances();
        } catch (ClientException e) {
            System.out.println(e.getErrCode());
            System.out.println(e.getErrMsg());
            System.out.println(e.getRequestId());
            throw new RuntimeException();
        }
    }

    /**
     * Initialization  初始化公共请求参数
     */
    private static IAcsClient Initialization() {
        // 初始化请求参数
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>",             // 您的可用区ID
                "<your-access-key-id>",         // 您的AccessKey ID
                "<your-access-key-secret>");    // 您的AccessKey Secret
        return new DefaultAcsClient(profile);
    }
}
				

执行结果

正确的返回结果类似如下:
--------------------查询实例的详细信息--------------------
{
    "requestId": "D3BE4FDB-8AE3-4DC4-854D-3500B6447113", 
    "totalCount": 1, 
    "pageNumber": 1, 
    "pageSize": 10, 
    "instances": [
        {
            "instanceId": "i-bp1332XXXXX", 
            "instanceName": "ECS_Test_001", 
            "description": "", 
            "imageId": "centos_7_05_64_20G_alibase_20181210.vhd", 
            "oSName": "CentOS  7.5 64位", 
            "oSNameEn": "CentOS  7.5 64 bit", 
            "oSType": "linux", 
            "regionId": "cn-hangzhou", 
            "zoneId": "cn-hangzhou-i", 
            "clusterId": "", 
            "instanceType": "ecs.gn6v-c8g1.2xlarge", 
            "cpu": 8, 
            "memory": 32768, 
            "hostName": "iZbp1332junnXXXXXX", 
            "deploymentSetId": "", 
            "status": "Running", 
            "serialNumber": "955be26e-9761-4fde-8b01-3XXXXX", 
            "internetChargeType": "PayByTraffic", 
            "internetMaxBandwidthIn": 2500, 
            "internetMaxBandwidthOut": 50, 
            "vlanId": "", 
            "creationTime": "2019-09-16T07:23Z", 
            "startTime": "2019-09-16T07:23Z", 
            "instanceNetworkType": "vpc", 
            "instanceChargeType": "PrePaid", 
            "saleCycle": "", 
            "expiredTime": "2019-09-23T16:00Z", 
            "autoReleaseTime": "", 
            "ioOptimized": true, 
            "deviceAvailable": true, 
            "instanceTypeFamily": "ecs.gn6v", 
            "gPUAmount": 1, 
            "gPUSpec": "NVIDIA V100", 
            "spotStrategy": "NoSpot", 
            "spotPriceLimit": 0, 
            "resourceGroupId": "", 
            "recyclable": false, 
            "stoppedMode": "Not-applicable", 
            "creditSpecification": "", 
            "deletionProtection": false, 
            "networkInterfaces": [], 
            "operationLocks": [ ], 
            "tags": [ ], 
            "securityGroupIds": [], 
            "publicIpAddress": [], 
            "innerIpAddress": [ ], 
            "rdmaIpAddress": [ ], 
            "vpcAttributes": {}, 
            "eipAddress": {}, 
            "dedicatedHostAttribute": {}, 
            "ecsCapacityReservationAttr": {}, 
            "dedicatedInstanceAttribute": {}
        }
    ]
}
--------------------查询实例下磁盘详细信息--------------------
{
    "requestId": "8F5B4F69-BFCD-40B4-8C24-9CC2B45949C7", 
    "totalCount": 1, 
    "pageNumber": 1, 
    "pageSize": 10, 
    "disks": [
        {
            "diskId": "d-bp19nwaq7XXXXX", 
            "regionId": "cn-hangzhou", 
            "zoneId": "cn-hangzhou-i", 
            "diskName": "", 
            "description": "", 
            "type": "system", 
            "category": "cloud_ssd", 
            "size": 20, 
            "imageId": "centos_7_05_64_20G_alibase_20181210.vhd", 
            "sourceSnapshotId": "", 
            "autoSnapshotPolicyId": "", 
            "productCode": "", 
            "portable": false, 
            "status": "In_use", 
            "instanceId": "i-bp1332junnXXXXX", 
            "device": "/dev/xvda", 
            "deleteWithInstance": true, 
            "deleteAutoSnapshot": false, 
            "enableAutoSnapshot": true, 
            "enableAutomatedSnapshotPolicy": false, 
            "creationTime": "2019-09-16T07:23:35Z", 
            "attachedTime": "2019-09-16T07:23:40Z", 
            "detachedTime": "", 
            "diskChargeType": "PrePaid", 
            "expiredTime": "2019-09-23T16:00Z", 
            "resourceGroupId": "", 
            "encrypted": false, 
            "storageSetId": "", 
            "kMSKeyId": "", 
            "performanceLevel": "", 
            "bdfId": "", 
            "operationLocks": [ ], 
            "mountInstances": [ ], 
            "tags": [ ]
        }
    ]
}