Static Websites

Last updated: 2023-09-13 14:55:19

Feature Overview

This document provides an overview of APIs and SDK code samples related to static websites.
API
Operation
Description
Setting a static website
Configures a static website for a bucket
Querying Static Website Configuration
Queries the static website configuration of a bucket
Deleting Static Website Configuration
Deletes the static website configuration of a bucket

Setting a static website

Note

This API is used to configure a static website for a bucket.

Method prototype

func (s *BucketService) PutWebsite(ctx context.Context, opt *BucketPutWebsiteOptions) (*Response, error)

Sample Request

package main

import (
"context"
"github.com/tencentyun/cos-go-sdk-v5"
"net/http"
"net/url"
"os"
)

func main() {
// Bucket name, composed of bucketname-appid, appid is required. The bucket name can be viewed in the COS console: https://console.cloud.tencent.com/cos5/bucket
// Replace with the user's region. The bucket region can be viewed in the COS console "Bucket Overview" at https://console.cloud.tencent.com/. For more information about regions, visit https://cloud.tencent.com/document/product/436/6224.
u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// Get the key from environment variables
// Environment variable SECRETID represents the user's SecretId. To view the key, log in to the Access Management Console at https://console.cloud.tencent.com/cam/capi.
SecretID: os.Getenv("SECRETID"), // User's SecretId, it is recommended to use a sub-account key, following the principle of least privilege to reduce usage risks. For obtaining a sub-account key, please refer to https://cloud.tencent.com/document/product/598/37140
// The environment variable SECRETKEY represents the user's SecretKey. To view the key, log in to the CAM console at https://console.cloud.tencent.com/cam/capi.
SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey, it is recommended to use a sub-account key, following the principle of least privilege to reduce usage risks. For obtaining a sub-account key, please refer to https://cloud.tencent.com/document/product/598/37140
},
})
opt := &cos.BucketPutWebsiteOptions{
Index: "index.html",
Error: &cos.ErrorDocument{"index_backup.html"},
RoutingRules: &cos.WebsiteRoutingRules{
[]cos.WebsiteRoutingRule{
{
ConditionErrorCode: "404",
RedirectProtocol: "https",
RedirectReplaceKey: "404.html",
},
{
ConditionPrefix: "docs/",
RedirectProtocol: "https",
RedirectReplaceKeyPrefix: "documents/",
},
},
},
}
_, err := client.Bucket.PutWebsite(context.Background(), opt)
if err != nil {
// ERROR
}
}

Description

type WebsiteRoutingRule struct {
ConditionErrorCode string
ConditionPrefix string

RedirectProtocol string
RedirectReplaceKey string
RedirectReplaceKeyPrefix string
}

type WebsiteRoutingRules struct {
Rules []WebsiteRoutingRule
}

type ErrorDocument struct {
Key string
}

type RedirectRequestsProtocol struct {
Protocol string
}

type BucketPutWebsiteOptions struct {
XMLName xml.Name
Index string
RedirectProtocol *RedirectRequestsProtocol
Error *ErrorDocument
RoutingRules *WebsiteRoutingRules
}
Parameter name
Description
Local Disk Types
BucketPutWebsiteOptions
Static website configuration parameters
Struct
Index
Index document suffix
String
RedirectProtocol
Site-wide redirect protocol
Struct
Protocol
Specifies the site-wide redirect protocol. Only HTTPS is supported
String
Error
Error document
Struct
Key
Common error response
String
RoutingRules
Multiple redirect rules. Up to 100 redirect rules can be set.
Struct
ConditionErrorCode
Redirect error code. Only 4xx status codes are supported. This has a higher priority than Error.Key.
String
ConditionPrefix
Specifies the object key prefix to replace with the specified “folder/” for the redirect
String
RedirectProtocol
Redirect protocol. Only HTTPS is supported.
String
RedirectReplaceKey
Content that is used to replace the entire key.
String
RedirectReplaceKeyPrefix
Replace the matched prefix with the specified content; this can only be set when the Condition is KeyPrefixEquals.
String

Querying Static Website Configuration

Note

This API (GET Bucket website) is used to query the static website configuration associated with a bucket.

Method prototype

