
# 获取桶元数据(Go SDK)
#### 功能介绍
调用获取桶元数据接口，可获取指定桶的相关信息，包括指定桶的存储类型、区域位置、跨域资源共享（CORS）规则、冗余策略等信息。
#### 接口约束
- 您必须是桶拥有者或拥有获取桶元数据的权限，才能获取桶元数据。建议使用IAM或桶策略进行授权，如果使用IAM则需授予obs:bucket:HeadBucket权限，如果使用桶策略则需授予HeadBucket权限。相关授权方式介绍可参见[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) GetBucketMetadata(input \*[GetBucketMetadataInput](https://support.huaweicloud.com/sdk-go-devg-obs/obs_33_0409.html), extensions...extensionOptions) (output \*[GetBucketMetadataOutput], err error)
#### 请求参数说明
表1请求参数列表 
| **参数名称**   | **参数类型**                                                                             | **是否必选** | **描述**                                                                                                                                                             |
|:---|:---|:---|:---|
| input      | \*[GetBucketMetadataInput]                              | 必选       | **参数解释** **：** 获取桶元数据请求参数，详情参考[GetBucketMetadataInput]。                                                 |
| 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)。 |
   
 表2GetBucketMetadataInput 
| **参数名称** | **参数类型** | **是否必选** | **描述**                                                                                                                                                                                                                                                                                            |
|:---|:---|:---|:---|
| Bucket   | string   | 必选       | **参数解释** **：** 桶名。 **约束限制：** - 同一用户在同一个区域多次创建同名桶不会报错，创建的桶属性以第一次请求为准。   **默认取值：** 无 |
   
#### 返回结果说明
表3返回结果列表 
| **参数名称** | **参数类型**                                                       | **描述**                                                                                                               |
|:---|:---|:---|
| output   | \*[GetBucketMetadataOutput] | **参数解释** **：** 接口返回信息，详情参考[GetBucketMetadataOutput]。 |
| err      | error                                                          | **参数解释** **：** 接口返回错误信息。                                                               |
   
 表4GetBucketMetadataOutput 
| **参数名称**        | **参数类型**                                                                             | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|
| 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响应头信息。 **默认取值：** 无                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| Location        | string                                                                               | **参数解释** **：** 桶所在的区域。 **约束限制：** 该参数定义了桶将会被创建在哪个区域，如果使用的终端节点是obs.myhuaweicloud.com，可以不携带此参数；如果使用的终端节点不是obs.myhuaweicloud.com，则必须携带此参数。 **取值范围：** 当前有效的OBS区域和终端节点的更多信息，请参考[地区和终端节点](https://developer.huaweicloud.com/endpoint?OBS)。终端节点即调用API的请求地址，不同服务不同区域的终端节点不同，您可以向企业管理员获取区域和终端节点信息。 **默认取值：** 终端节点为obs.myhuaweicloud.com且用户未设定区域时，默认为华北-北京一（cn-north-1）。                                                                                                                                                                                                                                                   |
| Version         | string                                                                               | **参数解释：** 桶所在的OBS服务版本号。 **取值范围：** - "3.0"表示最新版本的桶。  - "--"表示老版本的桶。   **默认取值：** 无                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| StorageClass    | [StorageClassType](https://support.huaweicloud.com/sdk-go-devg-obs/obs_33_0409.html) | **参数解释** **：** 桶的存储类型，详情参见[StorageClassType]。 **取值范围：** 当桶存储类型是标准存储时，该值为空。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| AllowOrigin     | string                                                                               | **参数解释：** 如果请求中的Origin满足桶的CORS规则，则返回CORS规则中的AllowedOrigin。AllowedOrigin指定允许的跨域请求的来源，即允许来自该域名下的请求访问该对象。 **约束限制：** 表示域名的字符串，每个匹配规则允许使用最多一个"\*"通配符。例如：https://\*.vbs.example.com。 **默认取值：** 无                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| AllowHeader     | string                                                                               | **参数解释：** 如果请求的RequestHeader满足桶的CORS规则，则返回CORS规则中的AllowedHeader。AllowedHeader是指允许的跨域请求的头域。只有匹配上允许的头域中的配置，才被视为是合法的CORS请求。 **约束限制：** 最多可填写一个"\*"通配符，不支持\&、:、\<、空格以及中文字符。 **默认取值：** 无                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| AllowMethod     | string                                                                               | **参数解释：** 桶CORS规则中的AllowedMethod。AllowedMethod指允许的跨域请求HTTP方法，即桶和对象的几种操作类型。 **取值范围：** 支持以下HTTP方法： - GET  - PUT  - HEAD  - POST  - DELETE   **默认取值：** 无 |
| ExposeHeader    | string                                                                               | **参数解释：** 桶CORS规则中的ExposeHeader。ExposeHeader是指CORS规则允许响应中可返回的附加头域，给客户端提供额外的信息。默认情况下浏览器只能访问以下头域：Content-Length、Content-Type，如果需要访问其他头域，需要在附加头域中配置。 **约束限制：** 不支持\*、\&、:、\<、空格以及中文字符。 **默认取值：** 无                                                                                                                                                                                                                                                                                                                                                                                                                        |
| MaxAgeSeconds   | int                                                                                  | **参数解释：** 桶CORS规则中的MaxAgeSeconds。MaxAgeSeconds指请求来源的客户端可以对跨域请求返回结果的缓存时间。 **约束限制：** 每个CORSRule可以包含至多一个MaxAgeSeconds。 **取值范围：** 0\~（2^31^-1），单位：秒。 **默认取值：** 100，单位：秒。                                                                                                                                                                                                                                                                                                                                                      |
| Epid            | string                                                                               | **参数解释** **：** 创桶时可指定的企业项目ID，开通企业项目的用户可以从企业项目服务获取。 **约束限制：** Epid格式为uuid，未开通企业项目的用户可以不带该头域。 示例：9892d768-2d13-450f-aac7-ed0e44c2585f **取值范围：** 如何获取企业项目ID参见[如何获取企业项目ID](https://support.huaweicloud.com/usermanual-em/zh-cn_topic_0126101490.html) 。 **默认取值：** 无                                                                                                                                                                                                                                                                                                                  |
   
 表5StorageClassType 
| **常量名**                 | **原始值**      | **说明**                                                                                                             |
|:---|:---|:---|
| StorageClassStandard    | STANDARD     | 标准存储。 标准存储拥有低访问时延和较高的吞吐量，适用于有大量热点对象（平均一个月多次）或小对象（\<1MB），且需要频繁访问数据的业务场景。 |
| StorageClassWarm        | WARM         | 低频访问存储。 低频访问存储适用于不频繁访问（平均一年少于12次）但在需要时也要求能够快速访问数据的业务场景。                 |
| StorageClassCold        | COLD         | 归档存储。 归档存储适用于很少访问（平均一年访问一次）数据的业务场景。                                     |
| StorageClassDeepArchive | DEEP_ARCHIVE | 深度归档存储 适用于长期不访问（平均几年访问一次）数据的业务场景。                                        |
   
#### 代码示例
本示例用于获取名为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())
    }
    input := &obs.GetBucketMetadataInput{}
    // 指定存储桶名称
    input.Bucket = "examplebucket"
    // 获取桶元数据信息
    output, err := obsClient.GetBucketMetadata(input)
    if err == nil {
        fmt.Printf("Get bucket(%s)'s metadata successful!\n", input.Bucket)
        fmt.Printf("RequestId:%s\n", output.RequestId)
        fmt.Printf("StorageClass:%s\n", output.StorageClass)
        fmt.Printf("Location:%s\n", output.Location)
        fmt.Printf("AZRedundancy:%s\n", output.AZRedundancy)
        fmt.Printf("Epid:%s\n", output.Epid)
        return
    }
    fmt.Printf("Get bucket(%s)'s metadata fail!\n", input.Bucket)
    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_0023.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)。
 
