
# 获取桶存量信息(Go SDK)
#### 功能介绍
调用获取桶存量信息接口，可查询指定桶内的对象个数，以及对象占用空间的大小。
![](https://support.huaweicloud.com/sdk-go-devg-obs/public_sys-resources/note_3.0-zh-cn.png)
由于OBS桶存量是后台统计，因此存量会有一定的时延，不能实时更新，因此不建议对存量做实时校验。
#### 接口约束
- 您必须是桶拥有者或拥有获取桶存量信息的权限，才能获取桶存量信息。建议使用IAM或桶策略进行授权，如果使用IAM则需授予obs:bucket:GetBucketStorage权限，如果使用桶策略则需授予GetBucketStorage权限。相关授权方式介绍可参见[OBS权限控制概述](https://support.huaweicloud.com/perms-cfg-obs/obs_40_0001.html)，配置方式详见[使用IAM自定义策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0121.html)、[自定义创建桶策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0123.html)。
- OBS支持的Region与Endpoint的对应关系，详细信息请参见[地区与终端节点](https://developer.huaweicloud.com/endpoint?OBS)。
 
#### 方法定义
**func** (obsClient ObsClient) GetBucketStorageInfo([bucketName](https://support.huaweicloud.com/sdk-go-devg-obs/obs_33_0411.html) string, extensions...extensionOptions) (output \*[GetBucketStorageInfoOutput], err error)
#### 请求参数说明
表1请求参数列表 
| **参数名称**   | **参数类型**                                                                             | **是否必选** | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
|:---|:---|:---|:---|
| bucketName | string                                                                               | 必选       | **参数解释** **：** 桶名。 **约束限制：** - 桶命名规则如下： - 需全局唯一，不能与已有的任何桶名称重复，包括其他用户创建的桶。用户删除桶后，立即创建同名桶或并行文件系统会创建失败，需要等待30分钟才能创建。  - 长度范围为3到63个字符，仅支持小写字母、数字、中划线（-）、英文句号（.）。  - 禁止两个英文句号（.）相邻，禁止英文句号（.）和中划线（-）相邻，禁止以英文句号（.）和中划线（-）开头或结尾。  - 禁止使用IP地址。    - 同一用户在同一个区域多次创建同名桶不会报错，创建的桶属性以第一次请求为准。   **默认取值**： 无 |
| extensions | [extensionOptions](https://support.huaweicloud.com/sdk-go-devg-obs/obs_33_0105.html) | 可选       | **参数解释：** 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头，详情参考[extensionOptions](https://support.huaweicloud.com/sdk-go-devg-obs/obs_33_0105.html)。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
   
#### 返回结果说明
表2返回结果列表 
| **参数名称** | **参数类型**                                                    | **描述**                                                                                                         |
|:---|:---|:---|
| output   | \*[GetBucketStorageInfoOutput] | **参数解释：** 接口返回信息，详情参考[GetBucketStorageInfoOutput]。 |
| err      | error                                                       | **参数解释：** 接口返回错误信息。                                                             |
   
 表3GetBucketStorageInfoOutput 
| **参数名称**        | **参数类型**                | **描述**                                                                                                                                                                                                                                                                                                                                                                               |
|:---|:---|:---|
| StatusCode      | int                     | **参数解释：** HTTP状态码。 **取值范围：** 状态码是一组从2xx（成功）到4xx或5xx（错误）的数字代码，状态码表示了请求响应的状态。完整的状态码列表请参见[状态码](https://support.huaweicloud.com/api-obs/obs_04_0114.html)。 **默认取值：** 无 |
| RequestId       | string                  | **参数解释：** OBS服务端返回的请求ID。 **默认取值：** 无                                                                                                                                                                                                                                                    |
| ResponseHeaders | map\[string\]\[\]string | **参数解释：** HTTP响应头信息。 **默认取值：** 无                                                                                                                                                                                                                                                           |
| Size            | int64                   | **参数解释：** 桶中对象占用的空间大小。 **取值范围：** 0\~（2^63^-1），单位：字节。                                                                                                                                                                                                                                   |
| ObjectNumber    | int                     | **参数解释：** 桶内对象个数。 **取值范围：** 0\~（2^63^-1），单位：个。 **默认取值：** 无                                                                                                                                                                     |
   
#### 代码示例
本示例用于获取名为examplebucket桶的存量信息，包括桶对象个数及对象占用空间。
```
package main
import (
    "fmt"
    "os"
    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
    //推荐通过环境变量获取AKSK，这里也可以使用其他外部引入方式传入，如果使用硬编码可能会存在泄露风险。
    //您可以登录访问管理控制台获取访问密钥AK/SK，获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html。
    ak := os.Getenv("AccessKeyID")
    sk := os.Getenv("SecretAccessKey")
    // 【可选】如果使用临时AK/SK和SecurityToken访问OBS，同样建议您尽量避免使用硬编码，以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK，也可以使用其他外部引入方式传入。
    // securityToken := os.Getenv("SecurityToken")
    // endpoint填写Bucket对应的Endpoint，这里以华北-北京四为例，其他地区请按实际情况填写。
    endPoint := "https://obs.cn-north-4.myhuaweicloud.com"
    // 创建obsClient实例
    // 如果使用临时AKSK和SecurityToken访问OBS，需要在创建实例时通过obs.WithSecurityToken方法指定securityToken值。
    obsClient, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)
    if err != nil {
        fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
    }
    // 指定存储桶名称
    bucketname := "examplebucket"
    // 获取桶存量信息
    output, err := obsClient.GetBucketStorageInfo(bucketname)
    if err == nil {
        fmt.Printf("Get bucket(%s)'s storage-info successful!\n", bucketname)
        fmt.Printf("RequestId:%s\n", output.RequestId)
        fmt.Printf("Size:%d, ObjectNumber:%d\n", output.Size, output.ObjectNumber)
        return
    }
    fmt.Printf("Get bucket(%s)'s storage-info fail!\n", bucketname)
    if obsError, ok := err.(obs.ObsError); ok {
        fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.")
        fmt.Println(obsError.Error())
    } else {
        fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.")
        fmt.Println(err)
    }
}
```
#### 相关链接
- 关于获取桶存量信息的API说明，请参见[获取桶存量信息](https://support.huaweicloud.com/api-obs/obs_04_0054.html)。
- 更多关于获取桶存量信息的示例代码，请参见[Github示例](https://github.com/huaweicloud/huaweicloud-sdk-go-obs/blob/master/examples/bucket_operations_sample.go)。
- 获取桶存量信息过程中返回的错误码含义、问题原因及处理措施可参考[OBS错误码](https://support.huaweicloud.com/api-obs/obs_04_0115.html#section1)。
- 桶和对象相关常见问题请参见[桶和对象相关常见问题](https://support.huaweicloud.com/obs_faq/obs_faq_1200.html)。
 
