Bucket Extraction

Last updated: 2023-09-13 12:43:44

Feature Overview

This document provides an overview of APIs and SDK code samples for checking a bucket.
API
Operation
Description
Checking a bucket and its permission
Checks whether a bucket exists and you have permission to access it

Checking a bucket and its permission

Note

This API is used to check whether a bucket exists and whether you have permission to access it.

Method prototype

func (s *BucketService) Head(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 must be entered, and 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 on 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
// The 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, and follow the principle of least privilege to minimize 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 Access Management 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 and follow 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.Head(context.Background())
if err != nil {
panic(err)
}
}