Initialization

CSN

  • API Reference
    • API function release records
    • Appendix
    • Bandwidth Package Related
      • Bandwidth package price inquiry
      • Bandwidth upgrade-downgrade for the bandwidth package
      • Bind a bandwidth package to a Cloud Smart Network
      • Create bandwidth package
      • Delete bandwidth package
      • Query bandwidth package list
      • Query the specified bandwidth package details
      • Unbind a bandwidth package from a Cloud Smart Network
      • Update bandwidth package
    • Cloud Smart Network Related
      • Create a Cloud Smart Network
      • Delete a Cloud Smart Network
      • Network instances loaded by the Cloud Smart Network
      • Network instances unloaded by the Cloud Smart Network
      • Query Cloud Smart Network details
      • Query Cloud Smart Network instance list
      • Query the list of Cloud Smart Network
      • Update a Cloud Smart Network
    • Common Headers and Error Responses
    • General Description
    • Ingress Management Related Interfaces
      • Add a routing entry
      • Create affiliation
      • Create learning relationship
      • Delete a learning relationship
      • Delete a routing entry
      • Delete an association relationship
      • Query association relationships
      • Query learning relationships
      • Query route table list
      • Query routing entries
    • Interface Overview
    • Region Bandwidth Related Interfaces
      • Create cross-region bandwidth
      • Delete cross-region bandwidth
      • Query cross-region bandwidth
      • Query the cross-region bandwidth of the specified Cloud Smart Network
      • Update cross-region bandwidth
    • Service domain
    • TGW Related Interfaces
      • Query routing entries of a TGW
      • Query TGW list
      • Update TGW information
  • FAQs
    • Consultation
  • Function Release Records
  • Operation guide
    • Cross-Account Authorization Management
      • Cancel Authorized Network Instance
      • Create Authorized Network Instance
      • Delete Authorized Network Instance
      • View Authorized Network Instance
    • Cross-Region Bandwidth Package Management
    • CSN Instance
      • Create CSN Instance
      • Delete CSN Instance
      • Modify CSN Instance
    • Identity and access management
    • Monitor and Operations
      • CSN Instance Diagnosis
    • Network instance
      • Add Cross-Account Network Instance
      • Add Local Account Network Instance
      • Adjust Network Instance Bandwidth
      • Uninstall Network Instance
    • Region Bandwidth Management
      • Adjust Region Bandwidth
      • Create cross-region bandwidth
      • Delete cross-region bandwidth
    • Route management
      • Association Relationship
        • Create affiliation
        • Delete affiliation
      • Custom route tables
        • Create custom route table
        • Delete custom route table
        • View Custom Route Table
      • Learning Relationship
        • Add Custom Route Entry
        • Create learning relationship
        • Delete Custom Route Entry
        • Delete learning relationship
        • Publish and Revoke Network Instance Route
        • View Route Entry
    • Tag Management
  • Product Description
    • Application scenarios
    • Introduction
    • Usage restrictions
  • Product pricing
  • SDK
    • GO-SDK
      • Bandwidth package
      • CSN Instance
      • Exception handling
      • Initialization
      • Install
      • Overview
      • Region Bandwidth
      • Route management
      • TGW
    • Java-SDK
      • CSN
      • Exception handling
      • Initialization
      • Install the SDK Package
      • Overview
      • Version history
    • Python-SDK
      • Bandwidth package
      • CSN Instance
      • Initialization
      • Region Bandwidth
      • Route management
      • TGW
  • Service Level Agreement (SLA)
    • Cloud Smart Network Service Level Agreement SLA
  • Typical Practices
    • CSN supports VPC custom route tables for secure traffic access
    • Multi-IDC Disaster Recovery via Cloud Smart Network
    • Multi-IDC Interconnection via Cloud Smart Network
    • Private Network VPC Interconnection with Edge Network
All documents
menu
No results found, please re-enter

