以下の例では、 ECS インスタンスの設定を取得するために DescribeInstancesRequest API を呼び出して Alibaba Cloud Java SDK を使用する方法を紹介します。

コード例

package TEST.TEST;
 import com.aliyuncs.profile.DefaultProfile;
 import com.aliyuncs.DefaultAcsClient;
 import com.aliyuncs.IAcsClient;
 import com.aliyuncs.exceptions.ClientException;
 import com.aliyuncs.exceptions.ServerException;
 import com.aliyuncs.ecs.model.v20140526.*;
 public class DescribeInstances {
     public static void main(String[] args) {
         // Create and initiate DefaultAcsClient
         DefaultProfile profile = DefaultProfile.getProfile(
             "cn-hangzhou",          // Region ID
             "your-access-key",      // AccessKey ID
             "your-access-key-secret"); //AccessKey Secret
         IAcsClient client = new DefaultAcsClient(profile);
         // Call an API and configure API parameters
         DescribeInstancesRequest request = new DescribeInstancesRequest();
         request.setPageSize(10);
         request.setConnectTimeout(5000); // Connection timeout in millisecond
         request.setReadTimeout(5000); //Read timeout in millisecond
         // Handle response
         DescribeInstancesResponse response;
         try {
             response = client.getAcsResponse(request);
             for (DescribeInstancesResponse.Instance instance:response.getInstances()) {
                 System.out.println(instance.getImageId());
                 System.out.println(instance.getInstanceId());
                 System.out.println(instance.getPublicIpAddress());
             }
         } catch (ServerException e) {
             e.printStackTrace();
         } catch (ClientException e) {
             e.printStackTrace();
         }    		 
     }
 }