
# 获取桶清单
#### 功能说明
OBS使用GET操作来获取指定桶的某个清单配置。要正确执行此操作，需要确保执行者拥有GetBucketInventoryConfiguration权限。桶拥有者默认具有此权限，并且可以将此权限授予其他人。
融合桶场景，主桶支持配置桶清单，备桶不支持配置桶清单。
#### 授权信息
您必须是桶拥有者或拥有获取桶清单的权限，才能调用本接口，建议使用IAM或桶策略进行授权。了解更多OBS授权方式请参见[OBS不同权限控制方式的区别](https://support.huaweicloud.com/perms-cfg-obs/obs_40_0001.html)。
- 如果使用**IAM授权** ，那么请在"角色与策略授权"、"身份策略授权"两种IAM授权方式中选择一种，并配置相应权限：
  - 如果使用**角色与策略授权** （旧版IAM，即IAM v3接口），需具备**obs:bucket:GetBucketInventoryConfiguration** 权限，授权操作请参见[创建IAM自定义策略](https://support.huaweicloud.com/usermanual-iam/iam_01_0605.html)。
  
  - 如果使用**身份策略授权** （新版IAM，即IAM v5接口），如下表所示，需具备**obs:bucket:getBucketInventoryConfiguration** 权限，授权操作请参见[创建IAM自定义身份策略](https://support.huaweicloud.com/usermanual-iam5/iam_01_0917.html)。
    
    | 授权项 Action   | 访问级别 Access Level | 资源类型（\*为必须） Resource Type (\*: required) | [条件键](https://support.huaweicloud.com/api-iam5/iam_02_1124.html) Condition Key                                                                                                                                                                                                                                                                     | [别名](https://support.huaweicloud.com/iam5_faq/iam_01_1103.html)别名 Alias | 依赖的授权项 Dependencies |
    |:---|:---|:---|:---|:---|:---|
    | obs:bucket:getBucketInventoryConfiguration | Read                                            | bucket \*                                                               | - [g:EnterpriseProjectId](https://support.huaweicloud.com/usermanual-iam5/iam_01_0934.html#section2)  - [g:ResourceTag/\<tag-key\>](https://support.huaweicloud.com/usermanual-iam5/iam_01_0934.html#section2)   | -                                                                                                     | -             |
    | obs:bucket:getBucketInventoryConfiguration | Read                                            | -                                                                       | - obs:EpochTime  - obs:SourceIp  - obs:TlsVersion  - obs:CustomDomain                | -                                                                                                     | -             |
       
    
   
- 如果使用**桶策略** 进行授权，需具备**obs:bucket:GetBucketInventoryConfiguration** 权限，具体操作请参见[自定义创建桶策略](https://support.huaweicloud.com/usermanual-obs/obs_03_0123.html)。
 
#### 接口约束
- OBS支持的Region与Endpoint的对应关系，详细信息请参见[地区与终端节点](https://developer.huaweicloud.com/endpoint?OBS)。

- 须知：调用桶清单相关接口前，需在初始化obsClient时调用obs.WithSignature(obs.SignatureObs)指定协议类型。
 
#### 方法定义
func (obsClient ObsClient) GetBucketInventory(input \*GetBucketInventoryInput, extensions ...extensionOptions) (output \*GetBucketInventoryOutput, err error)
#### 请求参数说明
表1GetBucketInventoryInput参数说明 
| 参数     | 类型     | 是否必选 | 说明                                                                                                                                                                      |
|:---|:---|:---|:---|
| Bucket | string | 是    | **参数解释：** 桶的名称。 **取值范围：** 无。                                                 |
| Id     | string | 是    | **参数解释：** 清单配置 ID。 **取值范围：** 只能由"a-z"、"A-Z"、"0-9"、"-"、"_"和".组成，且长度不超过64字节。 |
   
#### 响应参数说明
表2GetBucketInventoryOutput参数说明 
| 参数                     | 类型                     | 说明                                                                                                                                    |
|:---|:---|:---|
| BaseModel              | BaseModel              | **参数解释：** 基础模型，包含 RequestId 等。 **取值范围：** 无。 |
| InventoryConfiguration | InventoryConfiguration | **参数解释：** 清单配置详情。 **取值范围：** 无。               |
   
表3InventoryConfiguration参数说明 
| 字段                     | 类型                        | 说明                                                                                                                                                                                                                                                                                                                                                                                                                               |
|:---|:---|:---|
| Id                     | string                    | **参数解释：** 清单配置 ID。 **取值范围：** 无。                                                                                                                                                                                                                                                                                                               |
| IsEnabled              | bool                      | **参数解释：** 规则是否启用，如果设置为true，则生成清单，反之不生成。 **取值范围：** - true：生成桶清单。  - false：不生成桶清单                                                                                                     |
| Destination            | InventoryDestination      | **参数解释：** 清单的目标配置。 **取值范围：** 无。                                                                                                                                                                                                                                                                                                          |
| Schedule               | InventorySchedule         | **参数解释：** 清单文件的生成周期。 **取值范围：** 无。                                                                                                                                                                                                                                                                                                        |
| Filter                 | \*InventoryFilter         | **参数解释：** 清单过滤器配置，清单只包含符合过滤器规则的对象（只支持按对象名前缀进行过滤），如果没有配置过滤器，则包含所有对象。 **取值范围：** 无。                                                                                                                                                                                                                                                          |
| IncludedObjectVersions | string                    | **参数解释：** 清单文件中包含对象的多版本配置。 **取值范围：** - All：清单会包含对象所有的版本，清单中会增加版本相关的字段：VersionId、IsLatest、和DeleteMarker。  - Current：清单文件中只会列出当前版本信息，不会出现与多版本相关的VersionId、IsLatest、DeleteMarker字段。   |
| OptionalFields         | \*InventoryOptionalFields | **参数解释：** 在此选项中可以添加一些额外的对象元数据字段，生成的清单文件中会包含OptionalFields中配置的字段。 **取值范围：** 无。                                                                                                                                                                                                                                                            |
   
表4InventoryDestination参数说明 
| 参数     | 类型     | 是否必选 | 说明                                                         |
|:---|:---|:---|:---|
| Format | string | 是    | 生成的清单文件的格式，现只支持CSV格式。                                      |
| Bucket | string | 是    | 存放清单文件的目标桶的桶名。                                             |
| Prefix | string | 是    | 生成的清单文件对象名会以此前缀开头，如果不配置前缀，则生成的清单文件对象名默认以BucketInventory开头。 |
   
表5InventorySchedule参数说明 
| 参数        | 类型     | 是否必选 | 说明                                                                                                                      |
|:---|:---|:---|:---|
| Frequency | string | 是    | 清单文件的生成周期，只支持按天和按周生成清单，第一次配置完桶清单，任务会在一个小时内启动，之后每隔一个周期启动一次。 有效值：Daily、Weekly |
   
表6InventoryFilter参数说明 
| 参数     | 类型     | 是否必选 | 说明                          |
|:---|:---|:---|:---|
| Prefix | string | 否    | 前缀过滤条件，清单文件中只生成以此前缀开头的对象列表。 |
   
表7InventoryOptionalFields参数说明 
| 参数     | 类型         | 是否必选 | 说明                                                                                                                                                  |
|:---|:---|:---|:---|
| Fields | \[\]string | 否    | 可选字段列表，有效值 Size、LastModifiedDate、StorageClass、ETag、IsMultipartUploaded、ReplicationStatus、EncryptionStatus |
   
#### 代码示例
本示例用于查询桶examplebucket上ID为inventory-rule-1的桶清单配置。
```
package main
import (
    "fmt"
    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
    "log"
    "os"
)
func main() {
    // 推荐通过环境变量获取AKSK，这里也可以使用其他外部引入方式传入，如果使用硬编码可能会存在泄露风险。
     // 您可以登录访问管理控制台获取访问密钥AK/SK。
    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, err := obs.New(ak, sk, endPoint, obs.WithSignature(obs.SignatureObs), obs.WithSecurityToken(securityToken))
    if err != nil {
        fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
    }
    // 获取清单配置
    input := &obs.GetBucketInventoryInput{
        Bucket: "examplebucket",
        Id:     "inventory-rule-1",
    }
    output, err := obsClient.GetBucketInventory(input)
    if err != nil {
        log.Fatalf("Failed to get bucket inventory: %v", err)
    }
    fmt.Printf("Request ID: %s\n", output.RequestId)
    fmt.Printf("Inventory ID: %s\n", output.Id)
    fmt.Printf("Enabled: %v\n", output.IsEnabled)
    fmt.Printf("Frequency: %s\n", output.Schedule.Frequency)
    fmt.Printf("Format: %s\n", output.Destination.Format)
    fmt.Printf("Destination Bucket: %s\n", output.Destination.Bucket)
    fmt.Printf("Prefix: %s\n", output.Destination.Prefix)
    if output.Filter != nil {
        fmt.Printf("Filter Prefix: %s\n", output.Filter.Prefix)
    }
    if output.OptionalFields != nil {
        fmt.Printf("Optional Fields: %v\n", output.OptionalFields.Fields)
    }
}
```
#### 相关链接
- 更多关于桶清单的示例代码，请参见[Github示例](https://github.com/huaweicloud/huaweicloud-sdk-go-obs/blob/master/examples/object_operations_sample.go)。
- 桶清单接口返回的错误码含义、问题原因及处理措施可参考[OBS错误码](https://support.huaweicloud.com/api-obs/obs_04_0115.html#section1)。
 
