• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js SDK)
  4. Quick Start
  5. General Examples of ObsClient

General Examples of ObsClient

Except ObsClient.listBuckets (receiving only the callback function parameter) and ObsClient.createV2SignedUrlSync (receiving only the request object parameter), each ObsClient API function receives two parameters: a request object and a callback function. Sample code is as follows:

// Import the OBS library.
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 : 'yourdomainname'
});

// 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 : 'objectkey'
       // Other fields.
};

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

// Call the APIs for object oeprations, such as downloading an object.
obsClient.getObject(requestParam2, callback2);
NOTE:
  • 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.
  • The callback function contains the exception information and SDK common result object in sequence.

The calling result of OBS Node.js SDK is returned in the callback function. 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:

// Import the OBS library.
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 : 'yourdomainname'
});

// Call APIs to perform operations, such as uploading an object. 
obsClient.putObject({
       Bucket : 'bucketname',
       Key : 'objectkey',
       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);
              }
       }
// Close obsClient.
       obsClient.close();
});