tencent cloud

Lifecycle
Last updated: 2025-10-14 10:52:01
Lifecycle
Last updated: 2025-10-14 10:52:01

Feature Overview

This document provides an overview of APIs and SDK code samples related to lifecycles.
API
Operation
Description
Setting a Lifecycle Configuration
Sets lifecycle management configuration for a bucket
Querying lifecycle
Queries the lifecycle management configuration of a bucket
Deleting lifecycle
Deletes the lifecycle management configuration of a bucket

Setting a Lifecycle Configuration

Note

Configuring the lifecycle settings for a bucket.

Method prototype

cos_status_t *cos_put_bucket_lifecycle(const cos_request_options_t *options,
const cos_string_t *bucket,
cos_list_t *lifecycle_rule_list,
cos_table_t **resp_headers);

Description

Parameter name
ParameterDescription
Local Disk Types
options
COS request options
Struct
bucket
Bucket name in the format of BucketName-APPID
String
lifecycle_rule_list
Lifecycle rule information
Struct
id
Lifecycle rule ID
String
prefix
Prefix to which the rule applies
String
status
Indicates whether the rule is enabled. Enumerated values: Enabled, Disabled
String
expire
Expiration attributes of the rule
Struct
days
Number of days before the deletion or transition operation is performed; number of days within which the multipart upload must be completed after it starts
Int
date
Date when the deletion or transition operation is performed
String
transition
Rule transition attribute, which indicates when the object is transited to Standard_IA or Archive
Struct
storage_class
Specify the target storage type for the Object transfer, enumerated values: Standard_IA, Archive. For more storage types, see Storage Class Overview.
String
abort
Maximum time length allowed for a multipart upload
Struct
resp_headers
Returns the HTTP response headers
Struct

Response description

Response Parameter
Description
Local Disk Types
code
Error Codes
Int
error_code
Error code
String
error_msg
Error code description
String
req_id
Request message ID
String

Sample

#include "cos_http_io.h"
#include "cos_api.h"
#include "cos_log.h"
#include <unistd.h>

// endpoint is the COS access domain name. For more information, see https://www.tencentcloud.com/document/product/436/6224.
static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";
// A developer-owned secret ID/key used for the project. It can be obtained at https://console.tencentcloud.com/cam/capi.
static char *TEST_ACCESS_KEY_ID; //your secret_id
static char *TEST_ACCESS_KEY_SECRET; //your secret_key
// The only user-level resource identifier for COS access. It can be obtained at https://console.tencentcloud.com/cam/capi.
static char TEST_APPID[] = "<APPID>"; //your appid
// COS bucket name, in the format of [bucket]-[appid], for example mybucket-1253666666. It can be obtained at https://console.tencentcloud.com/cos5/bucket.
static char TEST_BUCKET_NAME[] = "<bucketname-appid>";

void log_status(cos_status_t *s)
{
cos_warn_log("status->code: %d", s->code);
if (s->error_code) cos_warn_log("status->error_code: %s", s->error_code);
if (s->error_msg) cos_warn_log("status->error_msg: %s", s->error_msg);
if (s->req_id) cos_warn_log("status->req_id: %s", s->req_id);
}

void init_test_config(cos_config_t *config, int is_cname)
{
cos_str_set(&config->endpoint, TEST_COS_ENDPOINT);
cos_str_set(&config->access_key_id, TEST_ACCESS_KEY_ID);
cos_str_set(&config->access_key_secret, TEST_ACCESS_KEY_SECRET);
cos_str_set(&config->appid, TEST_APPID);
config->is_cname = is_cname;
}

void init_test_request_options(cos_request_options_t *options, int is_cname)
{
options->config = cos_config_create(options->pool);
init_test_config(options->config, is_cname);
options->ctl = cos_http_controller_create(options->pool, 0);
}

void test_put_bucket_lifecycle()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_table_t *resp_headers = NULL;

// Initialize request options
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);

// Set lifecycle information
cos_list_t rule_list;
cos_list_init(&rule_list);
cos_lifecycle_rule_content_t *rule_content = NULL;

rule_content = cos_create_lifecycle_rule_content(p);
cos_str_set(&rule_content->id, "testrule1");
cos_str_set(&rule_content->prefix, "abc/");
cos_str_set(&rule_content->status, "Enabled");
rule_content->expire.days = 365;
cos_list_add_tail(&rule_content->node, &rule_list);

rule_content = cos_create_lifecycle_rule_content(p);
cos_str_set(&rule_content->id, "testrule2");
cos_str_set(&rule_content->prefix, "efg/");
cos_str_set(&rule_content->status, "Disabled");
cos_str_set(&rule_content->transition.storage_class, "Standard_IA");
rule_content->transition.days = 999;
cos_list_add_tail(&rule_content->node, &rule_list);

