Use the OSS Node.js SDK to add, view, list, and delete inventory configurations for a bucket.
If you use the inventory feature as a RAM (Resource Access Management) user, make sure you have the required permissions. For details, see OSS Action classification.
Add an inventory configuration
Before adding an inventory configuration, note the following limits:
A bucket can have a maximum of 1,000 inventory rules.
The source bucket and the destination bucket must be in the same region. The source bucket is the bucket whose objects are inventoried. The destination bucket is where OSS writes the inventory manifest files.
The following code adds an inventory configuration to a bucket. All examples use CommonJS (require). If your project uses ES modules, replace the require line with import OSS from 'ali-oss'.
const OSS = require('ali-oss');
const client = new OSS({
// Specify the source bucket name.
bucket: 'yourBucketName',
// Specify the region where the source bucket is located, for example, oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
});
const inventory = {
id: 'default', // required — unique ID for the inventory configuration
isEnabled: false, // required — true to enable, false to disable
prefix: 'logs/', // optional — filter objects by key prefix
OSSBucketDestination: {
format: 'CSV', // required — inventory output format; only CSV is supported
accountId: 'yourAccountId', // required — Alibaba Cloud account ID of the destination bucket owner
rolename: 'AliyunOSSRole', // required — RAM role name granted write access to the destination bucket
bucket: 'yourDestBucketName', // required — destination bucket name
prefix: 'inventory-reports/', // optional — key prefix for inventory files in the destination bucket
// To encrypt inventory files with SSE-OSS:
// encryption: { 'SSE-OSS': '' }
// To encrypt with SSE-KMS:
// encryption: { 'SSE-KMS': { keyId: 'your-kms-key-id' } }
},
frequency: 'Daily', // required — 'Daily' or 'Weekly'
includedObjectVersions: 'All', // required — 'All' (all versions) or 'Current' (latest version only)
optionalFields: {
field: [ // optional — additional object attributes to include in the report
'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.error('Failed to add inventory configuration:', err);
}
}
putInventory();
The following table describes the key parameters:
|
Parameter |
Required |
Description |
|
|
Yes |
Unique identifier for the inventory configuration. |
|
|
Yes |
Specifies whether to enable the configuration. Set to |
|
|
No |
Filters the inventory to objects whose keys start with this prefix. |
|
|
Yes |
Output format for the inventory file. Only |
|
|
Yes |
Alibaba Cloud account ID of the destination bucket owner. |
|
|
Yes |
RAM role name with write access to the destination bucket. |
|
|
Yes |
Name of the destination bucket. |
|
|
No |
Key prefix for inventory files stored in the destination bucket. |
|
|
Yes |
Generation schedule. Valid values: |
|
|
Yes |
Object versions to include. |
|
|
No |
Additional attributes to include in the report. Valid values: |
View an inventory configuration
The following code retrieves an inventory configuration by ID.
const OSS = require('ali-oss');
const client = new OSS({
bucket: 'yourBucketName',
region: 'yourRegion',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
});
async function getBucketInventoryById() {
const bucket = 'yourBucketName';
try {
const result = await client.getBucketInventory(bucket, 'yourInventoryId');
console.log(result.inventory);
} catch (err) {
console.error(err);
}
}
getBucketInventoryById();
List inventory configurations
A single request returns a maximum of 100 inventory configurations. To retrieve more, send multiple paginated requests using the nextContinuationToken from each response.
The following code lists all inventory configurations for a bucket, iterating through pages until all results are retrieved.
const OSS = require('ali-oss');
const client = new OSS({
bucket: 'yourBucketName',
region: 'yourRegion',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
});
async function listBucketInventory() {
const bucket = 'yourBucketName';
async function getNextPage(continuationToken) {
const result = await client.listBucketInventory(bucket, continuationToken);
// result shape:
// {
// inventoryList: [ { id, isEnabled, ... }, ... ],
// nextContinuationToken: 'STRING_VALUE' | undefined // undefined means no more pages
// }
console.log(result.inventoryList);
if (result.nextContinuationToken) {
await getNextPage(result.nextContinuationToken);
}
}
await getNextPage();
}
listBucketInventory();
Delete an inventory configuration
The following code deletes an inventory configuration by ID.
const OSS = require('ali-oss');
const client = new OSS({
bucket: 'yourBucketName',
region: 'yourRegion',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
});
async function deleteBucketInventoryById() {
const bucket = 'yourBucketName';
const inventoryId = 'yourInventoryId';
try {
await client.deleteBucketInventory(bucket, inventoryId);
console.log('Inventory configuration deleted.');
} catch (err) {
console.error('Failed to delete inventory configuration:', err);
}
}
deleteBucketInventoryById();
References
For complete sample code, see GitHub examples.
For the API operation to add a bucket inventory configuration, see PutBucketInventory.
For the API operation to view a bucket inventory configuration, see GetBucketInventory.
For the API operation to list bucket inventory configurations, see ListBucketInventory.
For the API operation to delete a bucket inventory configuration, see DeleteBucketInventory.