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 Java client for accessing OBS. It offers callers 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 Java 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 for creating an instance of ObsClient using permanent access keys (AK/SK):
String endPoint = "https://your-endpoint"; String ak = "*** Provide your Access Key ***"; String sk = "*** Provide your Secret Key ***"; // 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):
String endPoint = "https://your-endpoint"; String ak = "*** Provide your Access Key ***"; String sk = "*** Provide your Secret Key ***"; 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();
- Sample code for creating an instance of ObsClient using BasicCredentialsProvider:
String endPoint = "https://your-endpoint"; String ak = "*** Provide your Access Key ***"; String sk = "*** Provide your Secret Key ***"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(new BasicObsCredentialsProvider(ak, sk), endPoint); // Use the instance to access OBS. // Close ObsClient. obsClient.close();
- Sample code for creating an instance of ObsClient using EnvironmentVariableObsCredentialsProvider:
String endPoint = "https://your-endpoint"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(new EnvironmentVariableObsCredentialsProvider(), endPoint); // Use the instance to access OBS. // Close ObsClient. obsClient.close();
In the preceding code, the access keys are found in the system environment variables. You need to define OBS_ACCESS_KEY_ID and OBS_SECRET_ACCESS_KEY in the system environment variables to represent the permanent AK and SK respectively.
- Sample code for creating an instance of ObsClient using EcsObsCredentialsProvider:
String endPoint = "https://your-endpoint"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(new EcsObsCredentialsProvider(), endPoint); // Use the instance to access OBS. // Close ObsClient. obsClient.close();
When an application is deployed on an ECS, the instance of ObsClient created using the preceding methods automatically obtains the temporary access keys from the ECS and updates them periodically.
Ensure that the UTC time of the server is the same as that of the environment where the application is deployed. Otherwise, the temporary access keys may fail to be updated in time.
- Sample code for creating an instance of ObsClient using permanent access keys (AK/SK):
- In addition to the preceding methods, you can also search in sequence to obtain the corresponding access keys from the environment variables and ECSs.
- Sample code for creating an instance of ObsClient using the access keys obtained by searching in sequence:
String endPoint = "https://your-endpoint"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(new OBSCredentialsProviderChain(), endPoint); // Use the instance to access OBS. // Close ObsClient. obsClient.close();
The preceding method specifies that the access keys are searched from the predefined list in sequence. By default, the system provides two predefined search methods: obtaining the access keys from the environment variables and obtaining from ECSs. ObsClient searches for the access keys from the environment variables first and then from ECSs. In this case, ObsClient is created using the first pair of access keys obtained in the search.
- Sample code for creating an instance of ObsClient using the access keys obtained by searching in sequence:
- 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. The preceding methods of creating an instance of ObsClient support ObsConfiguration. The sample code is as follows:
String endPoint = "https://your-endpoint"; String ak = "*** Provide your Access Key ***"; String sk = "*** Provide your Secret Key ***"; // Create an ObsConfiguration instance. 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); // Create an instance of ObsClient using Provider. // ObsClient obsClient = new ObsClient(new EnvironmentVariableObsCredentialsProvider(), config); // ObsClient obsClient = new ObsClient(new EcsObsCredentialsProvider(), 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 any more.
Last Article: Configuring the AK and SK
Next Article: Configuring an Instance of ObsClient
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.