This topic describes how to use Alibaba Cloud SDK for Java to query disk information of an Elastic Compute Service (ECS) instance.

Prerequisites

Before you begin, ensure that the following requirements are met:
  • You must have an Alibaba Cloud account and an AccessKey pair (AccessKey ID and AccessKey secret) to use Alibaba Cloud SDK for Java. You can create and view your AccessKey pair on the AccessKey Management page in the Alibaba Cloud Management Console.
  • You have installed Alibaba Cloud SDK for Java. For more information about SDK versions, see Alibaba Cloud 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>

Sample code

This section shows the sample request code:
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    Queries the details of one or more instances.
 * DescribeDisks    Queries the disks that you have created.
 */
public class DescribeDisks {

    // The corresponding instance name, which supports the wildcard (*).
    private static String instanceName = "*001";

    public static void main(String[] args) {
        IAcsClient client = Initialization();
        // Query instance details by conditions.
        List<DescribeInstancesResponse.Instance> instances = DescribeInstances(client);
        // Query the instances whose disk information you want to query.
        DescribeInstancesResponse.Instance instance = instances.get(0);
        // Query an instance 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("--------------------The disk details of the instance were queried.--------------------");
            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();
        // The corresponding instance name, which supports fuzzy search by using the wildcard (*) .
        request.setInstanceName("*001");
        try {
            DescribeInstancesResponse response = client.getAcsResponse(request);
            System.out.println("--------------------The details of the instance were queried.--------------------");
            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  Initializes common request parameters.
     */
    private static IAcsClient Initialization() {
        // Initialize request parameters.
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>",             // The ID of the region.
                "<your-access-key-id>",         // The AccessKey ID.
                "<your-access-key-secret>"); // The AccessKey secret.
        return new DefaultAcsClient(profile);
    }
}
                

Output

This section shows the sample success response:
--------------------The details of the instance were queried.--------------------
{
    "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-bit", 
            "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": {}
        }
    ]
}
--------------------The disk details of the instance were queried.--------------------
{
    "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": [ ]
        }
    ]
}