Updated on 2026-01-27 GMT+08:00

Initializing a DIS SDK Client Instance

You can initialize the DIS SDK client instance using either of the following methods. The first method is recommended. For details about endpoint, ak, sk, region, and projectId, see Obtaining Authentication Information.

  • Use codes to initialize the DIS SDK client instance.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    //Create a DIS client instance.
    DIS dic = DISClientBuilder.standard()
        .withEndpoint("YOUR_ENDPOINT")
        // Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
           // In this example, the AK and SK stored in the environment variables are used for identity authentication. Before running this example, configure environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment.
        .withAk(System.getenv("HUAWEICLOUD_SDK_AK"))
        .withSk(System.getenv("HUAWEICLOUD_SDK_SK"))
        .withProjectId("YOUR_PROJECT_ID")
        .withRegion("YOUR_REGION")
        // Configure the number of retries upon a failure.
        .withProperty(DISConfig.PROPERTY_PRODUCER_RECORDS_RETRIES, "-1")
        .withProperty(DISConfig.PROPERTY_PRODUCER_EXCEPTION_RETRIES, "-1")
        .build();
    
  • Initialize the DIS client if a proxy needs to be used.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    //Create a DIS client instance.
    DIS dic = DISClientBuilder.standard() 
        .withEndpoint("YOUR_ENDPOINT") 
        // Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
           // In this example, the AK and SK stored in the environment variables are used for identity authentication. Before running this example, configure environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment.
        .withAk(System.getenv("HUAWEICLOUD_SDK_AK")) 
        .withSk(System.getenv("HUAWEICLOUD_SDK_SK")) 
        .withProjectId("YOUR_PROJECT_ID") 
        .withRegion("YOUR_REGION") 
    .withProxyHost("YOUR_PROXY_HOST") //Proxy IP address.
    .withProxyPort("YOUR_PROXY_PORT") //Proxy port.
    .withProxyProtocol(Protocol.HTTP) //Proxy protocol. The default value is HTTP.
    .withProxyUsername("YOUR_PROXY_USER_NAME") //Proxy username (optional).
    .withProxyPassword("YOUR_PROXY_PASSWORD") //Proxy password (optional)
        // Configure the number of retries upon a failure.
        .withProperty(DISConfig.PROPERTY_PRODUCER_RECORDS_RETRIES, "-1")
        .withProperty(DISConfig.PROPERTY_PRODUCER_EXCEPTION_RETRIES, "-1")
        .build();
    
  • Initialize the DIS client if you need to set the DIS connection timeout on the client.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    //Create a DIS client instance.
    DIS dic = DISClientBuilder.standard() 
        .withEndpoint("YOUR_ENDPOINT") 
        .withAk(System.getenv("HUAWEICLOUD_SDK_AK")) 
        .withSk(System.getenv("HUAWEICLOUD_SDK_SK")) 
        .withProjectId("YOUR_PROJECT_ID") 
        .withRegion("YOUR_REGION") 
        .withProperty(DISConfig.PROPERTY_CONNECTION_TIMEOUT, "60") // (unit: second)
        .build();
    
  • To enable transmission compression, initialize the DIS client as follows:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    //Create a DIS client instance.
    DIS dic = DISClientBuilder.standard()
        .withEndpoint("YOUR_ENDPOINT")
        .withAk(System.getenv("HUAWEICLOUD_SDK_AK"))
        .withSk(System.getenv("HUAWEICLOUD_SDK_SK"))
        .withProjectId("YOUR_PROJECT_ID")
        .withRegion("YOUR_REGION")
        .withBodyCompressEnabled(true)
    .withBodyCompressType(CompressionType.ZSTD) //Configure the compression algorithm. Currently, lz4 and zstd are supported. The default value is lz4.
        // Configure the number of retries upon a failure.
        .withProperty(DISConfig.PROPERTY_PRODUCER_RECORDS_RETRIES, "-1")
        .withProperty(DISConfig.PROPERTY_PRODUCER_EXCEPTION_RETRIES, "-1")
        .build();
    
  • If data needs to be encrypted before being uploaded to DIS on the client, use the encryption method provided by the DIS SDK. That is, add the DataEncryptEnabled and data.password parameters when building the disclient.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    // Create a DIS client instance.
    DIS dic = DISClientBuilder.standard()
        .withEndpoint("YOUR_ENDPOINT")
        .withAk(System.getenv("HUAWEICLOUD_SDK_AK"))
        .withSk(System.getenv("HUAWEICLOUD_SDK_SK"))
        .withProjectId("YOUR_PROJECT_ID")
        .withRegion("YOUR_REGION")
        .withDataEncryptEnabled(true)
    .withProperty("data.password", "xxx")//xxx indicates the data encryption key configured by the user.
        // Configure the number of retries upon a failure.
        .withProperty(DISConfig.PROPERTY_PRODUCER_RECORDS_RETRIES, "-1")
        .withProperty(DISConfig.PROPERTY_PRODUCER_EXCEPTION_RETRIES, "-1")
        .build();
    

    If JAVA SDK is used to encrypt the uploaded data, you also need to use JAVA SDK to configure the same key for data reading.

  • Use the configuration file to initialize a DIS SDK client instance.

    Add the following configuration items to the dis.properties file in the dis-sdk-demo\src\main\resources directory:

    • ak/sk: AK/SK created on the IAM
    • region: region of the stream
    • endpoint: access address of the DIS
    • projectId: project ID of the stream
    1
    2
    //Create a DIS SDK client instance.
                DIS dic = DISClientBuilder.standard().build();