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 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);
// 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)
{
}
}
}
Last Article: Deleting an Object
Next Article: Initialization
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.