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 call is complete using an instance of ObsClient, check whether an exception is thrown. If no, the return value is valid, and an instance of the HeaderResponse 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 { // 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 AccessKeyID and SecretAccessKey. // 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 = new ObsClient(ak, sk, endPoint); // Invoke the interface to perform operations, for example, upload an object. In the command, localfile indicates the path of the local file to be uploaded. You need to specify the file name. HeaderResponse response = obsClient.putObject("bucketname", "objectname", new File("localfile")); Log.i("PutObject", response); } catch (ObsException e) { Log.e("PutObject", "Response Code: " + e.getResponseCode()); Log.e("PutObject", "Error Message: " + e.getErrorMessage()); Log.e("PutObject", "Error Code: " + e.getErrorCode()); Log.e("PutObject", "Request ID: " + e.getErrorRequestId()); Log.e("PutObject", "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) { } } }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot