This topic describes how to add an inventory for a bucket and how to query, list, and delete inventories configured for a bucket.

Usage notes

  • Make sure that you have permissions to create, view, list, and delete inventories for the bucket. By default, the bucket owner have permissions to perform these operations. If you do not have permissions to perform these operations, apply for the permissions from the bucket owner.
  • You can configure up to 1,000 inventories for a bucket.
  • Inventory lists must be stored in a bucket in the same region as the bucket for which the inventory feature is configured.

Create an inventory for a bucket

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

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

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
var endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. 
var bucketName = "examplebucket";
// Specify the account ID granted by the bucket owner to perform the PutBucketInventory operation. 
var accountId ="yourDestinationBucketAccountId";
// Specify the RAM role for which you want to configure inventories. The RAM role must have permissions to read the source bucket for which you want to configure the inventory and permissions to write data to the destination bucket in which you want to store the generated inventory lists. 
var roleArn ="yourDestinationBucketRoleArn";
// Specify the name of the bucket in which you want to store the generated inventory lists. 
var destBucketName ="yourDestinationBucketName";
// Create an OSSClient instance. 
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Create an inventory for the bucket. 
    var config = new InventoryConfiguration();
    // Specify the inventory name. 
    config.Id = "report1";
    // Specify whether inventory is enabled for the bucket. Valid values: true and false. If you set this parameter to true, the inventory is enabled. 
    config.IsEnabled = true;
    // Specify the rule used to filter the objects to include in the inventory lists. 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 format of the inventory lists. 
    config.Destination.OSSBucketDestination.Format = InventoryFormat.CSV;
    // Specify the ID of the Alibaba Cloud 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 in which the generated inventory lists are stored. 
    config.Destination.OSSBucketDestination.Bucket = destBucketName;
    // Specify the prefix of the path in which you want to store the generated inventory lists. 
    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.  
    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 lists. 
    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);
}

Query an inventory configured for a bucket

The following code provides an example on how to query inventories configured for a bucket:

using Aliyun.OSS;
using Aliyun.OSS.Common;
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
var endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. 
var bucketName = "examplebucket";
// Specify the inventory name. 
var id = "BucketInventoryConfigurationId";

// Create an OSSClient instance. 
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Query the inventory with a specified ID. 
    var request = new GetBucketInventoryConfigurationRequest(bucketName, id);
    //Query the inventory. 
    var result = client.GetBucketInventoryConfiguration(request);
    Console.WriteLine("Get bucket:{0} BucketInventoryConfiguration succeeded ", bucketName);
    // Display the information about the inventory. 
    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 multiple inventories configured for a bucket

Note You can query up to 100 inventories by sending a request. To query more than 100 inventory rules, you must send multiple requests and use the token returned for each request as the parameter for the next request.

The following code provides an example on how to list multiple inventories configured for a bucket at a time:

using Aliyun.OSS;
using Aliyun.OSS.Common;
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
var endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. 
var bucketName = "examplebucket";
// Specify the inventory name. 
var id = "BucketInventoryConfigurationId";

// Create an OSSClient instance. 
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // List the inventories. By default, a maximum of 100 inventories are listed. If more than 100 inventories are configured, the inventories are listed in pages. You can use the continuationToken parameter to list the inventories 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. 
        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);
}

Delete an inventory configured for a bucket

The following code provides an example on how to delete an inventory configured for a bucket:

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

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
var endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. 
var bucketName = "examplebucket";
// Specify the inventory name. 
var id = "BucketInventoryConfigurationId";

// Create an OSSClient instance. 
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Delete the inventory 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);
}

References

  • For more information about the API operation that is called to create an inventory, see PutBucketInventory.
  • For more information about the API operation that is called to query inventories configured for a bucket, see GetBucketInventory.
  • For more information about the API operation that is called to list multiple inventories configured for a bucket, see ListBucketInventory.
  • For more information about the API operation that is called to delete inventories configured for a bucket, see DeleteBucketInventory.