All Products
Search
Document Center

Object Storage Service:Manage bucket inventories using OSS SDK for C# 1.0

Last Updated:Mar 20, 2026

Manage inventory configurations for a bucket: add, view, list, and delete inventory rules using the OSS SDK for C#.

Prerequisites

Before you begin, make sure that:

  • The source bucket (for which you configure an inventory rule) and the destination bucket (where the manifest file is stored) are in the same region

  • You have the permissions to add, view, list, and delete inventory configurations. The bucket owner has these permissions by default. To request these permissions, contact the bucket owner

Usage notes

  • The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For more information, see Regions and endpoints.

  • The examples create an OSSClient instance using an OSS endpoint. To initialize OSSClient with a custom domain name or Security Token Service (STS), see Initialization.

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

Add an inventory configuration

All examples use SetBucketInventoryConfiguration to submit the inventory rule. The key parameters are:

ParameterDescription
IdA unique name for the inventory rule
IsEnabledWhether the rule is active. Valid values: true, false
FilterFilters objects by prefix
Destination.OSSBucketDestination.FormatOutput format. Valid value: InventoryFormat.CSV
Destination.OSSBucketDestination.AccountIdAccount ID of the destination bucket owner
Destination.OSSBucketDestination.RoleArnARN of the role with read access to the source bucket and write access to the destination bucket
Destination.OSSBucketDestination.BucketName of the destination bucket
Destination.OSSBucketDestination.PrefixPath prefix for inventory result files in the destination bucket
ScheduleGeneration frequency. Valid values: InventoryFrequency.Daily, InventoryFrequency.Weekly
IncludedObjectVersionsObject versions to include. Valid values: InventoryIncludedObjectVersions.All (all versions, requires versioning enabled), InventoryIncludedObjectVersions.Current (current version only)
OptionalFieldsAdditional object attributes to include in the inventory. Supported values: Size, LastModifiedDate, StorageClass, IsMultipartUploaded, EncryptionStatus, ETag
using Aliyun.OSS;
using Aliyun.OSS.Common;

// Set the endpoint for the region where the bucket is located.
// Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
var endpoint = "yourEndpoint";
// Read credentials from environment variables. Set OSS_ACCESS_KEY_ID and
// OSS_ACCESS_KEY_SECRET before running this example.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the source bucket name.
var bucketName = "examplebucket";
// Account ID of the destination bucket owner.
var accountId = "yourDestinationBucketAccountId";
// ARN of the role that has read access to the source bucket and write access to the destination bucket.
var roleArn = "yourDestinationBucketRoleArn";
// Name of the destination bucket where inventory results are stored.
var destBucketName = "yourDestinationBucketName";
// Specify the region. Example: cn-hangzhou for China (Hangzhou).
const string region = "cn-hangzhou";

var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;

var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);

try
{
    var config = new InventoryConfiguration();
    config.Id = "report1";
    config.IsEnabled = true;
    // Filter objects by prefix.
    config.Filter = new InventoryFilter("filterPrefix");

    config.Destination = new InventoryDestination();
    config.Destination.OSSBucketDestination = new InventoryOSSBucketDestination();
    config.Destination.OSSBucketDestination.Format = InventoryFormat.CSV;
    config.Destination.OSSBucketDestination.AccountId = accountId;
    config.Destination.OSSBucketDestination.RoleArn = roleArn;
    config.Destination.OSSBucketDestination.Bucket = destBucketName;
    // Specify a path prefix for inventory result files.
    config.Destination.OSSBucketDestination.Prefix = "prefix1";

    // Generate inventory daily. Use InventoryFrequency.Weekly for weekly generation.
    config.Schedule = new InventorySchedule(InventoryFrequency.Daily);
    // Include all object versions. Takes effect only when versioning is enabled.
    config.IncludedObjectVersions = InventoryIncludedObjectVersions.All;

    // Specify additional object attributes to include in the inventory.
    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);
}

View an inventory configuration

Retrieve an inventory configuration by its rule ID using GetBucketInventoryConfiguration.

using Aliyun.OSS;
using Aliyun.OSS.Common;

var endpoint = "yourEndpoint";
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
var bucketName = "examplebucket";
// Specify the inventory rule ID to retrieve.
var id = "BucketInventoryConfigurationId";
const string region = "cn-hangzhou";

var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;

var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);

try
{
    var request = new GetBucketInventoryConfigurationRequest(bucketName, id);
    var result = client.GetBucketInventoryConfiguration(request);
    Console.WriteLine("Get bucket:{0} BucketInventoryConfiguration succeeded ", bucketName);
    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);
}

List inventory configurations

ListBucketInventoryConfiguration returns up to 100 inventory configurations per request. For buckets with more than 100 rules, the response includes a NextContinuationToken. Pass the token in the next request to retrieve the following page.

The response fields for pagination are:

FieldTypeDescription
IsTruncatedbooltrue if more results are available; false if this is the last page
NextContinuationTokenstringToken to pass in the next request to retrieve the next page
using Aliyun.OSS;
using Aliyun.OSS.Common;

var endpoint = "yourEndpoint";
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
var bucketName = "examplebucket";
const string region = "cn-hangzhou";

var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;

var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);

try
{
    // Paginate through all inventory configurations.
    // continuationToken is null for the first request.
    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);
        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);
        }
        // IsTruncated: true means more results are available.
        // NextContinuationToken is the token to pass in the next request.
        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);
}

Delete an inventory configuration

Delete an inventory configuration by its rule ID using DeleteBucketInventoryConfiguration.

using Aliyun.OSS;
using Aliyun.OSS.Common;

var endpoint = "yourEndpoint";
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
var bucketName = "examplebucket";
// Specify the inventory rule ID to delete.
var id = "BucketInventoryConfigurationId";
const string region = "cn-hangzhou";

var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;

var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);

try
{
    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);
}

What's next