设置桶生命周期规则

生命周期管理可以通过设置规则实现自动清理过期的对象,优化存储空间。本文介绍如何设置桶(Bucket)生命周期规则。

  • PutBucketLifecycleConfiguration

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

代码示例:

// 设置桶生命周期规则
func PutBucketLifecycleConfiguration(svc *s3.S3) {
    // rule1:设置匹配指定前缀的对象一天后过期
    rule1 := &s3.LifecycleRule{
        ID:     aws.String("expireAfterOneDay"),
        Status: aws.String("Enabled"),
        Filter: &s3.LifecycleRuleFilter{
            Prefix: aws.String("expireAfterOneDay/"),
        },
        Expiration: &s3.LifecycleExpiration{
            Days: aws.Int64(1),
        },
    }
    // rule2: 设置匹配指定前缀的对象的历史版本一天后过期
    rule2 := &s3.LifecycleRule{
        ID:     aws.String("noncurrentVersionExpireAfterOneDay"),
        Status: aws.String("Enabled"),
        Filter: &s3.LifecycleRuleFilter{
            Prefix: aws.String("noncurrentVersionExpireAfterOneDay/"),
        },
        NoncurrentVersionExpiration: &s3.NoncurrentVersionExpiration{
            NoncurrentDays: aws.Int64(1),
        },
    }
    // rule3: 设置匹配指定标签信息的对象一天后过期
    rule3 := &s3.LifecycleRule{
        ID:     aws.String("withTagsExpireAfterOneDay"),
        Status: aws.String("Enabled"),
        Expiration: &s3.LifecycleExpiration{
            Days: aws.Int64(1),
        },
        Filter: &s3.LifecycleRuleFilter{
            Tag: &s3.Tag{
                Key:   aws.String("Key1"),
                Value: aws.String("Value1"),
            },
        },
    }
    // rule4: 设置未合并的分片一天后过期
    rule4 := &s3.LifecycleRule{
        ID:     aws.String("abortIncompleteMultipartUpload"),
        Status: aws.String("Enabled"),
        Filter: &s3.LifecycleRuleFilter{
            Prefix: aws.String("abortIncompleteMultipartUpload/"),
        },
        AbortIncompleteMultipartUpload: &s3.AbortIncompleteMultipartUpload{
            DaysAfterInitiation: aws.Int64(1),
        },
    }
    putBucketLifecycleConfigurationInput := &s3.PutBucketLifecycleConfigurationInput{
        Bucket: aws.String("exampleBucket"),
        LifecycleConfiguration: &s3.BucketLifecycleConfiguration{
            Rules: []*s3.LifecycleRule{rule1, rule2, rule3, rule4},
        },
    }
    _, err := svc.PutBucketLifecycleConfiguration(putBucketLifecycleConfigurationInput)
    if err != nil {
        fmt.Printf("fail to put bucket lifecycle configuration. %v\n", err)
    }
}

PutBucketLifecycleConfigurationInput可设置的参数如下:

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

PutBucketLifecycleConfigurationRequest操作首先生成一个"request.Request"对象,该对象是一个执行PutBucketLifecycleConfiguration操作的请求。通过调用Request对象的Send方法完成获设置桶生命周期规则的操作。该方法可以生成定制化的请求,例如自定义请求头部请求超时重试设置。

代码示例:

func PutBucketLifecycleConfigurationRequest(svc *s3.S3) {
    // rule1:设置匹配指定前缀的对象一天后过期
    rule1 := &s3.LifecycleRule{
        ID:     aws.String("expireAfterOneDay"),
        Status: aws.String("Enabled"),
        Filter: &s3.LifecycleRuleFilter{
            Prefix: aws.String("expireAfterOneDay/"),
        },
        Expiration: &s3.LifecycleExpiration{
            Days: aws.Int64(1),
        },
    }
    // rule2: 设置匹配指定前缀的对象的历史版本一天后过期
    rule2 := &s3.LifecycleRule{
        ID:     aws.String("noncurrentVersionExpireAfterOneDay"),
        Status: aws.String("Enabled"),
        Filter: &s3.LifecycleRuleFilter{
            Prefix: aws.String("noncurrentVersionExpireAfterOneDay/"),
        },
        NoncurrentVersionExpiration: &s3.NoncurrentVersionExpiration{
            NoncurrentDays: aws.Int64(1),
        },
    }
    // rule3: 设置匹配指定标签信息的对象一天后过期
    rule3 := &s3.LifecycleRule{
        ID:     aws.String("withTagsExpireAfterOneDay"),
        Status: aws.String("Enabled"),
        Expiration: &s3.LifecycleExpiration{
            Days: aws.Int64(1),
        },
        Filter: &s3.LifecycleRuleFilter{
            Tag: &s3.Tag{
                Key:   aws.String("Key1"),
                Value: aws.String("Value1"),
            },
        },
    }
    // rule4: 设置未合并的分片一天后过期
    rule4 := &s3.LifecycleRule{
        ID:     aws.String("abortIncompleteMultipartUpload"),
        Status: aws.String("Enabled"),
        Filter: &s3.LifecycleRuleFilter{
            Prefix: aws.String("abortIncompleteMultipartUpload/"),
        },
        AbortIncompleteMultipartUpload: &s3.AbortIncompleteMultipartUpload{
            DaysAfterInitiation: aws.Int64(1),
        },
    }
    putBucketLifecycleConfigurationInput := &s3.PutBucketLifecycleConfigurationInput{
        Bucket: aws.String("exampleBucket"),
        LifecycleConfiguration: &s3.BucketLifecycleConfiguration{
            Rules: []*s3.LifecycleRule{rule1, rule2, rule3, rule4},
        },
    }

    req, resp := svc.PutBucketLifecycleConfigurationRequest(putBucketLifecycleConfigurationInput)

    err := req.Send()
    if err != nil {
        fmt.Printf("fail to put bucket policy. %v\n", err)
    } else {
        fmt.Println(resp)
    }
}

响应结果:

HTTP状态 响应码 描述
200 Success 操作成功。
400 MalformedXML 生命周期规则不合法。
403 AccessDenied 用户没有权限执行操作。
404 NoSuchKey 指定的桶不存在。