功能说明
列出调用方主账号在指定地域下的所有高性能桶(GET Service,即 ListBuckets)。查询存储桶列表请求发往 service 域名,地域由 service 域名决定。
方法原型
func (s *ServiceService) Get(ctx context.Context, opt ...*ServiceGetOptions) (*ServiceGetResult, *Response, error)
参数说明
type ServiceGetOptions struct {MaxKeys int64Marker string}
参数名称 | 参数描述 | 类型 | 是否必选 |
ctx | 请求上下文,用于控制超时与取消 | context.Context | 是 |
opt | 查询选项,可省略或传 nil | *ServiceGetOptions | 否 |
MaxKeys | 单次返回的最大存储桶数量,取值范围 1~2000,默认 2000 | int64 | 否 |
Marker | 分页游标,取值为上一次响应的 NextMarker;首次请求无需携带 | string | 否 |
返回结果说明
type ServiceGetResult struct {Owner *OwnerBuckets []BucketMarker stringNextMarker stringIsTruncated bool}type Bucket struct {Name stringRegion stringCreationDate string}
参数名称 | 参数描述 | 类型 |
Owner | 存储桶持有者信息 | *Owner |
Buckets | 存储桶列表 | []Bucket |
Marker | 本次请求使用的分页游标 | string |
NextMarker | 下一页分页游标,仅当 IsTruncated 为 true 时非空 | string |
IsTruncated | 是否仍有未列举完的存储桶,true 表示存在下一页 | bool |
Bucket.Name | 存储桶完整名称,格式为 <BucketName>-x--<APPID> | string |
Bucket.Region | 存储桶所在地域(对应 XML 中的 Location) | string |
Bucket.CreationDate | 存储桶创建时间,ISO 8601 格式 | string |
使用案例
查询存储桶列表请求发往 service 域名。
package mainimport ("context""fmt""log""net/http""net/url""os""time""github.com/tencentyun/cos-go-sdk-v5")func main() {// 请求发往 service 域名。su, _ := url.Parse("https://service.cosrapid.ap-guangzhou.myqcloud.com")b := &cos.BaseURL{ServiceURL: su}base := &cos.AuthorizationTransport{SecretID: os.Getenv("SECRETID"),SecretKey: os.Getenv("SECRETKEY"),}c := cos.NewClient(b, &http.Client{Transport: cos.NewRapidCredentialTransport(base),Timeout: 30 * time.Second,})ctx := context.Background()// 分页列举该地域下的全部存储桶。marker := ""for {res, _, err := c.Service.Get(ctx, &cos.ServiceGetOptions{MaxKeys: 2000,Marker: marker,})if err != nil {log.Fatalf("ListBuckets failed: %v", err)}for _, bucket := range res.Buckets {fmt.Printf("bucket: %s, region: %s, created: %s\\n",bucket.Name, bucket.Region, bucket.CreationDate)}if !res.IsTruncated {break}marker = res.NextMarker}}
注意事项
查询存储桶列表请求发往 service 域名
https://service.cosrapid.<Region>.myqcloud.com,该域名不带存储桶名前缀。本接口仅返回 service 域名对应
<Region> 下的存储桶;如需查看其他地域的存储桶,请更换 service 域名中的地域后分别调用。当
IsTruncated 为 true 时,将响应中的 NextMarker 作为下次请求的 Marker 传入,直到 IsTruncated 为 false。本接口只能列出签名密钥所属账号的存储桶。