以下の例では、セキュリティグループの詳細情報を取得するために DescribeSecurityGroupAttribute API を呼び出して、 Alibaba Cloud Java SDK を使用する方法を説明します。

コード例


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526. DescribeSecurityGroupAttributeRequest;
import com.aliyuncs.ecs.model.v20140526. DescribeSecurityGroupAttributeResponse;
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 describeSecurityGroupAttribute(FormatType formatType) {
		// Construct a request
		DescribeSecurityGroupAttributeRequest request = new DescribeSecurityGroupAttributeRequest();
		request.setSecurityGroupId("security group ID");
		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(describeSecurityGroups(FormatType.JSON));
	}
}