rule_content = cos_create_lifecycle_rule_content(p);
cos_str_set(&rule_content->id, "testrule3");
cos_str_set(&rule_content->prefix, "xxx/");
cos_str_set(&rule_content->status, "Enabled");
rule_content->abort.days = 1;
cos_list_add_tail(&rule_content->node, &rule_list);

// Set Lifecycle
s = cos_put_bucket_lifecycle(options, &bucket, &rule_list, &resp_headers);
log_status(s);

cos_pool_destroy(p);
}

int main(int argc, char *argv[])
{
// Get SECRETID and SECRETKEY from environment variables
TEST_ACCESS_KEY_ID = getenv("COS_SECRETID");
TEST_ACCESS_KEY_SECRET = getenv("COS_SECRETKEY");

if (cos_http_io_initialize(NULL, 0) != COSE_OK) {
exit(1);
}

//set log level, default COS_LOG_WARN
cos_log_set_level(COS_LOG_WARN);

//set log output, default stderr
cos_log_set_output(NULL);

test_put_bucket_lifecycle();

cos_http_io_deinitialize();

return 0;
}

Querying lifecycle

Note

This API is used to query the lifecycle management configuration of a bucket.

Method prototype

cos_status_t *cos_get_bucket_lifecycle(const cos_request_options_t *options,
const cos_string_t *bucket,
cos_list_t *lifecycle_rule_list,
cos_table_t **resp_headers);

Description

Parameter name
ParameterDescription
Local Disk Types
options
COS request options
Struct
bucket
Bucket name in the format of BucketName-APPID
String
lifecycle_rule_list
Lifecycle rule information
Struct
id
Lifecycle rule ID
String
prefix
Prefix to which the rule applies
String
status
Whether to enable the rule. Enumerated values: Enabled, Disabled
String
expire
Expiration attributes of the rule
Struct
days
Number of days before the deletion operation is performed
Int
date
Date when the deletion operation is performed
String
transition
Rule transition attribute, which indicates when the object is transited to Standard_IA or Archive
Struct
days
Number of days before the transition operation is performed
Int
date
Date when the transition operation is performed
String
storage_class
Specify the target storage type for the Object transfer, enumerated values: Standard_IA, Archive. For more storage types, see Storage Class Overview.
String
abort
Maximum time length allowed for a multipart upload
Struct
days
The number of days within which a multipart upload must be completed
Int
resp_headers
Returns the HTTP response headers
Struct

Response description

Response Parameter
Description
Local Disk Types
code
Error Codes
Int
error_code
Error code
String
error_msg
Error code description
String
req_id
Request message ID
String

Sample

#include "cos_http_io.h"
#include "cos_api.h"
#include "cos_log.h"
#include <unistd.h>

// endpoint is the COS access domain name. For more information, see https://www.tencentcloud.com/document/product/436/6224.
static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";
// A developer-owned secret ID/key used for the project. It can be obtained at https://console.tencentcloud.com/cam/capi.
static char *TEST_ACCESS_KEY_ID; //your secret_id
static char *TEST_ACCESS_KEY_SECRET; //your secret_key
// The only user-level resource identifier for COS access. It can be obtained at https://console.tencentcloud.com/cam/capi.
static char TEST_APPID[] = "<APPID>"; //your appid
// COS bucket name, in the format of [bucket]-[appid], for example mybucket-1253666666. It can be obtained at https://console.tencentcloud.com/cos5/bucket.
static char TEST_BUCKET_NAME[] = "<bucketname-appid>";

void log_status(cos_status_t *s)
{
cos_warn_log("status->code: %d", s->code);
if (s->error_code) cos_warn_log("status->error_code: %s", s->error_code);
if (s->error_msg) cos_warn_log("status->error_msg: %s", s->error_msg);
if (s->req_id) cos_warn_log("status->req_id: %s", s->req_id);
}

void init_test_config(cos_config_t *config, int is_cname)
{
cos_str_set(&config->endpoint, TEST_COS_ENDPOINT);
cos_str_set(&config->access_key_id, TEST_ACCESS_KEY_ID);
cos_str_set(&config->access_key_secret, TEST_ACCESS_KEY_SECRET);
cos_str_set(&config->appid, TEST_APPID);
config->is_cname = is_cname;
}

void init_test_request_options(cos_request_options_t *options, int is_cname)
{
options->config = cos_config_create(options->pool);
init_test_config(options->config, is_cname);
options->ctl = cos_http_controller_create(options->pool, 0);
}

void test_get_bucket_lifecycle()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_table_t *resp_headers = NULL;

// Initialize request options
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);

