
# 设置桶存储类型(Go SDK)
#### 功能说明
OBS提供了这些存储类型：标准存储、低频访问存储、归档存储、深度归档存储，从而满足客户业务对存储性能、成本的不同诉求，详情可参见OBS[存储类型](https://support.huaweicloud.com/usermanual-obs/obs_41_0006.html)。
调用设置桶存储类型接口，可设置指定桶的存储类型。设置了桶的默认存储类型之后，如果上传对象、复制对象和初始化多段上传任务时未指定对象的存储类型，则该对象的存储类型默认与桶的存储类型保持一致。
#### 接口约束
- 您必须是桶拥有者或拥有设置桶存储类型的权限，才能设置桶存储类型。建议使用IAM或桶策略进行授权，如果使用IAM则需授予obs:PutBucketStoragePolicy权限，如果使用桶策略则需授予PutBucketStoragePolicy权限。相关授权方式介绍可参见[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) SetBucketStoragePolicy(input \*[SetBucketStoragePolicyInput], extensions...extensionOptions) (output \*[BaseModel], err error)
#### 请求参数说明
表1请求参数列表 
| **参数名称**   | **参数类型**                                                                             | **是否必选** | **描述**                                                                                                                                                             |
|:---|:---|:---|:---|
| input      | \*[SetBucketStoragePolicyInput]                         | 必选       | **参数解释：** 设置桶存储类型请求参数，详见[SetBucketStoragePolicyInput]。                                                 |
| 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)。 |
   
 表2SetBucketStoragePolicyInput 
| **参数名称**     | **参数类型**                                             | **是否必选** | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
|:---|:---|:---|:---|
| Bucket       | string                                               | 必选       | **参数解释** **：** 桶名。 **约束限制：** - 桶命名规则如下： - 需全局唯一，不能与已有的任何桶名称重复，包括其他用户创建的桶。用户删除桶后，立即创建同名桶或并行文件系统会创建失败，需要等待30分钟才能创建。  - 长度范围为3到63个字符，仅支持小写字母、数字、中划线（-）、英文句号（.）。  - 禁止两个英文句号（.）相邻，禁止英文句号（.）和中划线（-）相邻，禁止以英文句号（.）和中划线（-）开头或结尾。  - 禁止使用IP地址。    - 同一用户在同一个区域多次创建同名桶不会报错，创建的桶属性以第一次请求为准。   **取值范围：** 长度为3～63个字符。 **默认取值：** 无 |
| StorageClass | [StorageClassType] | 必选       | **参数解释：** 桶的存储类型。 **取值范围：** 存储类型取值详见[StorageClassType]。 **默认取值：** 无                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
   
 表3StorageClassType 
| **常量名**                 | **原始值**      | **说明**                                                                                                             |
|:---|:---|:---|
| StorageClassStandard    | STANDARD     | 标准存储。 标准存储拥有低访问时延和较高的吞吐量，适用于有大量热点对象（平均一个月多次）或小对象（\<1MB），且需要频繁访问数据的业务场景。 |
| StorageClassWarm        | WARM         | 低频访问存储。 低频访问存储适用于不频繁访问（平均一年少于12次）但在需要时也要求能够快速访问数据的业务场景。                 |
| StorageClassCold        | COLD         | 归档存储。 归档存储适用于很少访问（平均一年访问一次）数据的业务场景。                                     |
| StorageClassDeepArchive | DEEP_ARCHIVE | 深度归档存储 适用于长期不访问（平均几年访问一次）数据的业务场景。                                        |
   
#### 返回结果说明
表4返回结果列表 
| **参数名称** | **参数类型**                                        | **描述**                                                                                         |
|:---|:---|:---|
| output   | \*[BaseModel] | **参数解释：** 接口返回信息，详见[BaseModel]。 |
| err      | error                                           | **参数解释：** 接口返回错误信息。                                              |
   
 表5BaseModel 
| **参数名称**        | **参数类型**                | **描述**                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
|:---|:---|:---|
| 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响应头信息。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                                                                                                      |
   
#### 代码示例
本示例用于设置名为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.SetBucketStoragePolicyInput{}
    // 指定存储桶名称
    input.Bucket = "bucketname"
    // 指定桶的存储类别，此处以obs.StorageClassWarm为例
    input.StorageClass = obs.StorageClassWarm
    // 设置桶存储类别
    output, err := obsClient.SetBucketStoragePolicy(input)
    if err == nil {
        fmt.Printf("Set bucket(%s)'s storage-class successful!\n", input.Bucket)
        fmt.Printf("Set bucket storage-class successful!\n")
        fmt.Printf("RequestId:%s\n", output.RequestId)
        return
    }
    fmt.Printf("Set bucket(%s)'s storage-class 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_0044.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)。
 
