简介
本文档提供关于静态网站的 API 概览以及 SDK 示例代码。
| API | 操作名 | 操作描述 | 
| 设置静态网站 | 设置存储桶的静态网站配置 | |
| 查询静态网站配置 | 查询存储桶的静态网站配置 | |
| 删除静态网站配置 | 删除存储桶的静态网站配置 | 
设置静态网站
功能说明
PUT Bucket website 用于为存储桶配置静态网站。
方法原型
func (s *BucketService) PutWebsite(ctx context.Context, opt *BucketPutWebsiteOptions) (*Response, error)
请求示例
package mainimport ("context""github.com/tencentyun/cos-go-sdk-v5""net/http""net/url""os")func main() {// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket// 替换为用户的 region,存储桶 region 可以在 COS 控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 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{// 通过环境变量获取密钥// 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capiSecretID: os.Getenv("SECRETID"), // 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140// 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capiSecretKey: os.Getenv("SECRETKEY"), // 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 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}}
参数说明
type WebsiteRoutingRule struct {ConditionErrorCode stringConditionPrefix stringRedirectProtocol stringRedirectReplaceKey stringRedirectReplaceKeyPrefix string}type WebsiteRoutingRules struct {Rules []WebsiteRoutingRule}type ErrorDocument struct {Key string}type RedirectRequestsProtocol struct {Protocol string}type BucketPutWebsiteOptions struct {XMLName xml.NameIndex stringRedirectProtocol *RedirectRequestsProtocolError *ErrorDocumentRoutingRules *WebsiteRoutingRules}
| 参数名称 | 描述 | 类型 | 
| BucketPutWebsiteOptions | 静态网站配置参数 | Struct | 
| Index | 指定索引文档 | String | 
| RedirectProtocol | 全站重定向的协议 | Struct | 
| Protocol | 指定全站重定向的协议,只能设置为 https | String | 
| Error | 错误文档 | Struct | 
| Key | 指定通用错误返回 | String | 
| RoutingRules | 设置重定向规则,最多设置100条 RoutingRule | Struct | 
| ConditionErrorCode | 指定重定向错误码,只支持配置4XX返回码,优先级高于 Error.Key | String | 
| ConditionPrefix | 指定前缀重定向的路径,替换指定的 folder/ | String | 
| RedirectProtocol | 指定重定向规定的协议,只能设置为 https | String | 
| RedirectReplaceKey | 替换整个 Key 为指定的内容 | String | 
| RedirectReplaceKeyPrefix | 替换匹配到的前缀为指定的内容,Condition 为 KeyPrefixEquals 才可设置 | String | 
查询静态网站配置
功能说明
GET Bucket website 用于查询与存储桶关联的静态网站配置信息。
方法原型
func (s *BucketService) GetWebsite(ctx context.Context) (*BucketGetWebsiteResult, *Response, error)
请求示例
package mainimport ("context""fmt""github.com/tencentyun/cos-go-sdk-v5""net/http""net/url""os")func main() {// 存储桶名称,由bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket// 替换为用户的 region,存储桶 region 可以在 COS 控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 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{// 通过环境变量获取密钥// 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capiSecretID: os.Getenv("SECRETID"), // 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140// 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capiSecretKey: os.Getenv("SECRETKEY"), // 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140},})res, _, err := client.Bucket.GetWebsite(context.Background())if err != nil {// ERROR}fmt.Println(res)}
返回结果说明
type BucketGetWebsiteResult BucketPutWebsiteOptions
| 参数名称 | 描述 | 类型 | 
| BucketGetWebsiteResult | 静态网站配置参数 | Struct | 
| Index | 指定索引文档 | String | 
| RedirectProtocol | 全站重定向的协议 | Struct | 
| Protocol | 指定全站重定向的协议,只能设置为 https | String | 
| Error | 通用错误返回 | Struct | 
| Key | 指定通用错误返回 | String | 
| RoutingRules | 设置重定向规则,最多设置100条 RoutingRule | Struct | 
| ConditionErrorCode | 指定重定向错误码,只支持配置4XX返回码,优先级高于 Error.Key | String | 
| ConditionPrefix | 指定前缀重定向的路径,替换指定的 folder/ | String | 
| RedirectProtocol | 指定重定向规定的协议,只能设置为 https | String | 
| RedirectReplaceKey | 替换整个 Key 为指定的内容 | String | 
| RedirectReplaceKeyPrefix | 替换匹配到的前缀为指定的内容,Condition 为 KeyPrefixEquals 才可设置 | String | 
删除静态网站配置
功能说明
DELETE Bucket website 用于删除存储桶中的静态网站配置。
方法原型
func (s *BucketService) DeleteWebsite(ctx context.Context) (*Response, error)
请求示例
package mainimport ("context""github.com/tencentyun/cos-go-sdk-v5""net/http""net/url""os")func main() {// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket// 替换为用户的 region,存储桶 region 可以在 COS 控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 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{// 通过环境变量获取密钥// 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capiSecretID: os.Getenv("SECRETID"), // 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140// 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capiSecretKey: os.Getenv("SECRETKEY"), // 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140},})_, err := client.Bucket.DeleteWebsite(context.Background())if err != nil {// ERROR}}