// Query Lifecycle
cos_list_t rule_list_ret;
cos_list_init(&rule_list_ret);
s = cos_get_bucket_lifecycle(options, &bucket, &rule_list_ret, &resp_headers);
if (cos_status_is_ok(s)) {
printf("get bucket lifecycle succeeded\\n");
} else {
printf("get bucket lifecycle failed\\n");
}

cos_pool_destroy(p);
}

int main(int argc, char *argv[])
{
// Get SECRETID and SECRETKEY from environment variables
TEST_ACCESS_KEY_ID = getenv("COS_SECRETID");
TEST_ACCESS_KEY_SECRET = getenv("COS_SECRETKEY");

if (cos_http_io_initialize(NULL, 0) != COSE_OK) {
exit(1);
}

//set log level, default COS_LOG_WARN
cos_log_set_level(COS_LOG_WARN);

//set log output, default stderr
cos_log_set_output(NULL);

test_get_bucket_lifecycle();

cos_http_io_deinitialize();

return 0;
}

Deleting lifecycle

Note

This API is used to delete the lifecycle management configuration of a bucket.

Method prototype

cos_status_t *cos_delete_bucket_lifecycle(const cos_request_options_t *options,
const cos_string_t *bucket,
cos_table_t **resp_headers);

Description

Parameter name
ParameterDescription
Local Disk Types
options
COS request options
Struct
bucket
Bucket name in the format of BucketName-APPID
String
resp_headers
Returns the HTTP response headers
Struct

Response description

Response Parameter
Description
Local Disk Types
code
Error Codes
Int
error_code
Error code
String
error_msg
Error code description
String
req_id
Request message ID
String

Sample

#include "cos_http_io.h"
#include "cos_api.h"
#include "cos_log.h"
#include <unistd.h>

// endpoint is the COS access domain name. For more information, see https://www.tencentcloud.com/document/product/436/6224.
static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";
// A developer-owned secret ID/key used for the project. It can be obtained at https://console.tencentcloud.com/cam/capi.
static char *TEST_ACCESS_KEY_ID; //your secret_id
static char *TEST_ACCESS_KEY_SECRET; //your secret_key
// The only user-level resource identifier for COS access. It can be obtained at https://console.tencentcloud.com/cam/capi.
static char TEST_APPID[] = "<APPID>"; //your appid
// COS bucket name, in the format of [bucket]-[appid], for example mybucket-1253666666. It can be obtained at https://console.tencentcloud.com/cos5/bucket.
static char TEST_BUCKET_NAME[] = "<bucketname-appid>";

void log_status(cos_status_t *s)
{
cos_warn_log("status->code: %d", s->code);
if (s->error_code) cos_warn_log("status->error_code: %s", s->error_code);
if (s->error_msg) cos_warn_log("status->error_msg: %s", s->error_msg);
if (s->req_id) cos_warn_log("status->req_id: %s", s->req_id);
}

void init_test_config(cos_config_t *config, int is_cname)
{
cos_str_set(&config->endpoint, TEST_COS_ENDPOINT);
cos_str_set(&config->access_key_id, TEST_ACCESS_KEY_ID);
cos_str_set(&config->access_key_secret, TEST_ACCESS_KEY_SECRET);
cos_str_set(&config->appid, TEST_APPID);
config->is_cname = is_cname;
}

void init_test_request_options(cos_request_options_t *options, int is_cname)
{
options->config = cos_config_create(options->pool);
init_test_config(options->config, is_cname);
options->ctl = cos_http_controller_create(options->pool, 0);
}

void test_delete_bucket_lifecycle()
{
cos_pool_t *p = NULL;
int is_cname = 0;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_string_t bucket;
cos_table_t *resp_headers = NULL;

// Initialize request options
cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, TEST_BUCKET_NAME);

// Delete Lifecycle
s = cos_delete_bucket_lifecycle(options, &bucket, &resp_headers);
if (cos_status_is_ok(s)) {
printf("delete bucket lifecycle succeeded\\n");
} else {
printf("delete bucket lifecycle failed\\n");
}

cos_pool_destroy(p);
}

int main(int argc, char *argv[])
{
// Get SECRETID and SECRETKEY from environment variables
TEST_ACCESS_KEY_ID = getenv("COS_SECRETID");
TEST_ACCESS_KEY_SECRET = getenv("COS_SECRETKEY");

if (cos_http_io_initialize(NULL, 0) != COSE_OK) {
exit(1);
}

//set log level, default COS_LOG_WARN
cos_log_set_level(COS_LOG_WARN);

//set log output, default stderr
cos_log_set_output(NULL);

test_delete_bucket_lifecycle();

cos_http_io_deinitialize();

return 0;
}

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback