Updated on 2026-09-20 GMT+08:00

Configuring a Bucket Inventory Rule (SDK for Node.js)

Function

This API is used to configure a bucket inventory rule. With this API, object information in a bucket (source) is regularly listed and saved as CSV files. These files are then stored in a specified bucket (destination). In this manner, you can easily manage objects in a bucket. A source bucket can also be the destination bucket. A bucket inventory file contains the following object-related information: object versions, sizes, storage classes, tags, encryption statuses, and last modification time.

You can encrypt bucket inventory files using SSE-KMS.

Restrictions

  • To configure a bucket inventory rule, you must be the bucket owner or have the required permission (obs:bucket:PutBucketInventoryConfiguration granted using IAM or PutBucketInventoryConfiguration granted using a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Creating a Custom Bucket Policy.
  • The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
  • Bucket versions
    • Inventory files can be generated only for OBS 3.0 buckets, but the bucket that stores the inventory files can be any OBS version.
  • Number of bucket inventory rules
    • A bucket can have a maximum of 10 inventory rules.
  • Source and destination buckets
    • The source bucket (for which a bucket inventory rule is configured) and the destination bucket (for storing the generated inventory files) must belong to the same account.
    • The source bucket and the destination bucket must be in the same region.
    • Default encryption cannot be enabled for the destination bucket configured for storing inventory files.
  • Functions
    • Inventory files can only be saved in CSV format.
    • Inventory rules can apply to all objects in a bucket or to a set of objects with the same name prefix.
    • The filter criteria of multiple inventory rules within the same bucket must not overlap.
      • If there is already an inventory rule for all objects in the bucket, any other inventory rule with an object name prefix specified cannot be created. To create a rule for only a set of objects, delete the inventory rule that applies to all objects first.
      • If there is already an inventory rule for a set of objects, a rule for all objects in the bucket cannot be created. To create a rule for all objects, delete all inventory rules that use a prefix filter.
      • Assume there is an inventory rule that uses the ab prefix as a filter. Any new inventory rule whose prefix overlaps with ab, such as a or abc, cannot be created. To create a rule with prefix a or abc, delete the overlapping rule first.
    • Only SSE-KMS can be used to encrypt bucket inventory files.
  • Permissions
    • The OBS system user uploads inventory files to the destination bucket. Therefore, you need to grant the OBS system user the permission to write objects to the destination bucket.
  • Others
    • The bucket inventory function is offered for free, but inventory files are billed for the storage they use.

Method

ObsClient.setBucketInventory(params)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

string

Yes

Explanation:

Bucket name.

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain a period (.) and a hyphen (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets with the same name in the same region, no error will be reported and the bucket properties comply with those set in the first creation request.

Value range:

The value can contain 3 to 63 characters.

Default value:

None

InventoryId

string

Yes

Explanation:

Inventory rule ID.

Restrictions:

It uniquely identifies an inventory rule.

Default value:

None

Id

string

No

Explanation:

Inventory rule ID.

Restrictions:

It uniquely identifies an inventory rule. The value must be the same as that of InventoryId.

Default value:

None

IsEnabled

boolean

Yes

Explanation:

Whether to enable the inventory rule.

Value range:

true: Enable the inventory rule.

false: Disable the inventory rule.

Default value:

None

Filter

object

No

Explanation:

Inventory filter configuration. For details, see Table 2.

Destination

object

Yes

Explanation:

Destination configuration for exporting inventory files. For details, see Table 3.

Schedule

object

Yes

Explanation:

Schedule configuration for exporting inventory files. For details, see Table 4.

IncludedObjectVersions

string

Yes

Explanation:

Whether to include all object versions.

Value range:

  • All: All versions are included.
  • Current: Only the current versions are included.

Default value:

None

OptionalFields

object

No

Explanation:

Optional fields included in the inventory files. For details, see Table 5.

Table 2 Filter

Parameter

Type

Mandatory (Yes/No)

Description

Prefix

string

No

Explanation:

Object name prefix.

Restrictions:

Only objects whose names have this prefix are listed in the inventory files.

Default value:

None

Table 3 Destination

Parameter

Type

Mandatory (Yes/No)

Description

Format

string

Yes

Explanation:

Inventory file format.

Value range:

CSV

Default value:

None

Bucket

string

Yes

Explanation:

Name of the destination bucket for storing the exported inventory files.

Default value:

None

Prefix

string

No

Explanation:

Prefix of the path for storing inventory files in the destination bucket.

Default value:

None

Table 4 Schedule

Parameter

Type

Mandatory (Yes/No)

Description

Frequency

string

Yes

Explanation:

Frequency of exporting inventory files.

Value range:

Daily and Weekly

Default value:

None

Table 5 OptionalFields

Parameter

Type

Mandatory (Yes/No)

Description

Field

array

No

Explanation:

List of optional fields.

Value range:

Size: the object size

LastModifiedDate: the last modification time of an object

StorageClass: the storage class of an object

ETag: the ETag value of an object

IsMultipartUploaded: whether an object is uploaded in a multipart upload

ReplicationStatus: the cross-region replication status of an object

EncryptionStatus: the encryption status of an object

Default value:

None

Responses

Table 6 Responses

Type

Description

Table 7

NOTE:

This API returns a Promise response, which requires the Promise or async/await syntax.

Explanation:

Returned results. For details, see Table 7.

Table 7 Response

Parameter

Type

Description

CommonMsg

ICommonMsg

Explanation:

Common information generated after an API call is complete, including the HTTP status code and error code. For details, see Table 4.

InterfaceResult

Table 5

Explanation:

Results returned for a successful call. For details, see Table 5.

Restrictions:

This parameter is left blank if the value of Status is greater than 300.

Table 8 ICommonMsg

Parameter

Type

Description

Status

number

Explanation:

HTTP status code returned by the OBS server.

Value range:

A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). For details, see Status Codes.

Code

string

Explanation:

Error code returned by the OBS server.

Message

string

Explanation:

Error description returned by the OBS server.

HostId

string

Explanation:

Request server ID returned by the OBS server.

RequestId

string

Explanation:

Request ID returned by the OBS server.

Id2

string

Explanation:

Request ID2 returned by the OBS server.

Indicator

string

Explanation:

Error code details returned by the OBS server.

Table 9 BaseResponseOutput

Parameter

Type

Description

RequestId

string

Explanation:

Request ID returned by the OBS server

Sample Code: Configuring a Bucket Inventory Rule

You can call ObsClient.setBucketInventory to configure a bucket inventory rule. The sample code is as follows:

// Import the OBS library.
// Use npm for installation.
const ObsClient = require("esdk-obs-nodejs");
// Use the source code for installation.
// var ObsClient = require('./lib/obs');

// Create an ObsClient instance.
const obsClient = new ObsClient({
  // Obtain an AK and SK pair using environment variables (recommended) or import it 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,
  // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are not advised to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
  // security_token: process.env.SECURITY_TOKEN,
  // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
  server: "https://obs.ap-southeast-1.myhuaweicloud.com"
});

async function setBucketInventory() {
  try {
    const params = {
      // Specify a bucket name.
      Bucket: "examplebucket",
      // Specify the inventory rule ID (URL parameter).
      InventoryId: "inventory-config-001",
      // Specify the inventory rule ID (XML body field, and the value must be the same as that of InventoryId).
      Id: "inventory-config-001",
      // Specify whether to enable the inventory rule.
      IsEnabled: true,
      // Specify the filter configuration.
      Filter: {
        Prefix: "logs/"
      },
      // Specify the destination configuration.
      Destination: {
        Format: "CSV",
        Bucket: "destination-bucketname",
        Prefix: "inventory-reports/"
      },
      // Specify the export schedule.
      Schedule: {
        Frequency: "Daily"
      },
      // Specify the versions to be included.
      IncludedObjectVersions: "Current",
      // Specify optional fields.
      OptionalFields: {
        Field: ["Size", "LastModifiedDate", "StorageClass"]
      }
    };
    // Configure a bucket inventory rule.
    const result = await obsClient.setBucketInventory(params);
    if (result.CommonMsg.Status <= 300) {
      console.log("Set bucket inventory successful!\n");
      console.log("RequestId: %s", result.CommonMsg.RequestId);
      return;
    };
    console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
    console.log("Status: %d", result.CommonMsg.Status);
    console.log("Code: %s", result.CommonMsg.Code);
    console.log("Message: %s", result.CommonMsg.Message);
    console.log("RequestId: %s", result.CommonMsg.RequestId);
  } catch (error) {
    console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
    console.log(error);
  };
};

setBucketInventory();