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

Java SDK Calling Example

This section describes how to use the Huawei Cloud Java SDK by calling the ExpandShare API of SFS Turbo.

Prerequisites

  • You have obtained the AK and SK corresponding to your Huawei Cloud account. For details, see Creating Access Keys.
  • JDK 1.8 or later has been installed in the environment where the SDK runs, and the Maven environment has 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. For details about the latest SDK version, visit SDK Center.

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

    If a third-party library conflict occurs, 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 cloud service collection package of the SDK. Therefore, you do not need to import the core package and service package separately. According to the Maven dependency parsing sequence, importing the core package and service package may cause the bundle package to fail to take effect.

Using the SDK

The following is a complete example demonstrating how to use the Huawei Cloud Java SDK to call the ExpandShare API for expanding the capacity of a file system.

  • To obtain the SDK sample 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 directly written into the code. You are advised to encrypt the AK and SK and store them in the configuration file or environment variables, and decrypt them when you use them. 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.core.region.Region;
import com.huaweicloud.sdk.sfsturbo.v1.*;
import com.huaweicloud.sdk.sfsturbo.v1.model.*;


public class ExpandShareSolution {

    public static void main(String[] args) {
        // ==================== Initialize the client. ==================== 
        // Obtain the AK/SK from the environment variables. Before running, you need to set the AK/SK as environment variables.
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String iamEndpoint = "https://iam.eu-west-101.myhuaweicloud.eu";
        String endpoint = "https://sfs-turbo.eu-west-101.myhuaweicloud.eu";
        String projectId = "2b31ed520xxxxxxebedb6e57xxxxxxxx";

        // Configure an authentication credential.
        ICredential auth = new BasicCredentials()
                .withIamEndpoint(iamEndpoint)
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

       // Initialize the client.
        SFSTurboClient client = SFSTurboClient.newBuilder()
                .withCredential(auth)
                .withRegion(new Region("eu-west-101", endpoint))
                .build();

        // ==================== Construct a request object. ==================== 
        // Use the request object provided by the SDK to encapsulate request parameters.
        ExpandShareRequest request = new ExpandShareRequest();
        request.withShareId("b959dfbb-xxxx-xxxx-xxxx-8930ec3a87b7");
        ExpandShareRequestBody body = new ExpandShareRequestBody();
        Extend extendbody = new Extend();
        extendbody.withNewSize(500);
        body.withExtend(extendbody);
        request.withBody(body);

        // ==================== Initiate a request and handle the response. ==================== 
        // Use the client to call the corresponding API. If the API is successfully called, a response object is returned. If the API fails to be called, an exception is thrown.
        try {
            ExpandShareResponse response = client.expandShare(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());
        }
    }
}