Static Website

Last updated: 2020-03-20 12:36:26

PDF

Note

This document provides an overview of API on static websites and sample SDK code.

API Operation name pedagogical operation
PUT Bucket website Setting up a Static Website Set Bucket's static website configuration
GET Bucket website Query static site configuration Query Bucket's static website configuration
DELETE Bucket website Delete static site configuration Delete Bucket's static website configuration

Setting up a Static Website

Function description

PUT Bucket website is used to configure a static website for Bucket.

Method prototype

cos_status_t *cos_put_bucket_website(const cos_request_options_t *options,
                                      const cos_string_t *bucket,
                                      cos_website_params_t *website_params,
                                      cos_table_t **resp_header);

Request Sample

cos_pool_t *pool = NULL;
int is_cname = 0;
cos_status_t *status = NULL;
cos_request_options_t *options = NULL;
cos_website_params_t  *website_params = NULL;
cos_website_params_t  *website_result = NULL;
cos_website_rule_content_t *website_content = NULL;
cos_table_t *resp_headers = NULL;
cos_string_t bucket;


cos_pool_create(&pool, NULL);


options = cos_request_options_create(pool);
options->config = cos_config_create(options->pool);
cos_str_set(&options->config->endpoint, TEST_COS_ENDPOINT);
cos_str_set(&options->config->access_key_id, TEST_ACCESS_KEY_ID);
cos_str_set(&options->config->access_key_secret, TEST_ACCESS_KEY_SECRET);
cos_str_set(&options->config->appid, TEST_APPID);
options->config->is_cname = is_cname;
options->ctl = cos_http_controller_create(options->pool, 0);
cos_str_set(&bucket, TEST_BUCKET_NAME);

website_params = cos_create_website_params(options->pool);
cos_str_set(&website_params->index, "index.html");
cos_str_set(&website_params->redirect_protocol, "https");
cos_str_set(&website_params->error_document, "Error.html");

website_content = cos_create_website_rule_content(options->pool);
cos_str_set(&website_content->condition_errcode, "404");
cos_str_set(&website_content->redirect_protocol, "https");
cos_str_set(&website_content->redirect_replace_key, "404.html");
cos_list_add_tail(&website_content->node, &website_params->rule_list);

website_content = cos_create_website_rule_content(options->pool);
cos_str_set(&website_content->condition_prefix, "docs/");
cos_str_set(&website_content->redirect_protocol, "https");
cos_str_set(&website_content->redirect_replace_key_prefix, "documents/");
cos_list_add_tail(&website_content->node, &website_params->rule_list);

website_content = cos_create_website_rule_content(options->pool);
cos_str_set(&website_content->condition_prefix, "img/");
cos_str_set(&website_content->redirect_protocol, "https");
cos_str_set(&website_content->redirect_replace_key, "demo.jpg");
cos_list_add_tail(&website_content->node, &website_params->rule_list);

status = cos_put_bucket_website(options, &bucket, website_params, &resp_headers);
log_status(status);

cos_pool_destroy(pool);

Parameter description

Parameter name Description Type
OPTIONS COS request options Struct
Bucket Set the Bucket of the static website in BucketName-APPID format. For more information, please see Naming convention String
Website_params Bucket static website configuration information Struct
INDEX Specify an indexed document String
Redirect_protocol Protocol who specifies the site-wide redirection can only be set to https String
Error_document Specify generic error return String
Rule_list Set redirection rules. Set a maximum of 100 RoutingRule List
Condition_errcode Specify redirect error code. Only 4XX error code can be configured. Priority is higher than error_document. String
Condition_prefix Specifies the path to the prefix redirection, replacing the specified folder/ String
Redirect_protocol Protocol, which specifies the redirection, can only be set to https. String
Redirect_replace_key Replace the entire Key with the specified content String
Redirect_replace_key_prefix Replace the prefix that matches to the specified content, and the Conditon can only be set to KeyPrefixEquals. String
Resp_headers Returns the header field of the HTTP response message Struct

Return result description

Returned Result Description Type
Code Error Codes Int
Error_code Error code content String
Error_msg Error code description String
Req_id Request message ID String

Query static site configuration

Function description

GET Bucket website is used to query static website configuration information with Bucket and Associate.

Method prototype