CSN

  • API Reference
    • API function release records
    • Appendix
    • Bandwidth Package Related
      • Bandwidth package price inquiry
      • Bandwidth upgrade-downgrade for the bandwidth package
      • Bind a bandwidth package to a Cloud Smart Network
      • Create bandwidth package
      • Delete bandwidth package
      • Query bandwidth package list
      • Query the specified bandwidth package details
      • Unbind a bandwidth package from a Cloud Smart Network
      • Update bandwidth package
    • Cloud Smart Network Related
      • Create a Cloud Smart Network
      • Delete a Cloud Smart Network
      • Network instances loaded by the Cloud Smart Network
      • Network instances unloaded by the Cloud Smart Network
      • Query Cloud Smart Network details
      • Query Cloud Smart Network instance list
      • Query the list of Cloud Smart Network
      • Update a Cloud Smart Network
    • Common Headers and Error Responses
    • General Description
    • Ingress Management Related Interfaces
      • Add a routing entry
      • Create affiliation
      • Create learning relationship
      • Delete a learning relationship
      • Delete a routing entry
      • Delete an association relationship
      • Query association relationships
      • Query learning relationships
      • Query route table list
      • Query routing entries
    • Interface Overview
    • Region Bandwidth Related Interfaces
      • Create cross-region bandwidth
      • Delete cross-region bandwidth
      • Query cross-region bandwidth
      • Query the cross-region bandwidth of the specified Cloud Smart Network
      • Update cross-region bandwidth
    • Service domain
    • TGW Related Interfaces
      • Query routing entries of a TGW
      • Query TGW list
      • Update TGW information
  • FAQs
    • Consultation
  • Function Release Records
  • Operation guide
    • Cross-Account Authorization Management
      • Cancel Authorized Network Instance
      • Create Authorized Network Instance
      • Delete Authorized Network Instance
      • View Authorized Network Instance
    • Cross-Region Bandwidth Package Management
    • CSN Instance
      • Create CSN Instance
      • Delete CSN Instance
      • Modify CSN Instance
    • Identity and access management
    • Monitor and Operations
      • CSN Instance Diagnosis
    • Network instance
      • Add Cross-Account Network Instance
      • Add Local Account Network Instance
      • Adjust Network Instance Bandwidth
      • Uninstall Network Instance
    • Region Bandwidth Management
      • Adjust Region Bandwidth
      • Create cross-region bandwidth
      • Delete cross-region bandwidth
    • Route management
      • Association Relationship
        • Create affiliation
        • Delete affiliation
      • Custom route tables
        • Create custom route table
        • Delete custom route table
        • View Custom Route Table
      • Learning Relationship
        • Add Custom Route Entry
        • Create learning relationship
        • Delete Custom Route Entry
        • Delete learning relationship
        • Publish and Revoke Network Instance Route
        • View Route Entry
    • Tag Management
  • Product Description
    • Application scenarios
    • Introduction
    • Usage restrictions
  • Product pricing
  • SDK
    • GO-SDK
      • Bandwidth package
      • CSN Instance
      • Exception handling
      • Initialization
      • Install
      • Overview
      • Region Bandwidth
      • Route management
      • TGW
    • Java-SDK
      • CSN
      • Exception handling
      • Initialization
      • Install the SDK Package
      • Overview
      • Version history
    • Python-SDK
      • Bandwidth package
      • CSN Instance
      • Initialization
      • Region Bandwidth
      • Route management
      • TGW
  • Service Level Agreement (SLA)
    • Cloud Smart Network Service Level Agreement SLA
  • Typical Practices
    • CSN supports VPC custom route tables for secure traffic access
    • Multi-IDC Disaster Recovery via Cloud Smart Network
    • Multi-IDC Interconnection via Cloud Smart Network
    • Private Network VPC Interconnection with Edge Network
  • Document center
  • arrow
  • CSN
  • arrow
  • SDK
  • arrow
  • Java-SDK
  • arrow
  • Initialization
Table of contents on this page
  • Confirm Endpoint
  • Retrieve AK/SK
  • Create CsnClient
  • Create a new CsnClient with AK/SK
  • Create CsnClient with STS
  • Request STS Token
  • Create CsnClient with STS token
  • Configure HTTPS access to CSN
  • Configure CsnClient
  • Use a proxy
  • Set network parameters

Initialization

Updated at:2025-11-11

Confirm Endpoint

Before configuring the endpoint for SDK usage, please refer to the developer guide section on API Service Domain Name to understand Endpoint-related concepts.

Access region Endpoint
Global csn.baidubce.com

Retrieve AK/SK

