Add, view, list, and delete inventory configurations for an OSS bucket using the Node.js SDK.
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-ossSDK installedThe
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables configuredAn 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.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | String | Yes | Inventory configuration ID. |
isEnabled | Boolean | Yes | Enables or disables the inventory configuration. Valid values: true, false. |
prefix | String | No | Prefix to filter objects for the inventory. |
OSSBucketDestination.format | String | Yes | Output format. Valid value: CSV. |
OSSBucketDestination.accountId | String | Yes | Account ID of the destination bucket owner. |
OSSBucketDestination.rolename | String | Yes | RAM role for the destination bucket. Example: AliyunOSSRole. |
OSSBucketDestination.bucket | String | Yes | Destination bucket name. Must be in the same region as the source bucket. |
OSSBucketDestination.prefix | String | No | Storage path prefix for the inventory list in the destination bucket. |
OSSBucketDestination.encryption | Object | No | Server-side encryption method. Supports SSE-OSS or SSE-KMS. |
frequency | String | Yes | Generation schedule. Valid values: Daily (once a day), Weekly (once a week). |
includedObjectVersions | String | Yes | Object versions to include. Valid values: All (all versions), Current (current versions only). |
optionalFields.field | Array | No | Object 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 code | HTTP status | Cause |
|---|---|---|
InvalidArgument | 400 | A parameter value is invalid or missing. |
InventoryExceedLimit | 400 | The bucket already has 1,000 inventory rules. Delete an existing rule before adding a new one. |
AccessDenied | 403 | The RAM user lacks the required inventory permissions. |
InventoryAlreadyExist | 409 | An inventory rule with the same ID already exists. |