Updated on 2024-01-26 GMT+08:00

Methods (SDK for Java)

If you have any questions during development, post them on the Issues page of GitHub.

If problems occur when using the OBS Java SDK, you can perform the following steps to analyze and locate the problems.

  1. Make sure that the latest version of OBS Java SDK of OBS Java SDK is used.
  2. Make sure that the logging function of OBS Java SDK is enabled. For details about how to enable the function, see the Log Analysis section. The recommended log level is WARN.
  3. Make sure that the program code of the OBS Java SDK complies with Using an OBS Client. All call exceptions of ObsClient APIs are processed as required. The following is a code example of uploading an object:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    ObsClient obsClient = null; 
    try
    {
        String endPoint = "https://your-endpoint";
        // 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");
    
        obsClient = new ObsClient(ak, sk, endPoint);
        HeaderResponse response = obsClient.putObject("bucketname", "objectname", new ByteArrayInputStream("Hello OBS".getBytes())); 
        // (Optional) If a call is successful, record the HTTP status code and request ID returned by the server.
        System.out.println(response.getStatusCode());
        System.out.println(response.getRequestId());
    }
    catch (ObsException e)
    {
        // Recommended: When an exception occurs, record the HTTP status code, server-side error code, and request ID returned by the server.
        System.out.println("HTTP Code: " + e.getResponseCode());
        System.out.println("Error Code:" + e.getErrorCode());
        System.out.println("Request ID:" + e.getErrorRequestId());
        // Recommended: When an exception occurs, record the stack information.
        e.printStackTrace(System.out);
    }
    

    You can click here to view the details about ObsException.

  4. If an exception occurs when an ObsClient API is called, obtain the HTTP status code and OBS server-side error code from ObsException or log file, and compare them to locate the exception cause.
  5. If the exception cause cannot be found in step 4, obtain the request ID returned by the OBS server from ObsException or log file and contact the OBS server O&M team to locate the cause.
  6. If the request ID is unable to be obtained, collect the stack information of ObsException and contact the OBS client O&M team to locate the cause.