All Products
Search
Document Center

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

Last Updated:Feb 28, 2026

Add, view, list, and delete inventory configurations for an OSS bucket using the Node.js SDK.

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, see OSS Action classification.

Prerequisites

Before you begin, make sure that you have:

  • The ali-oss SDK installed

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables configured

  • An existing source bucket (the bucket to inventory)

  • An existing destination bucket in the same region as the source bucket

Client initialization

All examples below use the following client initialization. Replace the placeholder values with your actual bucket name and region.

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

const client = new OSS({
  bucket: 'yourBucketName',
  // Example: 'oss-cn-hangzhou'
  region: 'yourRegion',
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
});

Inventory configuration parameters

The following table describes the inventory configuration parameters.

ParameterTypeRequiredDescription
idStringYesInventory configuration ID.
isEnabledBooleanYesEnables or disables the inventory configuration. Valid values: true, false.
prefixStringNoPrefix to filter objects for the inventory.
OSSBucketDestination.formatStringYesOutput format. Valid value: CSV.
OSSBucketDestination.accountIdStringYesAccount ID of the destination bucket owner.
OSSBucketDestination.rolenameStringYesRAM role for the destination bucket. Example: AliyunOSSRole.
OSSBucketDestination.bucketStringYesDestination bucket name. Must be in the same region as the source bucket.
OSSBucketDestination.prefixStringNoStorage path prefix for the inventory list in the destination bucket.
OSSBucketDestination.encryptionObjectNoServer-side encryption method. Supports SSE-OSS or SSE-KMS.
frequencyStringYesGeneration schedule. Valid values: Daily (once a day), Weekly (once a week).
includedObjectVersionsStringYesObject versions to include. Valid values: All (all versions), Current (current versions only).
optionalFields.fieldArrayNoObject properties to include. Valid values: Size, LastModifiedDate, ETag, StorageClass, IsMultipartUploaded, EncryptionStatus.

Usage notes

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

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

Add an inventory configuration

const inventory = {
  id: 'default',
  isEnabled: false,
  // Filter objects by prefix
  prefix: 'ttt',
  OSSBucketDestination: {
    format: 'CSV',
    accountId: 'yourAccountId',
    rolename: 'AliyunOSSRole',
    bucket: 'yourDestinationBucketName',
    prefix: 'yourPrefix',
    // To encrypt with SSE-OSS:
    // encryption: {'SSE-OSS': ''}

    // To encrypt with SSE-KMS:
    // encryption: {
    //   'SSE-KMS': {
    //     keyId: 'test-kms-id'
    //   }
    // }
  },
  frequency: 'Daily',
  includedObjectVersions: 'All',
  optionalFields: {
    field: [
      'Size',
      'LastModifiedDate',
      'ETag',
      'StorageClass',
      'IsMultipartUploaded',
      'EncryptionStatus',
    ],
  },
};

async function putInventory() {
  const bucket = 'yourBucketName';
  try {
    await client.putBucketInventory(bucket, inventory);
    console.log('Inventory configuration added.');
  } catch (err) {
    console.log('Failed to add inventory configuration:', err.code, err.message);
  }
}

putInventory();

View an inventory configuration

Pass the inventory configuration ID to getBucketInventory to retrieve a specific configuration.

async function getBucketInventoryById() {
  const bucket = 'yourBucketName';
  try {
    const result = await client.getBucketInventory(bucket, 'inventoryid');
    console.log(result.inventory);
  } catch (err) {
    console.log('Failed to get inventory configuration:', err.code, err.message);
  }
}

getBucketInventoryById();

List inventory configurations

A single request returns a maximum of 100 inventory configurations. To retrieve more, pass the nextContinuationToken from the previous response as the second argument.

async function listBucketInventory() {
  const bucket = 'yourBucketName';

  async function getNextPage(nextContinuationToken) {
    const result = await client.listBucketInventory(bucket, nextContinuationToken);
    console.log(result.inventoryList);

    if (result.nextContinuationToken) {
      await getNextPage(result.nextContinuationToken);
    }
  }

  await getNextPage();
}

listBucketInventory();

Delete an inventory configuration

Pass the inventory configuration ID to deleteBucketInventory to remove a specific configuration.

async function deleteBucketInventoryById() {
  const bucket = 'yourBucketName';
  const inventoryId = 'yourInventoryId';
  try {
    await client.deleteBucketInventory(bucket, inventoryId);
    console.log('Inventory configuration deleted.');
  } catch (err) {
    console.log('Failed to delete inventory configuration:', err.code, err.message);
  }
}

deleteBucketInventoryById();

Error handling

Common errors when managing inventory configurations:

Error codeHTTP statusCause
InvalidArgument400A parameter value is invalid or missing.
InventoryExceedLimit400The bucket already has 1,000 inventory rules. Delete an existing rule before adding a new one.
AccessDenied403The RAM user lacks the required inventory permissions.
InventoryAlreadyExist409An inventory rule with the same ID already exists.

References