tencent cloud

Cloud Object Storage

最新情報とお知らせ
製品アップデート情報
製品のお知らせ
製品概要
製品概要
機能概要
応用シナリオ
製品の優位性
基本概念
リージョンとアクセスドメイン名
仕様と制限
製品の課金
課金概要
課金方式
課金項目
無料利用枠
記帳例
請求書の確認とダウンロード
お支払い遅れについて
よくある質問
クイックスタート
コンソールクイックスタート
COSBrowserクイックスタート
ユーザーガイド
リクエストの作成
バケット
オブジェクト
データ管理
バッチ処理
グローバルアクセラレーション
監視とアラーム
運用管理センター
データ処理
インテリジェントツールボックス使用ガイド
データワークフロー
アプリ統合
ツールガイド
ツール概要
環境のインストールと設定
COSBrowserツール
COSCLIツール
COSCMDツール
COS Migrationツール
FTP Serverツール
Hadoopツール
COSDistCpツール
HDFS TO COSツール
オンラインツール (Onrain Tsūru)
セルフ診断ツール
実践チュートリアル
概要
アクセス制御と権限管理
パフォーマンスの最適化
AWS S3 SDKを使用したCOSアクセス
データディザスタリカバリバックアップ
ドメイン名管理の実践
画像処理の実践
COSオーディオビデオプレーヤーの実践
データセキュリティ
データ検証
COSコスト最適化ソリューション
サードパーティアプリケーションでのCOSの使用
移行ガイド
サードパーティクラウドストレージのデータをCOSへ移行
データレークストレージ
クラウドネイティブデータレイク
メタデータアクセラレーション
データアクセラレーター GooseFS
データ処理
データ処理概要
画像処理
メディア処理
コンテンツ審査
ファイル処理
ドキュメントプレビュー
トラブルシューティング
RequestId取得の操作ガイド
パブリックネットワーク経由でのCOSへのファイルアップロード速度の遅さ
COSへのアクセス時に403エラーコードが返される
リソースアクセス異常
POST Objectの一般的な異常
セキュリティとコンプライアンス
データ災害復帰
データセキュリティ
クラウドアクセスマネジメント
よくある質問
よくあるご質問
一般的な問題
従量課金に関するご質問
ドメインコンプライアンスに関するご質問
バケット設定に関する質問
ドメイン名とCDNに関するご質問
ファイル操作に関するご質問
権限管理に関するご質問
データ処理に関するご質問
データセキュリティに関するご質問
署名付きURLに関するご質問
SDKクラスに関するご質問
ツール類に関するご質問
APIクラスに関するご質問
Agreements
Service Level Agreement
プライバシーポリシー
データ処理とセキュリティ契約
連絡先
用語集
ドキュメントCloud Object Storage

Quick Start

フォーカスモード
フォントサイズ
最終更新日: 2025-12-29 16:25:12

Relevant Resources

The source code download address for XML Go SDK for COS: XML Go SDK.
Download address for the Example Demo: COS XML Go SDK sample.
For more information, see the COS Go SDK API documentation.
For all sample code in the SDK documentation, see SDK Code Samples.
For the change log of the SDK, see ChangeLog.
For SDK FAQ, see: Go SDK FAQ.
Note:
If you encounter errors such as missing functions or methods when using the SDK, please upgrade to the latest version and try again.

Environment Configuration and Preparation

Golang: used to download and install the Go compilation and run environment. Go to the Golang official website to download it.
The minimum supported Go version is 1.16, and it is recommended to use the latest stable version 1.24.10.
You need to obtain a Tencent Cloud API key. This key is a prerequisite for your use of the various features of the COS SDK.

Installing the SDK

Run the following command to install the Go SDK for COS.
go get -u github.com/tencentyun/cos-go-sdk-v5

Initialize COS Service

The following describes how to use the COS Go SDK to perform basic operations, such as initializing a client, creating a bucket, querying the bucket list, uploading an object, querying the object list, downloading an object, and deleting an object.

Initialize COS

Use the COS domain name to create a Client structure for COS Go.

Method Prototype

func NewClient(uri *BaseURL, httpClient *http.Client) *Client

