Help Center> Object Storage Service> Android> Initialization> Creating an Instance of ObsClient
Updated on 2024-05-09 GMT+08:00

Creating an Instance of ObsClient

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference

ObsClient functions as the Android client for accessing OBS. It offers users a series of APIs for interaction with OBS and is used for managing and performing operations on resources, such as buckets and objects, stored in OBS. To use OBS Android SDK to send a request to OBS, you need to initialize an instance of ObsClient and modify the default configurations in ObsConfiguration based on actual needs.

  • If you use the endpoint to create an instance of ObsClient, all parameters are in their default values and cannot be modified. Sample code is as follows:
    • Sample code for creating an instance of ObsClient using permanent access keys (AK/SK):
      // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
      // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
      String ak = System.getenv("ACCESS_KEY_ID");
      String sk = System.getenv("SECRET_ACCESS_KEY_ID");
      String endPoint = "https://your-endpoint";
      // Create an instance of ObsClient.
      ObsClient obsClient = new ObsClient(ak, sk, endPoint);
       
      // Use the instance to access OBS.
              
      // Close ObsClient.
      obsClient.close();
    • Sample code for creating an instance of ObsClient using temporary access keys (AK/SK and security token):
      // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
      // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
      String ak = System.getenv("ACCESS_KEY_ID");
      String sk = System.getenv("SECRET_ACCESS_KEY_ID");
      String endPoint = "https://your-endpoint";
      String securityToken = "*** Provide your Security Token ***"; 
      // Create an instance of ObsClient.
      ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint);
       
      // Use the instance to access OBS.
              
      // Close ObsClient.
      obsClient.close();

OBS is a global service. When obtaining temporary access keys, set the token scope to domain to apply the token to global services. Global services are not differentiated by any project or region.

  • If you use ObsConfiguration to create an instance of ObsClient, you can set configuration parameters as needed during the creation. After the instance is created, the parameters cannot be modified. For parameter details, see Configuring an Instance of ObsClient. Sample code is as follows:
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an instance of ObsConfiguration.
ObsConfiguration config = new ObsConfiguration();
config.setEndPoint(endPoint);
config.setSocketTimeout(30000);
config.setMaxErrorRetry(1);

// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, config);

// Use the instance to access OBS.

// Close ObsClient.
obsClient.close();
  • The project can contain one or more instances of ObsClient.
  • ObsClient is thread-safe and can be simultaneously used by multiple threads.

After you call ObsClient.close to close an instance of ObsClient, the instance cannot be used anymore.