九游平台/ 对象存储服务 obs/ sdk参考/ go/ / 获取桶元数据(go sdk)
更新时间:2024-08-26 gmt 08:00

获取桶元数据(go sdk)-九游平台

功能介绍

调用获取桶元数据接口,可获取指定桶的相关信息,包括指定桶的存储类型、区域位置、跨域资源共享(cors)规则、冗余策略等信息。

接口约束

  • 您必须是桶拥有者或拥有获取桶元数据的权限,才能获取桶元数据。建议使用iam或桶策略进行授权,如果使用iam则需授予obs:bucket:headbucket权限,如果使用桶策略则需授予headbucket权限。相关授权方式介绍可参见obs权限控制概述,配置方式详见、。
  • obs支持的region与endpoint的对应关系,详细信息请参见。

方法定义

func (obsclient obsclient) getbucketmetadata(input *getbucketmetadatainput, extensions...extensionoptions) (output *getbucketmetadataoutput, err error)

请求参数说明

表1 请求参数列表

参数名称

参数类型

是否必选

描述

input

*getbucketmetadatainput

必选

参数解释

获取桶元数据请求参数,详情参考getbucketmetadatainput

extensions

可选

参数解释:

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

表2 getbucketmetadatainput

参数名称

参数类型

是否必选

描述

bucket

string

必选

参数解释

桶名。

约束限制:

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

默认取值:

返回结果说明

表3 返回结果列表

参数名称

参数类型

描述

output

*getbucketmetadataoutput

参数解释

接口返回信息,详情参考getbucketmetadataoutput

err

error

参数解释

接口返回错误信息。

表4 getbucketmetadataoutput

参数名称

参数类型

描述

statuscode

int

参数解释:

http状态码。

取值范围:

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

默认取值:

requestid

string

参数解释:

obs服务端返回的请求id。

默认取值:

responseheaders

map[string][]string

参数解释:

http响应头信息。

默认取值:

location

string

参数解释

桶所在的区域。

约束限制:

该参数定义了桶将会被创建在哪个区域,如果使用的终端节点是obs.myhuaweicloud.com,可以不携带此参数;如果使用的终端节点不是obs.myhuaweicloud.com,则必须携带此参数。

取值范围:

当前有效的obs区域和终端节点的更多信息,请参考。终端节点即调用api的请求地址,不同服务不同区域的终端节点不同,您可以向企业管理员获取区域和终端节点信息。

默认取值:

终端节点为obs.myhuaweicloud.com且用户未设定区域时,默认为华北-北京一(cn-north-1)。

version

string

参数解释:

桶所在的obs服务版本号。

取值范围:

  • “3.0”表示最新版本的桶。
  • “--”表示老版本的桶。

默认取值:

storageclass

storageclasstype

参数解释

桶的存储类型,详情参见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~(231-1),单位:秒。

默认取值:

100,单位:秒。

epid

string

参数解释

创桶时可指定的企业项目id,开通企业项目的用户可以从企业项目服务获取。

约束限制:

epid格式为uuid,未开通企业项目的用户可以不带该头域。

示例:9892d768-2d13-450f-aac7-ed0e44c2585f

取值范围:

如何获取企业项目id参见如何获取企业项目id

默认取值:

表5 storageclasstype

常量名

原始值

说明

storageclassstandard

standard

标准存储。

标准存储拥有低访问时延和较高的吞吐量,适用于有大量热点对象(平均一个月多次)或小对象(<1mb),且需要频繁访问数据的业务场景。

storageclasswarm

warm

低频访问存储。

低频访问存储适用于不频繁访问(平均一年少于12次)但在需要时也要求能够快速访问数据的业务场景。

storageclasscold

cold

归档存储。

归档存储适用于很少访问(平均一年访问一次)数据的业务场景。

storageclassdeeparchive

deep_archive

深度归档存储(受限公测)

适用于长期不访问(平均几年访问一次)数据的业务场景。

代码示例

本示例用于获取名为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
41
42
43
44
packagemain
import(
"fmt"
"os"
obs"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
funcmain(){
//推荐通过环境变量获取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)*/)
iferr!=nil{
fmt.printf("create obsclient error, errmsg: %s",err.error())
}
input:=&obs.getbucketmetadatainput{}
// 指定存储桶名称
input.bucket="examplebucket"
// 获取桶元数据信息
output,err:=obsclient.getbucketmetadata(input)
iferr==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)
ifobserror,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说明,请参见获取桶元数据
  • 更多关于获取桶元数据的示例代码,请参见。
  • 获取桶元数据过程中返回的错误码含义、问题原因及处理措施可参考obs错误码
  • 桶和对象相关常见问题请参见桶和对象相关常见问题

相关文档

网站地图