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

コード例

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526. DescribeInstanceStatusRequest;
import com.aliyuncs.ecs.model.v20140526. DescribeInstanceStatusResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;

public class ECSOperate {

	private static final String ACCESS_KEY = "";
	private static final String ACCESS_SECRET = "";
	private static IAcsClient client = null;

	static {
		// Create a profile
		IClientProfile profile = DefaultProfile.getProfile("cn-shenzhen", ACCESS_KEY, ACCESS_SECRET);
		// Create a client instance
		client = new DefaultAcsClient(profile);
	}
	
	public static String describeInstanceStatus(FormatType formatType) {
		// Construct a request
		DescribeInstanceStatusRequest request = new DescribeInstanceStatusRequest();
		request.setAcceptFormat(formatType);
		try {
			//Obtain the returned content
			return client.doAction(request).getHttpContentString();
			//Obtain the returned object
			//return client.getAcsResponse(request);
		} catch (ServerException e) {
			System.out.println("Server exception: " + e);
			e.printStackTrace();
			return null;
		} catch (ClientException e) {
			System.out.println("Parameter configuration error: " + e);
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) {
		System.out.println(describeInstanceStatus(FormatType.JSON));
	}
}