All Products
Search
Document Center

Object Storage Service:Bucket inventory (Node.js SDK)

Last Updated:Nov 29, 2025

This topic describes how to use Node.js to add, view, list, and delete inventory configurations for a bucket.

Important

If you use the inventory feature as a Resource Access Management (RAM) user, make sure that you have the required permissions. For more information about the permissions for the inventory feature, see OSS Action classification.

Add an inventory configuration

Note the following when you add an inventory configuration:

  • A bucket can have a maximum of 1,000 inventory rules.

  • The source bucket and the destination bucket for the manifest file must be in the same region.

The following code shows how to add an inventory configuration to a bucket.

const OSS = require('ali-oss');

const client = new OSS({
  // Set bucket to the name of your bucket.
  bucket: 'yourBucketName',
  // Set region to the region where your bucket is located. For example, if your bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
});

const inventory = {
  // Set the ID of the inventory configuration.
  id: 'default',
  // Specifies whether to enable the inventory configuration. Valid values: true and false.
  isEnabled: false,
  // (Optional) Set a filter rule for the inventory to specify a prefix for filtering objects.
  prefix: 'ttt',
  OSSBucketDestination: {
    // Set the format of the inventory.
    format: 'CSV',
    // Set accountId to the ID of the owner of the destination bucket.
    accountId: 'yourAccountId',
    // Set rolename to the name of the role for the destination bucket.
    rolename: 'AliyunOSSRole',
    // Set bucket to the name of the destination bucket.
    bucket: 'yourBucketName',
    // (Optional) Set prefix to the storage path prefix for the inventory results.
    prefix: 'yourPrefix ',
    // To encrypt the inventory using SSE-OSS, see the following code.
    // encryption: {'SSE-OSS': ''}
    // To encrypt the inventory using SSE-KMS, see the following code.
    /*
    encryption: {
      'SSE-KMS': {
        keyId: 'test-kms-id';
      };
    */
  },
  // Set the generation schedule for the inventory. WEEKLY indicates once a week. DAILY indicates once a day.
  frequency: 'Daily',
  // Specifies whether to include all versions of objects in the inventory results. If you set this parameter to Current, only the current versions of objects are included.
  includedObjectVersions: 'All',
  optionalFields: {
    // (Optional) Set the object properties to include in the inventory.
    field: [
      'Size',
      'LastModifiedDate',
      'ETag',
      'StorageClass',
      'IsMultipartUploaded',
      'EncryptionStatus',
    ],
  },
};

async function putInventory() {
  // Set bucket to the name of the bucket for which you want to add an inventory configuration.
  const bucket = 'yourBucketName';
  try {
    await client.putBucketInventory(bucket, inventory);
    console.log('The inventory configuration is added.');
  } catch (err) {
    console.log('Failed to add the inventory configuration: ', err);
  }
}

putInventory();

View an inventory configuration

The following code shows how to view the inventory configuration of a bucket.

const OSS = require('ali-oss');

const client = new OSS({
  // Set bucket to the name of your bucket.
  bucket: 'yourBucketName',
  // Set region to the region where your bucket is located. For example, if your bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
});

async function getBucketInventoryById() {
  // Set bucket to the name of the bucket whose inventory configuration you want to view.
  const bucket = 'yourBucketName';
  try {
    // View the inventory configuration.
    const result = await client.getBucketInventory(bucket, 'inventoryid');
    console.log(result.inventory);
  } catch (err) {
    console.log(err);
  }
}

getBucketInventoryById();

List inventory configurations

Note

A single request can retrieve a maximum of 100 inventory configurations. To retrieve more than 100 configurations, send multiple requests. Include the token from the previous response in the next request.

The following code shows how to list the inventory configurations of a bucket.

const OSS = require('ali-oss');

const client = new OSS({
  // Set bucket to the name of your bucket.
  bucket: 'yourBucketName',
  // Set region to the region where your bucket is located. For example, if your bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
});

async function listBucketInventory() {
  const bucket = 'yourBucketName'; // Specify the name of the bucket.

  async function getNextPage(nextContinuationToken) {
    // Use nextContinuationToken to get the next page. If you do not provide a token, the first page is retrieved.
    const result = await client.listBucketInventory(bucket, nextContinuationToken);

    // Print the result.
    console.log(result.inventoryList);

    // If there is a next page, recursively call the function.
    if (result.nextContinuationToken) {
      await getNextPage(result.nextContinuationToken);
    }
  }

  // Start retrieving the first page.
  await getNextPage();
}

listBucketInventory();

Delete an inventory configuration

The following code shows how to delete an inventory configuration of a bucket.

const OSS = require('ali-oss');

const client = new OSS({
  // Set bucket to the name of your bucket.
  bucket: 'yourBucketName',
  // Set region to the region where your bucket is located. For example, if your bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
});

async function deleteBucketInventoryById() {
  // Specify the name of the bucket.
  const bucket = 'yourBucketName';
  // Delete the inventory configuration with the specified ID.
  const inventory_id = 'Your InventoryId';
  try {
    await client.deleteBucketInventory(bucket, inventory_id);
  } catch (err) {
    console.log(err);
  }
}

deleteBucketInventoryById();

References

  • For more information about the complete sample code for bucket inventory, see GitHub examples.

  • For more information about the API operation to add a bucket inventory configuration, see PutBucketInventory.

  • For more information about the API operation to view a bucket inventory configuration, see GetBucketInventory.

  • For more information about the API operation to list bucket inventory configurations, see ListBucketInventory.

  • For more information about the API operation to delete a bucket inventory configuration, see DeleteBucketInventory.