Setting Lifecycle Rules
If you have any questions during development, post them on the Issues page of GitHub.
You can call set_bucket_lifecycle_configuration to set lifecycle rules for a bucket.
Parameter Description
Field |
Type |
Mandatory or Optional |
Description |
---|---|---|---|
option |
The context of the bucket. For details, see Configuring option. |
Mandatory |
Bucket parameter |
bucket_lifecycle_conf |
obs_lifecycle_conf * |
Mandatory |
For details about the bucket lifecycle configuration, see the following table. |
blcc_number |
unsigned int |
Mandatory |
Number of array members in the array bucket_lifecycle_conf. |
handler |
obs_response_handler* |
Mandatory |
Callback function |
callback_data |
void * |
Optional |
Callback data |
The following table describes the bucket lifecycle configuration structure obs_lifecycle_conf.
Field |
Type |
Mandatory or Optional |
Description |
---|---|---|---|
date |
const char * |
If there is no days and no transition, noncurrent_version_days, or noncurrent_version_transition, this parameter is mandatory. |
Indicates the time when the object expiration rule of the latest version takes effect. The value must conform to the ISO8601 standards and must be at 00:00 (UTC time). |
days |
const char * |
If there is no date and no transition, noncurrent_version_days, noncurrent_version_transition is available, this parameter is mandatory. |
Number of days when the expiration rule takes effect after the object creation (only applicable to latest versions of objects). |
id |
const char * |
Optional |
Unique identifier of a rule. The value can contain a maximum of 255 characters. |
prefix |
const char * |
Mandatory |
Object name prefix identifying one or more objects to which the rule applies. |
status |
const char * |
Mandatory |
Indicates whether the rule is enabled. |
noncurrent_version_days |
const char * |
Optional |
Number of days when the specified expiration rule takes effect after the object becomes a noncurrent version (only applicable to an object's noncurrent version). Container for the expiration time of objects' noncurrent versions. If versioning is enabled or suspended for a bucket, you can set this parameter to delete noncurrent versions of objects that match the lifecycle rule (only applicable to the noncurrent versions of objects). |
transition_num |
unsigned int |
If transition is not empty, this parameter is mandatory. |
Number of array members in the array transition. |
transition |
obs_lifecycle_transtion * |
If there is no date, days, noncurrent_version_transition, or noncurrent_version_days, this parameter is mandatory. |
Indicates the transition time and the object storage class after transition (valid only for the latest object version). |
transition->date |
const char * |
This parameter is mandatory if there is transition but no transition.days. |
Indicates the time when the object transition rule of the latest version takes effect. The value must conform to the ISO8601 standards and must be at 00:00 (UTC time). |
transition->days |
const char * |
This parameter is mandatory if there is transition but no transition.date. |
Number of days when the transition rule takes effect after the object creation (only applicable to latest versions of objects). |
transition->storage_class |
obs_storage_class |
If there is transition, this parameter is mandatory. |
The storage class to which the latest version object is modified. |
noncurrent_version_transition_num |
unsigned int |
If obs_lifecycle_noncurrent_transtion is not empty, this parameter is mandatory. |
Number of array members in the array noncurrent_version_transition. |
noncurrent_version_transition |
obs_lifecycle_noncurrent_transtion * |
If there is no date, days, transition, or noncurrent_version_days, this parameter is mandatory. |
Transition time of noncurrent object versions and the object storage class after transition. |
noncurrent_version_transition->noncurrent_version_days |
const char * |
If there is noncurrent_version_transition, this parameter is mandatory. |
Number of days when the specified transition rule takes effect after the object becomes a noncurrent version (only applicable to an object's noncurrent version). |
noncurrent_version_transition->storage_class |
obs_storage_class |
If there is noncurrent_version_transition, this parameter is mandatory. |
The storage class to which the noncurrent version object is modified. |
Setting an Object Transition Policy
Sample code:
static void test_set_bucket_lifecycle_configuration1() { obs_status ret_status = OBS_STATUS_BUTT; // Create and initialize option. obs_options option; init_obs_options(&option); option.bucket_options.host_name = "<your-endpoint>"; option.bucket_options.bucket_name = "<Your bucketname>"; // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. option.bucket_options.access_key = getenv("ACCESS_KEY_ID"); option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY"); // Set the completed callback function. obs_response_handler response_handler = { NULL, &response_complete_callback }; obs_lifecycle_conf bucket_lifecycle_conf; memset(&bucket_lifecycle_conf, 0, sizeof(obs_lifecycle_conf)); // ID of the lifecycle rule bucket_lifecycle_conf.id = "test3"; // Designate prefix "test". bucket_lifecycle_conf.prefix = "bcd"; // The lifecycle rule takes effect. bucket_lifecycle_conf.status = "Enabled"; // Specify that objects whose names contain the prefix will expire 10 days after creation. bucket_lifecycle_conf.days = "10"; obs_lifecycle_transtion transition; memset(&transition, 0, sizeof(obs_lifecycle_transtion)); // Specify that objects whose names contain the specified prefix will be transitioned 30 days after creation. transition.days = "30"; // Specify the storage class that the object will be transitioned to. transition.storage_class = OBS_STORAGE_CLASS_STANDARD_IA; bucket_lifecycle_conf.transition = &transition; bucket_lifecycle_conf.transition_num = 1; obs_lifecycle_noncurrent_transtion noncurrent_transition; memset(&noncurrent_transition, 0, sizeof(obs_lifecycle_noncurrent_transtion)); // Specify that objects whose names contain the specified prefix will be transitioned 30 days after becoming noncurrent versions. noncurrent_transition.noncurrent_version_days = "30"; // Specify the storage class of objects whose names contain the prefix after changing into noncurrent versions. noncurrent_transition.storage_class = OBS_STORAGE_CLASS_STANDARD_IA; bucket_lifecycle_conf. noncurrent_version_transition = &noncurrent_transition; bucket_lifecycle_conf.noncurrent_version_transition_num = 1; set_bucket_lifecycle_configuration(&option, &bucket_lifecycle_conf, 1, &response_handler, &ret_status); if (OBS_STATUS_OK == ret_status) { printf("set bucket lifecycle configuration success.\n"); } else { printf("set bucket lifecycle configuration failed(%s).\n", obs_get_status_name(ret_status)); } }
Setting an Object Expiration Time
Sample code:
static void test_set_bucket_lifecycle_configuration2() { obs_options option; obs_status ret_status = OBS_STATUS_BUTT; // Set option. init_obs_options(&option); option.bucket_options.host_name = HOST_NAME; option.bucket_options.bucket_name = bucket_name; //Read the AK/SK from environment variables. option.bucket_options.access_key = getenv("ACCESS_KEY_ID"); option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY"); // Set the completed callback function. obs_response_handler response_handler = { NULL, &response_complete_callback }; obs_lifecycle_conf bucket_lifecycle_conf; memset(&bucket_lifecycle_conf, 0, sizeof(obs_lifecycle_conf)); // ID of the lifecycle rule bucket_lifecycle_conf.id = "test1"; // Designate prefix "test". bucket_lifecycle_conf.prefix = "test"; // Specify that objects whose names contain the prefix will expire 10 days after creation. bucket_lifecycle_conf.days = "10"; // Specify that objects whose names contain the specified prefix will expire 20 days after becoming noncurrent versions. bucket_lifecycle_conf.noncurrent_version_days = "20"; // The lifecycle rule takes effect. bucket_lifecycle_conf.status = "Enabled"; set_bucket_lifecycle_configuration(&option, &bucket_lifecycle_conf, 1, &response_handler, &ret_status); if (OBS_STATUS_OK == ret_status) { printf("set bucket lifecycle configuration success.\n"); } else { printf("set bucket lifecycle configuration failed(%s).\n", obs_get_status_name(ret_status)); } }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot