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

Result Returned via a Callback Function

ObsClient returns the results by using a callback function that contains two parameters in sequence: the exception information parameter and the SDK common result object parameter. If the exception information parameter in the callback function is not null, an error occurs during the API calling. Otherwise, the API is called. In such conditions, you need to obtain the HTTP status code from the SDK common result object parameter to check whether the operation is successful. Sample code is as follows:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use source codes to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'https://your-endpoint'
});

// Construct request parameters for bucket operations.
var requestParam1 = {
       Bucket : 'bucketname'
       // Other fields.
};

var callback1 = (err, result) => {
       // Process the calling result of the bucket operation.
};

// Call the APIs for bucket operations, such as creating a bucket.
obsClient.createBucket(requestParam1, callback1);

// Construct parameters for object operations.
var requestParam2 = {
       Bucket : 'bucketname',
       Key : 'objectname'
       // Other fields.
};

var callback2 = (err, result) => {
       // Process a request parameter of the bucket operation.
};

// Call an object-related API, such as the API downloading an object.
obsClient.getObject(requestParam2, callback2);

For APIs used for bucket operations, the Bucket parameter contained in the request object indicates the bucket name. For APIs used for object operations, the Bucket and Key parameters contained in the request object specify the bucket name and object name, respectively.

Sample code:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use source codes to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'https://your-endpoint'
});

// Call APIs to perform operations, such as uploading an object. 
obsClient.putObject({
       Bucket : 'bucketname',
       Key : 'objectname',
       Body : 'Hello OBS'
}, (err, result) => {
       // If the err parameter is not null, an error occurs during the API calling.
       if(err){
              console.log('Error-->' + err);
       }else{
              // If the exception information is null, the API call is complete. In such conditions, you need to check the HTTP status code.
              if(result.CommonMsg.Status < 300){// The operation is successful.
                     if(result.InterfaceResult){
                           // Process the business logic after the operation is successful.
                     }
              }else{// The operation fails. Obtain details about the exception.
                     console.log('Code-->' + result.CommonMsg.Code); 
                     console.log('Message-->' + result.CommonMsg.Message);
                     console.log('HostId-->' + result.CommonMsg.HostId);
                     console.log('RequestId-->' + result.CommonMsg.RequestId);
              }
       }
});

Result Returned via the Promise Object

ObsClient supports results returned via the Promise object. If no exception is caught by the catch method of the Promise object, the API calling is complete. In such conditions, you need to obtain the HTTP status code from the SDK Common Result Object to check whether the operation is successful. Sample code is as follows:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use source codes to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'https://your-endpoint'
});

// Construct request parameters for bucket operations.
var requestParam1 = {
       Bucket : 'bucketname'
       // Other fields.
};

// Call the APIs for bucket operations, such as creating a bucket.
var promise1 = obsClient.createBucket(requestParam1);
promise1.then((result) => {
   // Handle the API calling result. 
}).catch((err)=>{
   // Rectify the fault.
});

// Construct request parameters for object operations.
var requestParam2 = {
       Bucket : 'bucketname',
       Key : 'objectname'
       // Other fields.
};

// Call an object-related API, such as the API for downloading an object.
var promise2 = obsClient.getObject(requestParam2, callback2);
promise2.then((result) => {
   // Handle the API calling result.
}).catch((err)=>{
   // Rectify the fault.
});

For APIs used for bucket operations, the Bucket parameter contained in the request object indicates the bucket name. For APIs used for object operations, the Bucket and Key parameters contained in the request object specify the bucket name and object name, respectively.

Sample code:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use source codes to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'https://your-endpoint'
});

// Call APIs to perform operations, such as uploading an object. 
obsClient.putObject({
       Bucket : 'bucketname',
       Key : 'objectname',
       Body : 'Hello OBS'
}).then((result) => {
    // If no exception occurs and the API call is complete, check the HTTP status code.
    if(result.CommonMsg.Status < 300){// Operation succeeded
        if(result.InterfaceResult){
            // Process the business logic after the operation is successful.
        }
}else{// The operation fails. Obtain details about the exception.
        console.log('Code-->' + result.CommonMsg.Code); 
        console.log('Message-->' + result.CommonMsg.Message);
        console.log('HostId-->' + result.CommonMsg.HostId);
        console.log('RequestId-->' + result.CommonMsg.RequestId);
    }
}).catch((err) => {
    // An exception occurred after the API is called.
    console.error('Error-->' + err);
});