文档首页> 对象存储服务 OBS> Go> 桶相关接口(Go SDK)> 获取桶的多版本状态(Go SDK)
更新时间:2024-02-01 GMT+08:00
分享

获取桶的多版本状态(Go SDK)

功能介绍

OBS支持多版本控制,您可以在一个桶中保留对象的多个版本,使您更方便地检索和还原各个版本,在意外操作或应用程序故障时快速恢复数据。更多多版本相关信息请参见多版本控制

调用获取桶的多版本状态接口,您可以获取指定桶的多版本状态。

接口约束

  • 您必须是桶拥有者或拥有获取桶的多版本状态的权限,才能获取桶的多版本状态。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketVersioning权限,如果使用桶策略则需授予GetBucketVersioning权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略自定义创建桶策略
  • OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点

方法定义

func (obsClient ObsClient) GetBucketVersioning(bucketName string, extensions...extensionOptions) (output *GetBucketVersioningOutput, err error)

请求参数说明

表1 请求参数列表

参数名称

参数类型

是否必选

描述

bucketName

string

必选

参数解释

桶名。

约束限制:

  • 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。
  • 桶命名规则如下:
    • 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。
    • 禁止使用IP地址。
    • 禁止以“-”或“.”开头及结尾。
    • 禁止两个“.”相邻(如:“my..bucket”)。
    • 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。
  • 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。

默认取值:

extensions

extensionOptions

可选

参数解释:

桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions

返回结果说明

表2 返回结果列表

参数名称

参数类型

描述

output

*GetBucketVersioningOutput

参数解释:

接口返回信息,详见GetBucketVersioningOutput

err

error

参数解释:

接口返回错误信息。

表3 GetBucketVersioningOutput

参数名称

参数类型

描述

StatusCode

int

参数解释:

HTTP状态码。

取值范围:

状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码

默认取值:

RequestId

string

参数解释:

OBS服务端返回的请求ID。

默认取值:

ResponseHeaders

map[string][]string

参数解释:

HTTP响应头信息。

默认取值:

Status

VersioningStatusType

参数解释:

桶的多版本状态。

取值范围:

状态取值范围参见VersioningStatusType

默认取值:

表4 VersioningStatusType

常量名

原始值

说明

VersioningStatusEnabled

Enabled

开启多版本。

VersioningStatusSuspended

Suspended

暂停多版本。

代码示例

本示例用于获取名为examplebucket桶的多版本状态

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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.GetBucketVersioning(bucketname)
    if err == nil {
        fmt.Printf("Get bucket(%s)'s versioning status successful!\n", bucketname)
        fmt.Printf("RequestId:%s\n", output.RequestId)
        fmt.Printf("VersioningStatus:%s\n", output.Status)
        return
    }
    fmt.Printf("Get bucket(%s)'s versioning status 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)
    }
}

相关链接

分享:

    相关文档

    相关产品