本文介紹如何添加、查看、批量列舉和刪除儲存空間(Bucket)的清單(Inventory)配置。
注意事項
本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見地區和Endpoint。
本文以OSS網域名稱建立OSSClient為例。如果您希望通過自訂網域名、STS等方式建立OSSClient,請參見初始化(C# SDK V1)。
請確保您擁有調用添加、查看、列舉和刪除儲存空間清單配置的許可權。Bucket所有者預設擁有此類許可權,如果您無此類許可權,請先向Bucket所有者申請對應操作的許可權。
單個Bucket最多隻能有1000條清單配置。
配置清單的源Bucket與存放匯出的資訊清單檔所在的目標Bucket必須位於同一個Region。
添加清單配置
以下代碼用於為某個Bucket添加清單配置。
using Aliyun.OSS;
using Aliyun.OSS.Common;
// yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
var endpoint = "yourEndpoint";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// 填寫Bucket名稱。
var bucketName = "examplebucket";
// 填寫Bucket所有者授予的賬戶ID。
var accountId ="yourDestinationBucketAccountId";
// 填寫具有讀取源Bucket所有檔案和向目標Bucket寫入檔案許可權的角色名稱。
var roleArn ="yourDestinationBucketRoleArn";
// 填寫存放清單結果的Bucket名稱。
var destBucketName ="yourDestinationBucketName";
// 填寫Bucket所在地區對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。
const string region = "cn-hangzhou";
// 建立ClientConfiguration執行個體,按照您的需要修改預設參數。
var conf = new ClientConfiguration();
// 設定v4簽名。
conf.SignatureVersion = SignatureVersion.V4;
// 建立OssClient執行個體。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// 添加Bucket清單。
var config = new InventoryConfiguration();
// 設定清單規則名稱。
config.Id = "report1";
// 清單配置是否啟用的標識,取值為true或false。設定為true,表示啟用清單配置。
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的清單配置:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
var endpoint = "yourEndpoint";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// 填寫Bucket名稱。
var bucketName = "examplebucket";
// 填寫清單規則名稱。
var id = "BucketInventoryConfigurationId";
// 填寫Bucket所在地區對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。
const string region = "cn-hangzhou";
// 建立ClientConfiguration執行個體,按照您的需要修改預設參數。
var conf = new ClientConfiguration();
// 設定v4簽名。
conf.SignatureVersion = SignatureVersion.V4;
// 建立OssClient執行個體。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
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);
}批量列舉清單配置
說明
單次請求最多可擷取100條清單配置項內容。若需擷取超過100條清單配置項,則需發送多次請求,並保留相應的Token,作為下一次請求的參數。
以下代碼用於批量列舉某個Bucket的清單配置:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
var endpoint = "yourEndpoint";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// 填寫Bucket名稱。
var bucketName = "examplebucket";
// 填寫清單規則名稱。
var id = "BucketInventoryConfigurationId";
// 填寫Bucket所在地區對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。
const string region = "cn-hangzhou";
// 建立ClientConfiguration執行個體,按照您的需要修改預設參數。
var conf = new ClientConfiguration();
// 設定v4簽名。
conf.SignatureVersion = SignatureVersion.V4;
// 建立OssClient執行個體。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
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的清單配置:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
var endpoint = "yourEndpoint";
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// 填寫Bucket名稱。
var bucketName = "examplebucket";
// 填寫清單規則名稱。
var id = "BucketInventoryConfigurationId";
// 填寫Bucket所在地區對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。
const string region = "cn-hangzhou";
// 建立ClientConfiguration執行個體,按照您的需要修改預設參數。
var conf = new ClientConfiguration();
// 設定v4簽名。
conf.SignatureVersion = SignatureVersion.V4;
// 建立OssClient執行個體。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
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);
}相關文檔
關於添加儲存空間清單配置的API介面說明,請參見PutBucketInventory。
關於查看儲存空間清單配置的API介面說明,請參見GetBucketInventory。
關於批量列舉儲存空間清單配置的API介面說明,請參見ListBucketInventory。
關於刪除儲存空間清單配置的API介面說明,請參見DeleteBucketInventory。