本文档介绍如何添加、查看、列举和删除存储空间(Bucket)的清单(Inventory)配置。
说明 请确保您拥有调用以上操作的权限。Bucket所有者默认拥有此类权限,若您无此类权限,请先向Bucket所有者申请对应操作的权限。
添加清单配置
添加清单配置时,有如下注意事项:
- 单个Bucket最多只能配置1000条清单规则。
- 配置清单的源Bucket与存放导出的清单文件所在的目标Bucket必须位于同一个Region。
以下代码用于为某个Bucket添加清单(Inventory)配置:
using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var accountId ="<yourDestinationBucketAccountId>";
var roleArn ="<yourDestinationBucketRoleArn>";
var destBucketName ="<yourDestinationBucketName>";
// 创建OSSClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
//设置Bucket清单。
var config = new InventoryConfiguration();
// 设置清单配置id。
config.Id = "report1";
// 清单配置是否启用的标识, true或false。
config.IsEnabled = true;
// 设置清单筛选规则,指定筛选object的前缀。
config.Filter = new InventoryFilter("filterPrefix");
// 创建清单的bucket目的地配置。
config.Destination = new InventoryDestination();
config.Destination.OSSBucketDestination = new InventoryOSSBucketDestination();
// 设置清单格式。
config.Destination.OSSBucketDestination.Format = InventoryFormat.CSV;
// 目的地bucket的用户accountId。
config.Destination.OSSBucketDestination.AccountId = accountId;
// 目的地bucket的roleArn。
config.Destination.OSSBucketDestination.RoleArn = roleArn;
// 目的地bucket的名称。
config.Destination.OSSBucketDestination.Bucket = destBucketName;
// 设置产生清单结果的存储路径前缀。
config.Destination.OSSBucketDestination.Prefix = "prefix1";
// 设置清单的生成计划,以下示例为每周一次。其中,Weekly对应每周一次,Daily对应每天一次。
config.Schedule = new InventorySchedule(InventoryFrequency.Daily);
// 设置清单中包含的object的版本为当前版本。如果设置为InventoryIncludedObjectVersions.All则表示object的所有版本,在版本控制状态下生效。
config.IncludedObjectVersions = InventoryIncludedObjectVersions.All;
// 设置清单中包含的object属性。
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);
}
有关添加Bucket清单配置的详情,请参见PutBucketInventory。
查看清单配置
以下代码用于查看某个Bucket的清单配置:
using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var id = "<BucketInventoryConfigurationId>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// 查看指定id的清单配置信息。
var request = new GetBucketInventoryConfigurationRequest(bucketName, id);
// 获取Bucket清单。
var result = client.GetBucketInventoryConfiguration(request);
Console.WriteLine("Get bucket:{0} BucketInventoryConfiguration succeeded ", bucketName);
// 打印Bucket清单信息。
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);
}
有关查看Bucket清单配置的详情,请参见GetBucketInventory。
批量列举清单配置
说明 单次请求最多可获取100条清单配置项内容。若需获取超过100条清单配置项,则需发送多次请求,并保留相应的Token,作为下一次请求的参数。
以下代码用于批量列举某个Bucket的清单配置:
using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var id = "<BucketInventoryConfigurationId>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// 列举清单配置项。默认每次最多列举100条结果,如果配置超过100条,结果将会分页返回,通过传入Token方式列举下一页。
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);
//打印bucket清单信息
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);
}
有关批量列举Bucket清单配置的详情,请参见ListBucketInventory。
删除清单配置
以下代码用于删除某个Bucket的清单配置:
using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var id = "<BucketInventoryConfigurationId>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// 删除指定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);
}
有关删除Bucket清单配置的详情,请参见DeleteBucketInventory。