Parameter Description

// BaseURL The base URL required to access each API
type BaseURL struct {
// Base URL for accessing APIs related to bucket and object (excluding the path part): https://examplebucket-1250000000.cos.<Region>.myqcloud.com
BucketURL *url.URL
// Base URL for accessing service APIs (excluding the path part): https://cos.<Region>.myqcloud.com
ServiceURL *url.URL
// Base URL for accessing Batch APIs (excluding the path part): https://<UIN>.cos-control.<Region>.myqcloud.com
BatchURL *url.URL
// Base URL for accessing CI (excluding the path part): https://examplebucket-1250000000.ci.<Region>.myqcloud.com.
CIURL *url.URL
}
Parameter Name
Parameter Description
Type
Required
BucketURL
Base URL for accessing APIs related to bucket and object (excluding the path part)
string
Yes
ServiceURL
Base URL for accessing service APIs (excluding the path part)
string
No
BatchURL
Base URL for accessing Batch APIs (excluding the path part).
string
No
CIURL
Base URL for accessing CI (excluding the path part).
string
No

Configure Access Key for API

Access Keys are a prerequisite for COS SDK to access all services. The keys are divided into: temporary keys and permanent keys.
Note:
It is recommended that users use sub-account keys + environment variables to call the SDK to enhance the security of SDK usage. When authorizing sub-accounts, follow the least privilege principle to prevent the leakage of resources other than the target bucket or objects.
If you must use permanent secret keys, it is recommended that you follow the Principle of Least Privilege to restrict the scope of permissions for these keys.
Temporary Secret Key (Recommended)
permanent secret keys
Temporary secret key generation and usage can be found in Temporary Secret Key Generation and Usage Guidelines.
// Replace examplebucket-1250000000 and COS_REGION with your actual information.
// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
// 2. Temporary Secret Keys
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// If you are using temporary secret keys, you need to enter them. For guidelines on generating and using temporary secret keys, see https://www.tencentcloud.com/document/product/436/14048
SecretID: "SECRETID",
SecretKey: "SECRETKEY",
SessionToken: "SECRETTOKEN",
},
})
if client != nil {
// Call COS requests.
}

// Replace examplebucket-1250000000 and COS_REGION with your actual information.
// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.
u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
// Used for Get Service queries, defaults to all regions: service.cos.myqcloud.com.
su, _ := url.Parse("https://cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: u, ServiceURL: su}
// 1. Permanent Secret Keys
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"), // User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
},
})

Configure Domain Name

By modifying the BaseURL, you can directly use a custom domain name or global acceleration domain name to access COS.
// Use a domain name for global acceleration to access COS.
u, _ := url.Parse("http://<BucketName-APPID>.cos.accelerate.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
// 2. Temporary Secret Keys
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// If you are using temporary secret keys, you need to enter them. For guidelines on generating and using temporary secret keys, see https://www.tencentcloud.com/document/product/436/14048.
SecretID: "SECRETID",
SecretKey: "SECRETKEY",
SessionToken: "SECRETTOKEN",
},
})

CRC64 Check (Optional)

COS Go SDK has CRC64 verification for file uploads enabled by default.
Note:
COS Go SDK requires version v0.7.23 or above.
It is strongly recommended that users do not disable CRC64 verification.
// Replace examplebucket-1250000000 and COS_REGION with your actual information.
// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.
u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
// 2. Temporary Secret Keys
client := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
// If you are using temporary secret keys, you need to enter them. For guidelines on generating and using temporary secret keys, see https://www.tencentcloud.com/document/product/436/14048.
SecretID: "SECRETID",
SecretKey: "SECRETKEY",
SessionToken: "SECRETTOKEN",
},
})
// Disable CRC64 verification.
client.Conf.EnableCRC = false


Access the COS Service

Create a bucket
Querying the Bucket List
PUT Object
Querying the Object List
Downloading an Object
Deleting Objects
The following are basic examples. For more sample demos, see GitHub address.
package main


import (
"context"
"net/http"
"net/url"
"os"

"github.com/tencentyun/cos-go-sdk-v5"
)


