Help Center> Object Storage Service> SDK Reference> Java> Quick Start> General Examples of ObsClient

General Examples 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

After an API calling is complete using an instance of ObsClient, view whether an exception is thrown. If no, the return value is valid and an instance of the HeaderResponse class (or of its sub-class) is returned. If yes, obtain the error information from the instance of ObsException.

Sample code:

// You can reserve only one global instance of ObsClient in your project.
// ObsClient is thread-safe and can be simultaneously used by multiple threads.
ObsClient obsClient = null; 
try
{
    String endPoint = "https://your-endpoint";
    String ak = "*** Provide your Access Key ***";
    String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
    obsClient = new ObsClient(ak, sk, endPoint);
    // Call APIs to perform related operations, for example, uploading an object.
    HeaderResponse response = obsClient.putObject("bucketname", "objectname", new File("localfile"));  // localfile indicates the path of the local file to be uploaded. You need to specify the file name.
    System.out.println(response);
}
catch (ObsException e)
{
    System.out.println("HTTP Code: " + e.getResponseCode());
    System.out.println("Error Code:" + e.getErrorCode());
    System.out.println("Error Message: " + e.getErrorMessage());
    
    System.out.println("Request ID:" + e.getErrorRequestId());
    System.out.println("Host ID:" + e.getErrorHostId());
}finally{
    // Close the instance of ObsClient. If this instance is a global one, you do not need to close it every time you complete calling a method.
    // After you call the ObsClient.close method to close an instance of ObsClient, the instance cannot be used any more.
    if(obsClient != null){
        try
        {
            // obsClient.close();
        }
        catch (IOException e)
        {
        }
    }
}