This topic describes how to add, view, list, and delete the inventory configurations of a bucket.

Note Before you can perform these operations, you must have the appropriate permissions on the bucket. By default, these permissions are available only to the bucket owner. If you do not have the permissions to perform these operations, apply for the permissions from the bucket owner.

Add inventory rules

Note the following items when you add inventory rules for a bucket:

  • You can add up to 1,000 inventory rules for a bucket.
  • The inventory list must be stored in a bucket within the same region as the bucket for which the inventory is configured.

The following code provides an example on how to add an inventory configuration for a bucket:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var accountId ="<yourDestinationBucketAccountId>";
var roleArn ="<yourDestinationBucketRoleArn>";
var destBucketName ="<yourDestinationBucketName>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Configure the inventory rule.
    var config = new InventoryConfiguration();
    // Specify the ID of the inventory rule.
    config.Id = "report1";
    // Specify whether inventory is enabled. The value of true indicates that the inventory list is generated, while false indicates that no inventory list is generated.
    config.IsEnabled = true;
    // Specify the rule used to filter the objects to include in the inventory list. The following code provides an example on how to filter the objects by prefix.
    config.Filter = new InventoryFilter("filterPrefix");
    // Configure the destination bucket used to store the generated inventory lists.
    config.Destination = new InventoryDestination();
    config.Destination.OSSBucketDestination = new InventoryOSSBucketDestination();
    // Specify the output format of the inventory lists.
    config.Destination.OSSBucketDestination.Format = InventoryFormat.CSV;
    // Specify the ID of the account that owns the destination bucket.
    config.Destination.OSSBucketDestination.AccountId = accountId;
    // Specify the role ARN of the destination bucket.
    config.Destination.OSSBucketDestination.RoleArn = roleArn;
    // Specify the name of the destination bucket.
    config.Destination.OSSBucketDestination.Bucket = destBucketName;
    // Specify the prefix of the inventory list object.
    config.Destination.OSSBucketDestination.Prefix = "prefix1";
    
    // Specify whether to generate the inventory list on a daily or weekly basis. The following code provides an example on how to generate an inventory list on a weekly basis. Weekly indicates once a week. Daily indicates once a day.
    config.Schedule = new InventorySchedule(InventoryFrequency.Daily);
    // Specify that the inventory list includes only the current version of objects. If you set the InventoryIncludedObjectVersions parameter to All, all versions of objects are included in the inventory list. This configuration takes effect only when versioning is enabled for the bucket.
    config.IncludedObjectVersions = InventoryIncludedObjectVersions.All;
    
    // Specify the object attributes to include in the inventory list.
    config.OptionalFields.Add(InventoryOptionalField.Size);
    config.OptionalFields.Add(InventoryOptionalField.LastModifiedDate);
    config.OptionalFields.Add(InventoryOptionalField.StorageClass);
    config.OptionalFields.Add(InventoryOptionalField.IsMultipartUploaded);
    config.OptionalFields.Add(InventoryOptionalField.EncryptionStatus);
    config.OptionalFields.Add(InventoryOptionalField.ETag);
    var req = new SetBucketInventoryConfigurationRequest(bucketName, config)
    client.SetBucketInventoryConfiguration(req);
    Console.WriteLine("Set bucket:{0} InventoryConfiguration succeeded", bucketName);
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}

For more information about how to add inventory rules for a bucket, see PutBucketInventory.

Query an inventory rule

The following code provides an example on how to view the inventory configurations of a bucket:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var id = "<BucketInventoryConfigurationId>";

// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Query the inventory rule with a specified ID.
    var request = new GetBucketInventoryConfigurationRequest(bucketName, id);
    //Query the inventory rule.
    var result = client.GetBucketInventoryConfiguration(request);
    Console.WriteLine("Get bucket:{0} BucketInventoryConfiguration succeeded ", bucketName);
    // Display the information about the rule.
    Console.WriteLine("bucket InventoryConfiguration id: {0}; bucket InventoryConfiguration IsEnabled: {1}", result.Configuration.Id, result.Configuration.IsEnabled);
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}

For more information about how to query an inventory rule, see GetBucketInventory.

List inventory rules

Note You can obtain up to 100 inventory configurations per request. To obtain more than 100 inventory configurations, you must send multiple requests. Each subsequent request must use the token returned in the previous request.

The following code provides an example on how to list all inventory configurations of a bucket at a time:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var id = "<BucketInventoryConfigurationId>";

// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // List the inventory rules By default, a maximum of 100 rules are listed. If more than 100 rules are configured, the rules are listed in pages. You can use the continuationToken parameter to list the rules on the subsequent page.
    string continuationToken = null;
    bool isTruncated = false;
    do {
        var request = new ListBucketInventoryConfigurationRequest(bucketName, continuationToken)
        var result = client.ListBucketInventoryConfiguration(request);
        Console.WriteLine("List bucket:{0} BucketInventoryConfiguration succeeded ", bucketName);
        // Display the information about the inventory rules.
        for (var i = 0; i < result.Configurations.Count; i++) {
            Console.WriteLine("bucket InventoryConfiguration id: {0}; bucket InventoryConfiguration IsEnabled: {1}", result.Configurations[i].Id, result.Configurations[i].IsEnabled);            
        }
        continuationToken = result.NextContinuationToken;
        isTruncated = result.IsTruncated;
    } while (isTruncated)
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}

For more information about how to list multiple inventory rules at a time, see ListBucketInventory.

Delete an inventory rule

The following code provides an example on how to delete the inventory configurations of a bucket:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var id = "<BucketInventoryConfigurationId>";

// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Delete the inventory rule with the specified ID.
    var request = new DeleteBucketInventoryConfigurationRequest(bucketName, id);
    var result = client.DeleteBucketInventoryConfiguration(request);
    Console.WriteLine("delete bucket:{0} BucketInventoryConfiguration succeeded ", bucketName);
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}

For more information about how to delete an inventory rule configured for a bucket, see DeleteBucketInventory.