Huawei Cloud ECS Management Guide: Java Code Samples to Quickly Connect the Interface
Abstract: This article will introduce how to use Java code samples to quickly connect to the interface of Huawei Cloud Elastic Cloud Server (ECS). By studying this article, you will learn how to use Java code to manage ECS instances and implement operations such as creating, querying, starting, and stopping instances. The code examples will help you better understand how to use the SDK provided by Huawei Cloud to operate the ECS interface.
Introduction:
Huawei Cloud Elastic Cloud Server (ECS) provides a series of interfaces to facilitate users to manage ECS instances through code. This article will use Java code examples to introduce how to connect to the interface of Huawei Cloud ECS and implement common operations.
import com.huaweicloud.sdk.core.AuthCredentials; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ClientRequestException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.ecs.v2.EcsClient; import com.huaweicloud.sdk.ecs.v2.model.*; public class CreateEcsExample { public static void main(String[] args) { AuthCredentials credentials = new BasicCredentials() .withAk("<your access key>") .withSk("<your secret key>") .withProjectId("<your project ID>"); EcsClient client = EcsClient.newBuilder() .withCredential(credentials) .withRegion("<your region ID>") .build(); CreateServersRequest request = new CreateServersRequest() .withBody(new CreateServersRequestBody() .withName("test-ecs") .withImageRef("<image ID>") .withFlavorRef("<flavor ID>") .withAvailabilityZone("<availability zone>") .withAdminPass("<admin password>") ); try { CreateServersResponse response = client.createServers(request); System.out.println("Create ECS instance succeeded, ECS ID: " + response.getServerIds()); } catch (ServiceResponseException e) { System.err.println(e.getMessage()); } catch (ClientRequestException e) { System.err.println(e.getMessage()); } } }
In this example, you need Replace <your access key>
, <your secret key>
, <your project ID>
, <your region ID>
, <image ID>
, <flavor ID>
, <availability zone>
and <admin password> ;
for your own information. After running the code, an ECS instance named test-ecs
will be created and the instance ID will be returned.
import com.huaweicloud.sdk.core.AuthCredentials; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ClientRequestException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.ecs.v2.EcsClient; import com.huaweicloud.sdk.ecs.v2.model.*; public class QueryEcsExample { public static void main(String[] args) { AuthCredentials credentials = new BasicCredentials() .withAk("<your access key>") .withSk("<your secret key>") .withProjectId("<your project ID>"); EcsClient client = EcsClient.newBuilder() .withCredential(credentials) .withRegion("<your region ID>") .build(); ListServersDetailsRequest request = new ListServersDetailsRequest(); try { ListServersDetailsResponse response = client.listServersDetails(request); System.out.println("Query ECS instance details succeeded, ECS list: " + response.getServers()); } catch (ServiceResponseException e) { System.err.println(e.getMessage()); } catch (ClientRequestException e) { System.err.println(e.getMessage()); } } }
Similarly, Change <your access key>
, <your secret key>
, <your project ID>
and <your region in the code Replace ID>
with your own information. After running the code, the details of the ECS instance will be returned.
Conclusion:
This article introduces how to use Java code examples to connect to the interface of Huawei Cloud ECS and implement operations such as creating and querying ECS instances. By studying this article, you can quickly get started using the Java SDK provided by Huawei Cloud to achieve flexible management of ECS instances. I hope this article will be helpful to you when using Huawei Cloud ECS!
Reference materials:
The above is the detailed content of Huawei Cloud ECS Management Guide: Java Code Sample Quick Interface. For more information, please follow other related articles on the PHP Chinese website!