func main() {
// Replace examplebucket-1250000000 and COS_REGION with your actual information.
// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.
u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"), // User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
},
})


_, err := c.Bucket.Put(context.Background(), nil)
if err != nil {
panic(err)
}
}
The following are basic examples. For more sample demos, see GitHub address.
package main


import (
"context"
"fmt"
"net/http"
"os"


"github.com/tencentyun/cos-go-sdk-v5"
)


func main() {
c := cos.NewClient(nil, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"), // User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
},
})


s, _, err := c.Service.Get(context.Background())
if err != nil {
panic(err)
}


for _, b := range s.Buckets {
fmt.Printf("%#v\\n", b)
}
}
The following are basic examples. For more sample demos, see GitHub address.
package main


import (
"context"
"net/http"
"net/url"
"os"
"strings"

"github.com/tencentyun/cos-go-sdk-v5"
)


func main() {
// Replace examplebucket-1250000000 and COS_REGION with your actual information.
// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.
u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"), // User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1
},
})
// The object key is the unique identifier of the object in the bucket.
// For example, in the object's domain name `examplebucket-1250000000.cos.COS_REGION.myqcloud.com/test/objectPut.go`, the object key is test/objectPut.go.
name := "test/objectPut.go"
// 1. Upload an object via a string.
f := strings.NewReader("test")


_, err := c.Object.Put(context.Background(), name, f, nil)
if err != nil {
panic(err)
}
// 2. Upload an object via a local file.
_, err = c.Object.PutFromFile(context.Background(), name, "../test", nil)
if err != nil {
panic(err)
}
// 3. Upload an object via a file stream.
fd, err := os.Open("./test")
if err != nil {
panic(err)
}
defer fd.Close()
_, err = c.Object.Put(context.Background(), name, fd, nil)
if err != nil {
panic(err)
}
}
The following are basic examples. For more sample demos, see GitHub address.
package main


import (
"context"
"fmt"
"net/http"
"net/url"
"os"

"github.com/tencentyun/cos-go-sdk-v5"
)


func main() {
// Replace examplebucket-1250000000 and COS_REGION with your actual information.
// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.
u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"), // User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
},
})


opt := &cos.BucketGetOptions{
Prefix: "test",
MaxKeys: 3,
}
v, _, err := c.Bucket.Get(context.Background(), opt)
if err != nil {
panic(err)
}


for _, c := range v.Contents {
fmt.Printf("%s, %d\\n", c.Key, c.Size)
}
}
The following are basic examples. For more sample demos, see GitHub address.
package main


import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"

"github.com/tencentyun/cos-go-sdk-v5"
)


func main() {
// Replace examplebucket-1250000000 and COS_REGION with your actual information.
// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.
u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"), // User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
},
})
// 1. Obtain the object via the response body.
name := "test/objectPut.go"
resp, err := c.Object.Get(context.Background(), name, nil)
if err != nil {
panic(err)
}
bs, _ := io.ReadAll(resp.Body)
resp.Body.Close()
fmt.Printf("%s\\n", string(bs))
// 2. Obtain the object to a local file.
_, err = c.Object.GetToFile(context.Background(), name, "exampleobject", nil)
if err != nil {
panic(err)
}
}
The following are basic examples. For more sample demos, see GitHub address.
Note:
Once an object is deleted, its corresponding data will no longer be accessible.
package main


import (
"context"
"net/http"
"net/url"
"os"

"github.com/tencentyun/cos-go-sdk-v5"
)


func main() {
// Replace examplebucket-1250000000 and COS_REGION with your actual information.
// Bucket name, composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
// COS_REGION can be viewed in the console at https://console.tencentcloud.com/cos5/bucket. For details about regions, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.
u, _ := url.Parse("https://examplebucket-1250000000.cos.COS_REGION.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("SECRETID"), // User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
SecretKey: os.Getenv("SECRETKEY"), // User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to mitigate usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
},
})
name := "test/objectPut.go"
_, err := c.Object.Delete(context.Background(), name)
if err != nil {
panic(err)
}
}

FAQs

You may encounter some common issues during use. For solutions, see Go SDK FAQ.

ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック