Updated on 2023-11-09 GMT+08:00

Storage Class

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.

OBS allows you to set storage classes for buckets. The storage class of an object defaults to be that of its residing bucket. There are three types of storage class for buckets, as described in the following table, catering to various storage performance and cost requirements.

Type

Description

Value in OBS Node.js SDK

OBS Standard

Features low access latency and high throughput and is applicable to storing frequently-accessed (multiple times per month) hotspot or small objects (< 1 MB) requiring quick response.

ObsClient.enums.StorageClassStandard

OBS Infrequent Access

Is applicable to storing semi-frequently accessed (less than 12 times a year) data requiring quick response.

ObsClient.enums.StorageClassWarm

OBS Archive

Is applicable to archiving rarely-accessed (once a year) data.

ObsClient.enums.StorageClassCold

For more information, see Bucket Storage Classes.

Setting the Storage Class for a Bucket

You can call ObsClient.setBucketStoragePolicy to set the storage class for a bucket. Sample code is as follows:

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

// Create an ObsClient instance.
var obsClient = new ObsClient({
       //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
       //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.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

obsClient.setBucketStoragePolicy({
       Bucket : 'bucketname',
       StorageClass: obsClient.enums.StorageClassWarm
}, (err, result) => {
       if(err){
              console.error('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});

Use the StorageClass parameter to set the storage class for a bucket.

Obtaining the Storage Class of a Bucket

You can call ObsClient.getBucketStoragePolicy to obtain the storage class of a bucket. Sample code is as follows:

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

// Create an ObsClient instance.
var obsClient = new ObsClient({
       //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
       //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.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

obsClient.getBucketStoragePolicy({
       Bucket : 'bucketname'
}, (err, result) => {
       if(err){
              console.error('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
              if(result.CommonMsg.Status < 300 && result.InterfaceResult){
                     console.log('StorageClass-->' + result.InterfaceResult.StorageClass);
              }
       }
});