English | 简体中文
The HuaweiCloud Java SDK allows you to easily work with Huawei Cloud services such as Elastic Compute Service (ECS) and Virtual Private Cloud (VPC) without the need to handle API related tasks.
This document introduces how to obtain and use HuaweiCloud Java SDK.
-
To use HuaweiCloud Java SDK, you must have Huawei Cloud account as well as the Access Key and Secret key of the HuaweiCloud account. The accessKey is required when initializing {Service}Client. You can create an AccessKey in the Huawei Cloud console. For more information, see My Credentials.
-
HuaweiCloud Java SDK requires Java JDK 1.8 or later.
You could obtain and install Java SDK through following methods:
- Use Maven to declare dependencies (Recommended)
It is recommended to use Maven to declare dependencies to use our Java SDK. At first, you need to download and install Maven, and then declare dependencies in the pom.xml file in your Java project.
<dependency>
<groupId>com.huaweicloud.sdk</groupId>
<artifactId>huaweicloud-sdk</artifactId>
<!-- please find the latest version in the Maven Repository -->
<version>3.0.1-beta</version>
</dependency>- Use source code
- Download our source code package on Github.
- Unzip the source code package and apply it for a necessary location in your project.
- For specific usage, please refer to [Code Example](#Code Example).
- Import the required modules as follows:
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.exception.ServerResponseException;
import com.huaweicloud.sdk.vpc.v2.VpcClient;
import com.huaweicloud.sdk.vpc.v2.model.ListVpcsRequest;
import com.huaweicloud.sdk.vpc.v2.model.ListVpcsResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;-
Config the
{Service}Clientconfigurations:HttpConfig config = HttpConfig.getDefaultHttpConfig(); config.withProxyHost("http://proxy.huaweicloud.com") .withProxyPort(8080) .withProxyUsername("test") .withProxyPassword("test"); config.withIgnoreSSLVerification(true);
-
Initialize the
{Service}Clientinstance:BasicCredentials auth = new BasicCredentials() .withAk(ak) .withSk(sk) .withProjectId(projectId); VpcClient vpcClient = VpcClient.newBuilder() .withHttpConfig(config) .withCredential(auth) .withEndpoint(endpoint) .build();
where:
akis the access key ID for your account.skis the secret access key for your account.projectIdis the ID of your project depending on your region which you want to operate.endpointis the service specific endpoints, see Regions and Endpoints
-
Send a request and print response.
ListVpcsResponse listVpcsResponse = vpcClient.listVpcs(new ListVpcsRequest().withLimit(1)); logger.info(listVpcsResponse.toString());
The following example shows how to query a list of VPC in a specific region. Substitute the values for {your ak string}, {your sk string}, {your endpoint string} and {your project id}.
package com.huaweicloud.sdk.test;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.exception.ServerResponseException;
import com.huaweicloud.sdk.vpc.v2.VpcClient;
import com.huaweicloud.sdk.vpc.v2.model.ListVpcsRequest;
import com.huaweicloud.sdk.vpc.v2.model.ListVpcsResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
HttpConfig config = HttpConfig.getDefaultHttpConfig();
config.withIgnoreSSLVerification(true);
String ak = "{your ak string}";
String sk = "{your sk string}";
String endpoint = "{your endpoint string}";
String projectId = "{your project id}";
BasicCredentials auth = new BasicCredentials()
.withAk(ak)
.withSk(sk)
.withProjectId(projectId);
VpcClient vpcClient = VpcClient.newBuilder()
.withHttpConfig(config)
.withCredential(auth)
.withEndpoint(endpoint)
.build();
try {
ListVpcsResponse listVpcsResponse = vpcClient.listVpcs(new ListVpcsRequest().withLimit(1));
logger.info(listVpcsResponse.toString());
} catch (ClientRequestException e) {
logger.error("HttpStatusCode:" + e.getHttpStatusCode() + ", ErrorMsg: " + e.getErrorMsg());
} catch (ServerResponseException e) {
logger.error("HttpStatusCode:" + e.getHttpStatusCode() + ", ErrorMsg: " + e.getErrorMsg());
} catch (ConnectionException e) {
logger.error("Connection error: ", e);
}
}
}