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 78faa301cc2e3849f73f43da60b6602b
, 496368d73db885d7bf0a8d2901c6ebf7
, a5c5db44bbfcdf9638f44052826bd3f1
, deb1f3be78431abe3b842b323a50434d
, 4763926fbd8b476b5324276b5a2dc7f6
, a93661bfe877cfe28e299d070b8e7c29
, 1c641c8a24ac3350fdd974c3fc7dc092
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 78faa301cc2e3849f73f43da60b6602b
, 496368d73db885d7bf0a8d2901c6ebf7
, a5c5db44bbfcdf9638f44052826bd3f1
and f99cafdf7023d29b56128903a6b65de9
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!