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. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 configure lifecycle rules, you must have the
oss:PutBucketLifecycle
permission. To query lifecycle rules, you must have theoss:GetBucketLifecycle
permission. To delete lifecycle rules, you must have theoss:DeleteBucketLifecycle
permission. For more information, see Attach a custom policy 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(
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your 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 configured.
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')
# Configure 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(
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your 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 configured.
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')
# Query the lifecycle rules.
rules = bucket.lifecycle
puts rules
Delete 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(
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your 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 configured.
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 the lifecycle rules that are configured for the bucket.
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.