九游平台/ 对象存储服务 obs/ sdk参考/ go/ / 设置桶的自定义域名(go sdk)
更新时间:2024-06-21 gmt 08:00

设置桶的自定义域名(go sdk)-九游平台

功能介绍

调用设置桶的自定义域名配置接口,您可以为指定设置桶的自定义域名信息。

接口约束

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

方法定义

func (obsclient obsclient) setbucketcustomdomain(input *setbucketcustomdomaininput, extensions...extensionoptions) (output *basemodel, err error)

请求参数说明

表1 请求参数列表

参数名称

参数类型

是否必选

描述

input

*setbucketcustomdomaininput

必选

参数解释:

设置桶的自定义域名参数,详见setbucketcustomdomaininput

extensions

可选

参数解释:

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

表2 setbucketcustomdomaininput

参数名称

参数类型

是否必选

描述

bucket

string

必选

参数解释

桶名。

约束限制:

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

默认取值

customdomain

string

必选

参数解释

需要设置的自定义域名。

约束限制

必须满足域名规则。

默认取值:

最长256字节。

默认取值

无。

返回结果说明

表3 返回结果列表

参数名称

参数类型

描述

output

*basemodel

参数解释:

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

err

error

参数解释:

接口返回错误信息。

表4 basemodel

参数名称

参数类型

描述

statuscode

int

参数解释:

http状态码。

取值范围:

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

默认取值:

requestid

string

参数解释:

obs服务端返回的请求id。

默认取值:

responseheaders

map[string][]string

参数解释:

http响应头信息。

默认取值:

代码示例

本示例用于设置名为examplebucket桶的自定义域名为www.example.com

 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
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.setbucketcustomdomaininput{}
// 指定存储桶名称
input.bucket="examplebucket"
// 指定桶的自定义域名,此处以www.example.com为例
input.customdomain="www.example.com"
// 设置桶的自定义域名
output,err:=obsclient.setbucketcustomdomain(input)
iferr==nil{
fmt.printf("set bucket(%s)'s customdomain successful!\n",input.bucket)
fmt.printf("requestid:%s\n",output.requestid)
return
}
fmt.printf("set bucket(%s)'s customdomain 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错误码

相关文档

网站地图