设置桶生命周期

PutLifecycleConfiguration操作可以设置bucket的生命周期规则,规则可以通过匹配对象key前缀、标签的方法设置当前版本或者历史版本对象的过期时间,对象过期后会被自动删除。bucket的版本控制状态必须处于Enabled或者Suspended,历史版本对象过期时间配置才能生效。每次执行PutBucketLifecycleConfiguration操作会覆盖bucket中已存在的生命周期规则。

代码示例:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;

namespace DotNetSDK.BucketOperation
{
    public class PutBucketLifeCycleConfigurationExample
    {
        public static async Task PutBucketLifecycleConfiguration()
        {
            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);

                List<LifecycleRule> rules = new List<LifecycleRule>();
                // rule1:设置符合指定前缀的对象一天后过期
                var rule1 = new LifecycleRule()
                {
                    Id = "expireAfterOneDay",
                    Filter = new LifecycleFilter()
                    {
                        LifecycleFilterPredicate = new LifecyclePrefixPredicate()
                        {
                            Prefix = "expireAfterOneDay/"
                        }
                    },
                    Status = LifecycleRuleStatus.Enabled,
                    Expiration = new LifecycleRuleExpiration()
                    {
                        Days = 1
                    }
                };
                rules.Add(rule1);
                // rule2: 设置符合指定前缀的对象的历史版本一天后过期
                var rule2 = new LifecycleRule()
                {
                    Id = "noncurrentVersionExpireAfterOneDay",
                    Status = LifecycleRuleStatus.Enabled,
                    Filter = new LifecycleFilter()
                    {
                        LifecycleFilterPredicate = new LifecyclePrefixPredicate()
                        {
                            Prefix = "noncurrentVersionExpireAfterOneDay/"
                        }
                    },
                    NoncurrentVersionExpiration = new LifecycleRuleNoncurrentVersionExpiration()
                    {
                        NoncurrentDays = 1
                    }
                };
                rules.Add(rule2);
                // rule3: 设置匹配指定标签信息的对象一天后过期
                var rule3 = new LifecycleRule()
                {
                    Id = "withTagsExpireAfterOneDay",
                    Status = LifecycleRuleStatus.Enabled,
                    Expiration = new LifecycleRuleExpiration()
                    {
                        Days = 1
                    },
                    Filter = new LifecycleFilter()
                    {
                        LifecycleFilterPredicate = new LifecycleTagPredicate()
                        {
                            Tag = new Tag()
                            {
                                Key = "key1",
                                Value = "value1"
                            }
                        }
                    },
                };
                rules.Add(rule3);
                LifecycleConfiguration configuration = new LifecycleConfiguration()
                {
                    Rules = rules
                };
                var putLifecycleConfigurationRequest = new PutLifecycleConfigurationRequest()
                {
                    BucketName = bucketName,
                    Configuration = configuration
                };

                var result = await s3Client.PutLifecycleConfigurationAsync(putLifecycleConfigurationRequest);
                if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
                {
                    Console.WriteLine("fail to put lifecycle configuration to bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

PutLifecycleConfigurationRequest可设置的参数如下:

参数 类型 说明 是否必要
BucketName string bucket的名称。
Configuration LifecycleConfiguration 封装了生命周期规则的数组,最多可以包含1000条规则。

响应结果:

HTTP状态码 响应码 描述
200 OK 操作成功。
400 BadRequest 生命周期规则不合法。
403 Forbidden 用户没有权限执行操作。
404 NotFound 指定的bucket不存在。