生命週期規則可針對首碼或對象標籤生效,您也可以同時指定兩者作為生命週期規則生效的條件。
說明
標籤條件中標籤的Key和Value必須同時匹配。同一個規則中,如果同時配置了首碼和多個對象標籤,則只有同時滿足首碼且匹配規則中所有對象標籤的對象(Object),才視為適用於該規則。
生命週期規則中添加標籤匹配規則
以下代碼用於生命週期規則中添加標籤匹配規則:
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
{
var setBucketLifecycleRequest = new SetBucketLifecycleRequest(bucketName);
LifecycleRule lcr1 = new LifecycleRule()
{
ID = "delete obsoleted files",
Prefix = "obsoleted/",
Status = RuleStatus.Enabled,
ExpriationDays = 3,
Tags = new Tag[1]
};
var tag1 = new Tag
{
Key = "project",
Value = "projectone"
};
lcr1.Tags[0] = tag1;
LifecycleRule lcr2 = new LifecycleRule()
{
ID = "delete temporary files",
Prefix = "temporary/",
Status = RuleStatus.Enabled,
ExpriationDays = 20,
Tags = new Tag[1]
};
var tag2 = new Tag
{
Key = "user",
Value = "jsmith"
};
lcr2.Tags[0] = tag2;
setBucketLifecycleRequest.AddLifecycleRule(lcr1);
setBucketLifecycleRequest.AddLifecycleRule(lcr2);
client.SetBucketLifecycle(setBucketLifecycleRequest);
Console.WriteLine("Set bucket:{0} Lifecycle 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);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
查看生命週期規則中匹配的標籤資訊
以下代碼用於查看生命週期規則中匹配的標籤資訊:
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
{
var rules = client.GetBucketLifecycle(bucketName);
Console.WriteLine("Get bucket:{0} Lifecycle succeeded ", bucketName);
foreach (var rule in rules)
{
Console.WriteLine("ID: {0}", rule.ID);
Console.WriteLine("Prefix: {0}", rule.Prefix);
Console.WriteLine("Status: {0}", rule.Status);
foreach (var tag in rule.Tags)
{
Console.WriteLine("key:{0}, value:{1}", tag.Key, tag.Value);
}
if (rule.ExpriationDays.HasValue)
Console.WriteLine("ExpirationDays: {0}", rule.ExpriationDays);
}
}
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);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}