func (s *BucketService) GetWebsite(ctx context.Context) (*BucketGetWebsiteResult, *Response, error)

Sample Request

package main

import (
"context"
"fmt"
"github.com/tencentyun/cos-go-sdk-v5"
"net/http"
"net/url"
"os"
)

func main() {
// Bucket name, composed of bucketname-appid, appid is required. The bucket name can be viewed in the COS console: https://console.cloud.tencent.com/cos5/bucket
// Replace with the user's region. The bucket region can be viewed in the COS console "Bucket Overview" at https://console.cloud.tencent.com/. For more information about regions, visit https://cloud.tencent.com/document/product/436/6224.
u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// Get the key from environment variables
// Environment variable SECRETID represents the user's SecretId. To view the key, log in to the Access Management Console at https://console.cloud.tencent.com/cam/capi.
SecretID: os.Getenv("SECRETID"), // User's SecretId, it is recommended to use a sub-account key, following the principle of least privilege to reduce usage risks. For obtaining a sub-account key, please refer to https://cloud.tencent.com/document/product/598/37140
// The environment variable SECRETKEY represents the user's SecretKey. To view the key, log in to the CAM console at https://console.cloud.tencent.com/cam/capi.
SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey, it is recommended to use a sub-account key, following the principle of least privilege to reduce usage risks. For obtaining a sub-account key, please refer to https://cloud.tencent.com/document/product/598/37140
},
})
res, _, err := client.Bucket.GetWebsite(context.Background())
if err != nil {
// ERROR
}
fmt.Println(res)
}

Response description

type BucketGetWebsiteResult BucketPutWebsiteOptions
Parameter name
Description
Local Disk Types
BucketGetWebsiteResult
Static website configuration parameters
Struct
Index
Index document suffix
String
RedirectProtocol
Site-wide redirect protocol
Struct
Protocol
Specifies the site-wide redirect protocol. Only HTTPS is supported
String
Error
Common error response
Struct
Key
Common error response
String
RoutingRules
Multiple redirect rules. Up to 100 redirect rules can be set.
Struct
ConditionErrorCode
Redirect error code. Only 4xx status codes are supported. This has a higher priority than Error.Key.
String
ConditionPrefix
Specifies the object key prefix to replace with the specified “folder/” for the redirect
String
RedirectProtocol
Redirect protocol. Only HTTPS is supported.
String
RedirectReplaceKey
Content that is used to replace the entire key.
String
RedirectReplaceKeyPrefix
Replace the matched prefix with the specified content; this can only be set when the Condition is KeyPrefixEquals.
String

Deleting Static Website Configuration

Note

This API (DELETE Bucket website) is used to delete the static website configuration of a bucket.

Method prototype

func (s *BucketService) DeleteWebsite(ctx context.Context) (*Response, error)

Sample Request

package main

import (
"context"
"github.com/tencentyun/cos-go-sdk-v5"
"net/http"
"net/url"
"os"
)

func main() {
// Bucket name, composed of bucketname-appid, appid is required. The bucket name can be viewed in the COS console: https://console.cloud.tencent.com/cos5/bucket
// Replace with the user's region. The bucket region can be viewed in the COS console "Bucket Overview" at https://console.cloud.tencent.com/. For more information about regions, visit https://cloud.tencent.com/document/product/436/6224.
u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// Get the key from environment variables
// Environment variable SECRETID represents the user's SecretId. To view the key, log in to the Access Management Console at https://console.cloud.tencent.com/cam/capi.
SecretID: os.Getenv("SECRETID"), // User's SecretId, it is recommended to use a sub-account key, following the principle of least privilege to reduce usage risks. For obtaining a sub-account key, please refer to https://cloud.tencent.com/document/product/598/37140
// The environment variable SECRETKEY represents the user's SecretKey. To view the key, log in to the CAM console at https://console.cloud.tencent.com/cam/capi.
SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey, it is recommended to use a sub-account key, following the principle of least privilege to reduce usage risks. For obtaining a sub-account key, please refer to https://cloud.tencent.com/document/product/598/37140
},
})
_, err := client.Bucket.DeleteWebsite(context.Background())
if err != nil {
// ERROR
}
}