查看桶生命周期
生命周期管理可以通过设置规则实现自动清理过期的对象,优化存储空间。
GetBucketLifecycleConfiguration操作可以查看bucket当前的生命周期规则。
代码示例:
using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace DotNetSDK.BucketOperation
{
public class GetBucketLifecycleConfigurationExample
{
public static async Task GetBucketLifecycleConfiguration()
{
var accessKey = "YOUR_ACCESS_KEY";
var secretKey = "YOUR_SECRET_KEY";
var endpoint = "YOUR_ENDPOINT";
var bucketName = "EXAMPLE_BUCKET";
try
{
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var conf = new AmazonS3Config
{
ServiceURL = endpoint
};
var s3Client = new AmazonS3Client(credentials, conf);
var getLifecycleConfigurationRequest = new GetLifecycleConfigurationRequest()
{
BucketName = bucketName
};
var result = await s3Client.GetLifecycleConfigurationAsync(getLifecycleConfigurationRequest);
if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine("fail to get lifecycle configuration of bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
return;
}
Console.WriteLine("the lifecycle configuration of bucket {0} are:", bucketName);
foreach (var lifecycleRule in result.Configuration.Rules)
{
Console.WriteLine("Lifecycle rule id: {0}", lifecycleRule.Id);
Console.WriteLine("Lifecycle rule prefix: {0}", lifecycleRule.Prefix);
Console.WriteLine("Lifecycle rule status: {0}", lifecycleRule.Status);
if (null != lifecycleRule.Expiration)
{
Console.WriteLine("expiration days: {0}", lifecycleRule.Expiration.Days);
}
if (null != lifecycleRule.NoncurrentVersionExpiration)
{
Console.WriteLine("NoncurrentVersionExpiration NoncurrentDays: {0}", lifecycleRule.NoncurrentVersionExpiration.NoncurrentDays);
}
if (null != lifecycleRule.Transitions)
{
foreach (var transition in lifecycleRule.Transitions)
{
Console.WriteLine("Transition Days: {0}", transition.Days.ToString());
}
}
if (null != lifecycleRule.NoncurrentVersionTransitions)
{
foreach (var nontransition in lifecycleRule.NoncurrentVersionTransitions)
{
Console.WriteLine("NoncurrentVersionTransition NoncurrentDays: {0}", nontransition.NoncurrentDays.ToString());
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
GetLifecycleConfigurationRequest可设置的参数如下:
参数 | 类型 | 说明 | 是否必要 |
---|---|---|---|
BucketName | string | bucket的名称。 | 是 |
GetBucketLifecycleConfiguration返回的结果如下:
属性名 | 类型 | 说明 |
---|---|---|
Rules | List<LifecycleRule> | 一个描述生命周期管理的规则数组,一条规则包含了规则ID、匹配的对象key前缀、匹配的对象标签信息、当前版本对象过期时间、历史版本对象过期时间和是否生效标识等信息。 |
响应结果:
HTTP状态码 | 响应码 | 描述 |
---|---|---|
200 | OK | 操作成功。 |
403 | Forbidden | 用户没有权限执行操作。 |
404 | NotFound | 指定的bucekt不存在或者bucekt中没有配置生命周期管理信息。 |