Updated on 2026-11-26 GMT+08:00

Java SDK Usage Example

This section describes how to use the Huawei Cloud Java SDK by calling the CreateInstanceNamespace API of SWR.

Prerequisites

  • You have obtained the access key (AK) and secret access key (SK) corresponding to your Huawei Cloud account. For details, see Creating Access Keys.
  • JDK 1.8 or a later version has been installed in the environment where the SDK runs, and the Maven environment has also been installed and configured.

Installing the SDK

You are advised to install the Huawei Cloud Java SDK using Maven. The following uses Maven as an example.

Open the pom.xml file of the Maven project and add the following dependency configuration under the <dependencies> node. Maven will automatically download the required SDK package.

The Java SDK 3.1.219 is used as an example. You can obtain the latest SDK version from SDK Center.

  • Installing the SDK package of a single cloud service
    <dependency> 
        <groupId>com.huaweicloud.sdk</groupId> 
        <artifactId>huaweicloud-sdk-swr</artifactId> 
        <version>3.1.219</version> 
    </dependency>
  • Installing the SDK package of a cloud service collection
    <dependency> 
        <groupId>com.huaweicloud.sdk</groupId> 
        <artifactId>huaweicloud-sdk-all</artifactId> 
        <version>3.1.219</version> 
    </dependency>
  • Installing the SDK bundle package of a cloud service collection

    If there is a third-party library conflict, you can import the bundle package (3.0.40-rc or later). It contains all supported services and redirects the third-party software that the SDK depends on, preventing conflicts with the libraries that the service itself depends on.

    <dependency> 
        <groupId>com.huaweicloud.sdk</groupId> 
        <artifactId>huaweicloud-sdk-bundle</artifactId> 
        <version>3.1.219</version> 
    </dependency>

The bundle package already contains the core package and the SDK package of all cloud services. There is no need to import the core package and service package separately. Due to the Maven dependency resolution order, adding the core package and the service package may prevent the bundle package from taking effect.

Using the SDK

The following is a complete example demonstrating how to use the Huawei Cloud Java SDK to call the CreateInstanceNamespace API.

  • To obtain the SDK example code for other APIs, see Obtaining SDK Sample Code.
  • This example provides only the basic API parameter settings. For more advanced settings, see the User Manual of the SDK.
  • There will be huge security risks if the AK and SK used for authentication are written into the code. You are advised to encrypt the AK and SK and store them in a configuration file or environment variables, and decrypt them when needed to ensure security. In this example, the AK and SK are stored as the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK. For details, see Environment Variables.
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.swr.v2.region.SwrRegion;
    import com.huaweicloud.sdk.swr.v2.*;
    import com.huaweicloud.sdk.swr.v2.model.*;
    
    
    public class CreateInstanceNamespaceSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            SwrClient client = SwrClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(SwrRegion.valueOf("cn-north-4"))
                    .build();
            CreateInstanceNamespaceRequest request = new CreateInstanceNamespaceRequest();
            request.withInstanceId("instance_id");
            CreateInstanceNamespaceRequestBody body = new CreateInstanceNamespaceRequestBody();
            NamespaceMetadata metadatabody = new NamespaceMetadata();
            metadatabody.withPublic(NamespaceMetadata.PublicEnum.fromValue("true"));
            body.withMetadata(metadatabody);
            body.withNamespaceName("test");
            request.withBody(body);
            try {
                CreateInstanceNamespaceResponse response = client.createInstanceNamespace(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }