Some data uploaded to Object Storage Service (OSS) may not be frequently accessed but still needs to be stored in cold storage due to compliance or archiving requirements. Also, you may want to delete data that is no longer required in batches to reduce storage costs. In such cases, you can configure lifecycle rules based on the last modified time to periodically convert hot data to cold data or delete unwanted objects.
Usage notes
Before you configure lifecycle rules based on the last modified time of objects, make sure that you familiarize yourself with this feature. For more information, see Lifecycle rules based on the last modified time.
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To set lifecycle rules, you must have the
oss:PutBucketLifecyclepermission. To view lifecycle rules, you must have theoss:GetBucketLifecyclepermission. To delete all lifecycle rules, you must have theoss:DeleteBucketLifecyclepermission. For more information, see Grant custom access policies to a RAM user.
Configure lifecycle rules for a bucket
The following sample code provides an example on how to configure lifecycle rules based on the last modified time for a bucket named examplebucket. To modify lifecycle rules, follow the instructions as described in How do I change the configurations of one or more lifecycle rules?
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) Endpoint is used as an example. Replace it with the actual Endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the bucket name.
bucket = client.get_bucket('examplebucket')
# Set the lifecycle rules.
bucket.lifecycle = [
Aliyun::OSS::LifeCycleRule.new(
:id => 'rule1', :enable => true, :prefix => 'foo/', :expiry => 3),
Aliyun::OSS::LifeCycleRule.new(
:id => 'rule2', :enable => false, :prefix => 'bar/', :expiry => Date.new(2016, 1, 1))
]Query the lifecycle rules of a bucket
The following sample code provides an example on how to query the lifecycle rules configured for examplebucket:
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) Endpoint is used as an example. Replace it with the actual Endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the bucket name.
bucket = client.get_bucket('examplebucket')
# View the lifecycle rules.
rules = bucket.lifecycle
puts rulesDelete all lifecycle rules of a bucket
The following sample code provides an example on how to delete lifecycle rules configured for examplebucket. To delete one or more lifecycle rules, follow the instructions as described in How do I delete one or more lifecycle rules?
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# The China (Hangzhou) Endpoint is used as an example. Replace it with the actual Endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the bucket name.
bucket = client.get_bucket('examplebucket')
# Delete all lifecycle rules.
bucket.lifecycle = []References
For more information about the API operation that you can call to configure lifecycle rules, see PutBucketLifecycle.
For more information about the API operation that you can call to query lifecycle rules, see GetBucketLifecycle.
For more information about the API operation that you can call to delete lifecycle rules, see DeleteBucketLifecycle.