This topic describes how to create an inventory for a bucket and how to query, list, and delete the inventories configured for a bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
Ensure that you have the permissions to add, view, list, and delete inventory configurations. The bucket owner has these permissions by default. If you do not have these permissions, you must request them from the bucket owner.
You can configure a maximum of 1,000 inventory rules for a single bucket.
The source bucket for which you configure an inventory rule and the destination bucket where the manifest file is stored must be in the same region.
Add an inventory configuration
The following code shows how to add an inventory configuration for a bucket.
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Set endpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the AccountId granted by the bucket owner.
var accountId ="yourDestinationBucketAccountId";
// Specify the name of the role that has permissions to read all files in the source bucket and write files to the destination bucket.
var roleArn ="yourDestinationBucketRoleArn";
// Specify the name of the bucket where the inventory results are stored.
var destBucketName ="yourDestinationBucketName";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Set the signature version to V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Add a bucket inventory.
var config = new InventoryConfiguration();
// Set the inventory rule name.
config.Id = "report1";
// A flag that indicates whether the inventory configuration is enabled. Valid values: true and false. Set the value to true to enable the inventory configuration.
config.IsEnabled = true;
// Set the inventory filter rule to filter objects by prefix.
config.Filter = new InventoryFilter("filterPrefix");
// Create a destination bucket configuration for the inventory.
config.Destination = new InventoryDestination();
config.Destination.OSSBucketDestination = new InventoryOSSBucketDestination();
// Set the inventory format.
config.Destination.OSSBucketDestination.Format = InventoryFormat.CSV;
// The AccountId of the user who owns the destination bucket where the inventory results are stored.
config.Destination.OSSBucketDestination.AccountId = accountId;
// The roleArn for the destination bucket where the inventory results are stored.
config.Destination.OSSBucketDestination.RoleArn = roleArn;
// The name of the destination bucket where the inventory results are stored.
config.Destination.OSSBucketDestination.Bucket = destBucketName;
// Set the storage path prefix for the inventory results.
config.Destination.OSSBucketDestination.Prefix = "prefix1";
// Set the generation schedule for the inventory. The following example is for weekly generation. Weekly corresponds to weekly generation, and Daily corresponds to daily generation.
config.Schedule = new InventorySchedule(InventoryFrequency.Daily);
// Set the object versions to include in the inventory to the current version. If you set this to InventoryIncludedObjectVersions.All, all object versions are included. This takes effect only when versioning is enabled.
config.IncludedObjectVersions = InventoryIncludedObjectVersions.All;
// Set the object properties 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
The following code provides an example on how to query an inventory configured for a bucket:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Set endpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the inventory rule name.
var id = "BucketInventoryConfigurationId";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Set the signature version to V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// View the inventory configuration information for the specified ID.
var request = new GetBucketInventoryConfigurationRequest(bucketName, id);
// Get the bucket inventory.
var result = client.GetBucketInventoryConfiguration(request);
Console.WriteLine("Get bucket:{0} BucketInventoryConfiguration succeeded ", bucketName);
// Print the bucket inventory information.
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
You can query up to 100 inventories in a single request. To query more than 100 inventories, 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 inventories configured for a bucket:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Set endpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the inventory rule name.
var id = "BucketInventoryConfigurationId";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Set the signature version to V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// List the inventory configuration items. By default, a maximum of 100 results are listed at a time. If there are more than 100 configurations, the results are returned in pages. Pass the token to list the next 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);
//Print the bucket inventory information.
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 configuration
The following code provides an example on how to delete an inventory configured for a bucket:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Set endpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run this sample code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the inventory rule name.
var id = "BucketInventoryConfigurationId";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Set the signature version to V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Delete the inventory configuration 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 for creating an inventory for a bucket, see PutBucketInventory.
For more information about the API operation for querying an inventory configured for a bucket, see GetBucketInventory.
For more information about the API operation for listing inventories configured for a bucket, see ListBucketInventory.
For more information about the API operation for deleting the inventories configured for a bucket, see DeleteBucketInventory.