To use Baidu AI Cloud CSN, you must have a valid AK (Access Key ID) and SK (Secret Access Key) for signature authentication. These AK/SK credentials are system-generated strings used to identify users and authenticate service access. You can obtain and review your AK/SK information using the following steps:

  1. Register a Baidu AI Cloud account
  2. Create AK/SK

Create CsnClient

The CsnClient functions as the interface for CSN services, delivering developers a variety of methods to interact with CSN services.

Create a new CsnClient with AK/SK

Users can refer to the following code to create a client to access CSN with AK/SK:

Java
1public class Sample {
2    public static void main(String[] args) {
3 String ACCESS_KEY_ID =<your-access-key-id>;                   // User’s Access Key ID
4 String SECRET_ACCESS_KEY =<your-secret-access-key>;           // User’s Secret Access Key
5        
6 // Initialize a CsnClient
7        BceClientConfiguration config = new BceClientConfiguration();
8        config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
9        CsnClient client = new CsnClient(config);
10    }
11}

In the code above, ACCESS_KEY_ID corresponds to “Access Key ID” in the console. SECRET_ACCESS_KEY corresponds to “Access Key Secret” in the console. For the method to retrieve them, refer to the Guide - Manage ACCESSKEY.

By default, the above method uses the default domain name as CSN's service address. If you wish to use a custom domain name, input the ENDPOINT parameter accordingly.

Java
1String ACCESS_KEY_ID =<your-access-key-id>;                   // User’s Access Key ID
2 String SECRET_ACCESS_KEY =<your-secret-access-key>;           // User’s Secret Access Key
3 String ENDPOINT = <domain-name>;                               // User-defined domain name
4 BceClientConfiguration config = new BceClientConfiguration();
5 config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
6 config.setEndpoint(ENDPOINT);
7 CsnClient client = new CsnClient(config);

Create CsnClient with STS

Request STS Token

CSN supports temporary third-party access authorization using the STS mechanism. STS (Security Token Service) is a temporary authorization service provided by Baidu AI Cloud. It allows you to issue credentials with customized permissions and validity periods for third-party users, enabling them to directly call Baidu AI Cloud APIs or SDKs to access the cloud resources.

To access CSN via STS, users must first request a certification string through the STS client. For instructions on obtaining STS credentials, refer to Baidu AI Cloud STS Usage Guide。

Create CsnClient with STS token

After requesting the STS token, configure it in the CsnClient. The following codes demonstrate how to create a CsnClient:

Java
1public class StsExample {
2    private static final String STS_ENDPOINT = "http://sts.bj.baidubce.com";
3    private static final String ACCESS_KEY_ID = "your accesskey id";
4    private static final String SECRET_ACCESS_KEY = "your secret accesskey";
5    public static void main(String[] args) {
6        BceCredentials credentials = new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY);
7        StsClient client = new StsClient(
8                new BceClientConfiguration().withEndpoint(STS_ENDPOINT).withCredentials(credentials)
9        );
10        GetSessionTokenResponse response = client.getSessionToken(new GetSessionTokenRequest());
11        // or simply call:
12        // GetSessionTokenResponse response = client.getSessionToken();
13        // or you can specify limited permissions with ACL:
14        // GetSessionTokenResponse response = client.getSessionToken(new GetSessionTokenRequest().withAcl("blabla"));
15        // build DefaultBceSessionCredentials object from response:
16        BceCredentials csnStsCredentials = new DefaultBceSessionCredentials(
17                response.getAccessKeyId(),
18                response.getSecretAccessKey(),
19                response.getSessionToken());
20        System.out.println("==================================");
21        System.out.println("GetSessionToken result:");
22        System.out.println("    accessKeyId:  " + response.getAccessKeyId());
23        System.out.println("    secretAccessKey:  " + response.getSecretAccessKey());
24        System.out.println("    securityToken:  " + response.getSessionToken());
25        System.out.println("    expiresAt:  " + response.getExpiration().toString());
26        System.out.println("==================================");
27        // build CSN client
28        BceClientConfiguration config = new BceClientConfiguration();
29        config.setCredentials(csnStsCredentials);
30        CsnClient client = new CsnClient(config);
31    }
32}

Note: Currently, when configuring a client with STS, regardless of where the corresponding CSN service endpoint is located, the endpoint must be set to http://sts.bj.baidubce.com.

Configure HTTPS access to CSN