cos_status_t *cos_get_bucket_website(const cos_request_options_t *options,
                                      const cos_string_t *bucket,
                                      cos_website_params_t *website_params,
                                      cos_table_t **resp_header);

Request Sample

cos_pool_t *pool = NULL;
int is_cname = 0;
cos_status_t *status = NULL;
cos_request_options_t *options = NULL;
cos_website_params_t  *website_params = NULL;
cos_website_params_t  *website_result = NULL;
cos_website_rule_content_t *website_content = NULL;
cos_table_t *resp_headers = NULL;
cos_string_t bucket;

cos_pool_create(&pool, NULL);

options = cos_request_options_create(pool);
options->config = cos_config_create(options->pool);
cos_str_set(&options->config->endpoint, TEST_COS_ENDPOINT);
cos_str_set(&options->config->access_key_id, TEST_ACCESS_KEY_ID);
cos_str_set(&options->config->access_key_secret, TEST_ACCESS_KEY_SECRET);
cos_str_set(&options->config->appid, TEST_APPID);
options->config->is_cname = is_cname;
options->ctl = cos_http_controller_create(options->pool, 0);
cos_str_set(&bucket, TEST_BUCKET_NAME);

website_result = cos_create_website_params(options->pool);
status = cos_get_bucket_website(options, &bucket, website_result, &resp_headers);
log_status(status);

cos_pool_destroy(pool);

Parameter description

Parameter name Description Type
OPTIONS COS request options Struct
Bucket Query Bucket configured on a static website in BucketName-APPID format. For more information, please see Naming convention String
Website_params Bucket static website configuration information Struct
INDEX Specify an indexed document String
Redirect_protocol Protocol who specifies the site-wide redirection can only be set to https String
Error_document Specify generic error return String
Rule_list Set redirection rules. Set a maximum of 100 RoutingRule List
Condition_errcode Specify redirect error code. Only 4XX error code can be configured. Priority is higher than error_document. String
Condition_prefix Specifies the path to the prefix redirection, replacing the specified folder/ String
Redirect_protocol Protocol, which specifies the redirection, can only be set to https. String
Redirect_replace_key Replace the entire Key with the specified content String
Redirect_replace_key_prefix Replace the prefix that matches to the specified content, and the Conditon can only be set to KeyPrefixEquals. String
Resp_headers Returns the header field of the HTTP response message Struct

Return result description

Returned Result Description Type
Code Error Codes Int
Error_code Error code content String
Error_msg Error code description String
Req_id Request message ID String

Delete static site configuration

Function description

DELETE Bucket website is used to delete the static website configuration in Bucket.

Method prototype

cos_status_t *cos_delete_bucket_website(const cos_request_options_t *options,
                                          const cos_string_t *bucket,
                                          cos_table_t **resp_header);

Request Sample

cos_pool_t *pool = NULL;
int is_cname = 0;
cos_status_t *status = NULL;
cos_request_options_t *options = NULL;
cos_website_params_t  *website_params = NULL;
cos_website_params_t  *website_result = NULL;
cos_website_rule_content_t *website_content = NULL;
cos_table_t *resp_headers = NULL;
cos_string_t bucket;

cos_pool_create(&pool, NULL);

options = cos_request_options_create(pool);
options->config = cos_config_create(options->pool);
cos_str_set(&options->config->endpoint, TEST_COS_ENDPOINT);
cos_str_set(&options->config->access_key_id, TEST_ACCESS_KEY_ID);
cos_str_set(&options->config->access_key_secret, TEST_ACCESS_KEY_SECRET);
cos_str_set(&options->config->appid, TEST_APPID);
options->config->is_cname = is_cname;
options->ctl = cos_http_controller_create(options->pool, 0);
cos_str_set(&bucket, TEST_BUCKET_NAME);

status = cos_delete_bucket_website(options, &bucket, &resp_headers);
log_status(status); 

cos_pool_destroy(pool);

Parameter description

Parameter name Description Type
OPTIONS COS request options Struct
Bucket Bucket, whose static website configuration is deleted, is in BucketName-APPID format. For more information, please see Naming convention String
Resp_headers Returns the header field of the HTTP response message Struct

Return result description

Returned Result Description Type
Code Error Codes Int
Error_code Error code content String
Error_msg Error code description String
Req_id Request message ID String