TOS 支持设置桶的生命周期规则(Lifecycle),自动删除过期的对象(Object)和未合并的分片数据,或将到期的对象转化为低频或归档存储类型,从而节约用户存储费用。本文介绍如何管理桶的生命周期规则。
注意
tos:PutLifecycleConfiguration
权限,具体操作请参见权限配置指南。以下代码用于设置桶 bucket-test
的生命周期规则。
import datetime import os import tos from tos import StatusType, StorageClassType from tos.models2 import BucketLifeCycleRule, BucketLifeCycleExpiration, BucketLifeCycleAbortInCompleteMultipartUpload, \ BucketLifeCycleTransition, Tag, BucketLifeCycleNonCurrentVersionTransition, \ BucketLifeCycleNoCurrentVersionExpiration # 从环境变量获取 AK 和 SK 信息。 ak = os.getenv('TOS_ACCESS_KEY') sk = os.getenv('TOS_SECRET_KEY') # your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 tos-cn-beijing.volces.com,your region 填写 cn-beijing。 endpoint = "your endpoint" region = "your region" bucket_name = "bucket-test" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) # 设置创建 70 天后过期 rule1 = BucketLifeCycleRule(id='rule1', prefix='test1/', status=StatusType.Status_Enable, expiration=BucketLifeCycleExpiration(days=70)) # 设置日期对象过期时间 rule2 = BucketLifeCycleRule(id='rule2', prefix='test2/', status=StatusType.Status_Enable, expiration=BucketLifeCycleExpiration(date=datetime.datetime(2022, 11, 30))) # 设置分片过期规则,过期时间创建后的10天 rule3 = BucketLifeCycleRule(id='rule3', prefix='test3/', status=StatusType.Status_Enable, abort_in_complete_multipart_upload=BucketLifeCycleAbortInCompleteMultipartUpload( days_after_init=10)) # 设置对象的转化规则, 指定对象在 30天后转化为低频 rule4 = BucketLifeCycleRule(id='rule4', prefix='test4/', status=StatusType.Status_Enable, transitions=[BucketLifeCycleTransition(days=30, storage_class=StorageClassType.Storage_Class_Ia)]) # 设置匹配标签,与rule4不同在于除了匹配前缀外需对象被设置了对应标签才能匹配此规则 rule5 = BucketLifeCycleRule(id='rule5', prefix='test5/', status=StatusType.Status_Enable, transitions=[BucketLifeCycleTransition(days=30, storage_class=StorageClassType.Storage_Class_Ia)], tags=[Tag('key1', 'value1')]) # rule6 针对开启多版本对象的桶 # 设置最新版本在70天后过期 # 历史版本在30天后转化为低频存储 # 历史版本在40天后过期 rule6 = BucketLifeCycleRule(id='rule6', prefix='test6/', status=StatusType.Status_Enable, expiration=BucketLifeCycleExpiration(days=70), non_current_version_transitions=[ BucketLifeCycleNonCurrentVersionTransition(non_current_days=30, storage_class=StorageClassType.Storage_Class_Ia)], no_current_version_expiration=BucketLifeCycleNoCurrentVersionExpiration( no_current_days=40)) lifecycle = [rule1, rule2, rule3, rule4, rule5, rule6] out = client.put_bucket_lifecycle(bucket_name, lifecycle) except tos.exceptions.TosClientError as e: # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常 print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause)) except tos.exceptions.TosServerError as e: # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息 print('fail with server error, code: {}'.format(e.code)) # request id 可定位具体问题,强烈建议日志中保存 print('error with request id: {}'.format(e.request_id)) print('error with message: {}'.format(e.message)) print('error with http code: {}'.format(e.status_code)) print('error with ec: {}'.format(e.ec)) print('error with request url: {}'.format(e.request_url)) except Exception as e: print('fail with unknown error: {}'.format(e))
注意
要获取桶设置生命周期规则,您必须具备 tos:GetLifecycleConfiguration
权限,具体操作请参见权限配置指南。
以下代码用于获取桶 bucket-test
的生命周期规则。
import os import tos # 从环境变量获取 AK 和 SK 信息。 ak = os.getenv('TOS_ACCESS_KEY') sk = os.getenv('TOS_SECRET_KEY') # your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 tos-cn-beijing.volces.com,your region 填写 cn-beijing。 endpoint = "your endpoint" region = "your region" bucket_name = "bucket-test" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) rules = client.get_bucket_lifecycle(bucket_name) for rule in rules.rules: print('id:', rule.id) print('prefix', rule.prefix) print('status', rule.status.value) if rule.tags is not None: for tag in rule.tags: print('tag key:{}, value:{}'.format(tag.key, tag.value)) if rule.abort_in_complete_multipart_upload is not None: if rule.abort_in_complete_multipart_upload.days_after_init is not None: print('abort_in_complete_multipart_upload:', rule.abort_in_complete_multipart_upload.days_after_init) if rule.expiration is not None: if rule.expiration.date is not None: print('expiration_date', rule.expiration.date) else: print('expiration_days', rule.expiration.days) if rule.transitions is not None: if len(rule.transitions) > 0: for transition in rule.transitions: if transition.days is not None: print('transition_days', transition.days) else: print('transition_date', transition.date) print('transition_storage_class', transition.storage_class.value) if rule.no_current_version_expiration is not None: if rule.no_current_version_expiration.no_current_days is not None: print('no_current_version_expiration.no_current_days', rule.no_current_version_expiration.no_current_days) if rule.non_current_version_transitions is not None: if len(rule.non_current_version_transitions) > 0: for non_current_version_transition in rule.non_current_version_transitions: print('non_current_version_transition_days', non_current_version_transition.non_current_days) print('non_current_version_transition_storage_class', non_current_version_transition.storage_class.value) except tos.exceptions.TosClientError as e: # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常 print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause)) except tos.exceptions.TosServerError as e: # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息 print('fail with server error, code: {}'.format(e.code)) # request id 可定位具体问题,强烈建议日志中保存 print('error with request id: {}'.format(e.request_id)) print('error with message: {}'.format(e.message)) print('error with http code: {}'.format(e.status_code)) print('error with ec: {}'.format(e.ec)) print('error with request url: {}'.format(e.request_url)) except Exception as e: print('fail with unknown error: {}'.format(e))
注意
删除桶生命周期规则,您必须具备 tos:PutLifecycleConfiguration
权限,具体操作请参见权限配置指南。
以下代码用于删除桶 bucket-test
的生命周期规则。
import os import tos # 从环境变量获取 AK 和 SK 信息。 ak = os.getenv('TOS_ACCESS_KEY') sk = os.getenv('TOS_SECRET_KEY') # your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 tos-cn-beijing.volces.com,your region 填写 cn-beijing。 endpoint = "your endpoint" region = "your region" bucket_name = "bucket-test" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) # 删除桶的生命周期规则 client.delete_bucket_lifecycle(bucket_name) except tos.exceptions.TosClientError as e: # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常 print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause)) except tos.exceptions.TosServerError as e: # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息 print('fail with server error, code: {}'.format(e.code)) # request id 可定位具体问题,强烈建议日志中保存 print('error with request id: {}'.format(e.request_id)) print('error with message: {}'.format(e.message)) print('error with http code: {}'.format(e.status_code)) print('error with ec: {}'.format(e.ec)) print('error with request url: {}'.format(e.request_url)) except Exception as e: print('fail with unknown error: {}'.format(e))
关于生命周期规则的更多信息,请参见设置生命周期规则。