控制台指南

最佳实践

开发者指南

数据湖存储

API 文档

SDK 文档

文档捉虫大赛火热进行中,好礼多多> HOT

简介

本文档提供关于跨域访问的 API 概览以及 SDK 示例代码。

API 操作名 操作描述
PUT Bucket cors 设置跨域配置 设置存储桶的跨域名访问权限
GET Bucket cors 查询跨域配置 查询存储桶的跨域名访问配置信息
DELETE Bucket cors 删除跨域配置 删除存储桶的跨域名访问配置信息

设置跨域配置

功能说明

设置指定存储桶的跨域名访问配置信息(PUT Bucket cors)。

方法原型

func (s *BucketService) PutCORS(ctx context.Context, opt *BucketPutCORSOptions) (*Response, error)

请求示例

package main

import (
    "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/capi
            SecretID: os.Getenv("SECRETID"),  // 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
            // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
            SecretKey: os.Getenv("SECRETKEY"),  // 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
        },
    })
    opt := &cos.BucketPutCORSOptions{
        Rules: []cos.BucketCORSRule{
            {
                AllowedOrigins: []string{"http://www.qq.com"},
                AllowedMethods: []string{"PUT", "GET"},
                AllowedHeaders: []string{"x-cos-meta-test", "x-cos-xx"},
                MaxAgeSeconds:  500,
                ExposeHeaders:  []string{"x-cos-meta-test1"},
            },
            {
                ID:             "1234",
                AllowedOrigins: []string{"http://www.baidu.com", "twitter.com"},
                AllowedMethods: []string{"PUT", "GET"},
                MaxAgeSeconds:  500,
            },
        },
    }
    _, err := client.Bucket.PutCORS(context.Background(), opt)
    if err != nil {
        panic(err)
    }
}

参数说明

type BucketCORSRule struct {
    ID             string   
    AllowedMethods []string 
    AllowedOrigins []string 
    AllowedHeaders []string 
    MaxAgeSeconds  int      
    ExposeHeaders  []string 
}

参数名称 参数描述 类型 是否必填
BucketCORSRule 设置对应的跨域规则,包括 ID,MaxAgeSeconds,AllowedOrigin,AllowedMethod,AllowedHeader,ExposeHeader struct
ID 设置规则的 ID string
AllowedMethods 设置允许的方法,如 GET,PUT,HEAD,POST,DELETE []string
AllowedOrigins 设置允许的访问来源,如 "http://cloud.tencent.com",支持通配符 * []string
AllowedHeaders 设置请求可以使用哪些自定义的 HTTP 请求头部,支持通配符 * []string
MaxAgeSeconds 设置 OPTIONS 请求得到结果的有效期 int
ExposeHeaders 设置浏览器可以接收到的来自服务器端的自定义头部信息 []string

查询跨域配置

功能说明

查询指定存储桶的跨域名访问配置信息(GET Bucket cors)。

方法原型

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

请求示例

package main

import (
    "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/capi
            SecretID: os.Getenv("SECRETID"),  // 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
            // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
            SecretKey: os.Getenv("SECRETKEY"),  // 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
        },
    })
    _, _, err := client.Bucket.GetCORS(context.Background())
    if err != nil {
        panic(err)
    }
}

返回结果说明

通过 GetBucketCORSResult 返回请求结果。

type BucketCORSRule struct {
    ID             string   
    AllowedMethods []string 
    AllowedOrigins []string 
    AllowedHeaders []string 
    MaxAgeSeconds  int      
    ExposeHeaders  []string 
}

参数名称 参数描述 类型 是否必填
BucketCORSRule 设置对应的跨域规则,包括 ID,MaxAgeSeconds,AllowedOrigin,AllowedMethod,AllowedHeader,ExposeHeader struct
ID 设置规则的 ID string
AllowedMethods 设置允许的方法,如 GET,PUT,HEAD,POST,DELETE []string
AllowedOrigins 设置允许的访问来源,如 "http://cloud.tencent.com",支持通配符 * []string
AllowedHeaders 设置请求可以使用哪些自定义的 HTTP 请求头部,支持通配符 * []string
MaxAgeSeconds 设置 OPTIONS 请求得到结果的有效期 int
ExposeHeaders 设置浏览器可以接收到的来自服务器端的自定义头部信息 []string

删除跨域配置

功能说明

删除指定存储桶的跨域名访问配置(DELETE Bucket cors)。

方法原型

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

请求示例

package main

import (
    "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/capi
            SecretID: os.Getenv("SECRETID"),  // 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
            // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
            SecretKey: os.Getenv("SECRETKEY"),  // 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
        },
    })
    _, err := client.Bucket.DeleteCORS(context.Background())
    if err != nil {
        panic(err)
    }
}

目录