CSN supports HTTPS transport protocol. You can use HTTPS to access the CSN service in the CSN Java SDK in the following two ways:

  • Specify HTTPS in the endpoint:
Java
1 String endpoint = "https://csn.baidubce.com";
2 String ak = "ak";
3 String sk = "sk";
4 BceClientConfiguration config = new BceClientConfiguration();
5 config.setCredentials(new DefaultBceCredentials(ak, sk));
6 CsnClient client = new CsnClient(config);
  • Configure HTTPS by calling setProtocol:
Java
1 String endpoint = "csn.baidubce.com"; // Endpoint without protocol
2 String ak = "ak";
3 String sk = "sk";
4 BceClientConfiguration config = new BceClientConfiguration();
5 config.setCredentials(new DefaultBceCredentials(ak, sk));
6 config.setEndpoint(ENDPOINT);
7 config.setProtocol(Protocol.HTTPS); // Defaults to HTTP if unspecified
8 CsnClient client = new CsnClient(config);

Note: If the endpoint already includes a protocol, the one in endpoint takes effect, and the setProtocol() method will be ignored.

Java
1 String endpoint = "http://csn.baidubce.com";
2 String ak = "ak";
3 String sk = "sk";
4 BceClientConfiguration config = new BceClientConfiguration();
5 config.setCredentials(new DefaultBceCredentials(ak, sk));
6 config.setEndpoint(ENDPOINT);    
7 config.setProtocol(Protocol.HTTPS); // Invalid operation if specified in endpoint, applicable to HTTP cases
8 CsnClient client = new CsnClient(config);

Configure CsnClient

To customise parameters for the CsnClient, pass a BceClientConfiguration object during initialization. This allows you to set options such as proxies, maximum connections, and other network parameters.

Use a proxy

The following code snippet enables the client to access CSN service using a proxy:

Java
1String ACCESS_KEY_ID =<your-access-key-id>;                   // User’s Access Key ID
2 String SECRET_ACCESS_KEY =<your-secret-access-key>;           // User’s Secret Access Key
3 String ENDPOINT = <domain-name>;                               // User-defined domain name
4 // Create BceClientConfiguration instance
5 BceClientConfiguration config = new BceClientConfiguration();
6 // Configure proxy to local port 8080
7 config.setProxyHost("127.0.0.1");
8 config.setProxyPort(8080);
9 // Create CSN client
10 config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
11 config.setEndpoint(ENDPOINT);
12 CsnClient client = new CsnClient(config);

All client requests will then be routed through the proxy at 127.0.0.1:8080.

For verified proxies, configure credentials:

Plain Text
1// Create BceClientConfiguration instance
2 BceClientConfiguration config = new BceClientConfiguration();
3    
4 // Configure proxy to local port 8080
5 config.setProxyHost("127.0.0.1");
6 config.setProxyPort(8080);
7    
8 // Set username and password
9 config.setProxyUsername(<username>);                             // Username
10 config.setProxyPassword(<password>);                             // Password

Set network parameters

Users may set the basic network parameters with BceClientConfiguration:

Java
1BceClientConfiguration config = new BceClientConfiguration();
2    
3 // Set maximum number of HTTP connections to 10
4config.setMaxConnections(10);
5    
6 // Set TCP connection timeout to 5,000 milliseconds
7config.setConnectionTimeout(5000);
8    
9 // Set timeout for Socket data transmission to 2,000 milliseconds
10config.setSocketTimeout(2000);

Parameter description The following parameters can be configured via BceClientConfiguration:

Parameters Description
UserAgent User agent, refers to HTTP’s User-Agent header
Protocol Connection protocol type
ProxyDomain Windows domain for NTLM-verified proxy
ProxyHost Proxy server host address
ProxyPort Proxy server port
ProxyUsername Proxy verification username
ProxyPassword Proxy verification password
ProxyPreemptiveAuthenticationEnabled Enable user agent verification or not
ProxyWorkstation NTLM proxy workstation name
LocalAddress Local address
ConnectionTimeoutInMillis Timeout for establishing TCP connections (unit: ms)
SocketTimeoutInMillis Timeout for socket data transmission (unit: ms)
MaxConnections Maximum allowable HTTP connections
RetryPolicy Retry policy for connections
SocketBufferSizeInBytes Buffer size for socket operations

Previous
Exception handling
Next
Install the SDK Package