-
代码示例 本示例用于创建名为examplebucket的桶,并设置所在区域在华北-北京四(cn-north-4),桶ACL是私有桶,存储类型是低频访问存储,多AZ方式存储。 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
45
46
47
48
49
50 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.CreateBucketInput{}
// 指定存储桶名称
input.Bucket = "examplebucket"
// 指定存储桶所在区域,此处以“cn-north-4”为例,必须跟传入的Endpoint中Region保持一致。
input.Location = "cn-north-4"
// 指定存储桶ACL,此处以obs.AclPrivate为例。
input.ACL = obs.AclPrivate
// 指定存储桶的存储类型,此处以obs.StorageClassWarm为例。如果未指定该参数,则创建的桶为标准存储类型。
input.StorageClass = obs.StorageClassWarm
// 指定存储桶的AZ类型,此处以“3AZ”为例。不携带时默认为单AZ,如果对应region不支持多AZ存储,则该桶的存储类型仍为单AZ。
input.AvailableZone = "3az"
// 创建桶
output, err := obsClient.CreateBucket(input)
if err == nil {
fmt.Printf("Create bucket:%s successful!\n", input.Bucket)
fmt.Printf("RequestId:%s\n", output.RequestId)
return
}
fmt.Printf("Create bucket:%s 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)
}
}
-
返回结果说明 表5 返回结果列表 参数名称 参数类型 描述 output *BaseModel 参数解释: 接口返回信息,详见BaseModel。 err error 参数解释: 接口返回错误信息。 表6 BaseModel 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无
-
接口约束 您必须拥有obs:bucket:CreateBucket权限,才能创建桶。建议使用
IAM 进行授权,配置方式详见使用IAM自定义策略。 OBS支持的region以及region与endPoint的对应关系,详细信息请参见地区与终端节点。 创建桶时,如果初始化客户端使用的终端节点(endPoint)为“obs.myhuaweicloud.com”,则可以不指定桶所在区域(location),系统会自动在华北-北京一(cn-north-1)创建桶;如果初始化客户端使用的终端节点(endPoint)不是obs.myhuaweicloud.com,则必须指定桶所在区域(location),且指定的区域必须与终端节点(endPoint)区域一致,否则会返回状态码400。 比如初始化时使用的终端节点endPoint是obs.cn-north-4. myhuaweicloud.com,那么在创建桶的时候必须指定Location:cn-north-4 才会创建成功,否则会返回状态码400。 同一账号下,可以创建多个存桶,数量上限是100个(不区分地域),存储桶中的对象数量和大小没有限制。 新创建桶的桶名在OBS中必须是唯一的。如果是同一个用户重复创建同一区域的同名桶时返回HTTP状态码200。除此以外的其他场景重复创建同名桶返回HTTP状态码409,表明桶已存在。 用户删除桶后,需要等待30分钟才能创建同名桶和并行文件系统。 并不是所有区域都支持创建多AZ桶,您可以在产品价格详情页面,查询指定区域是否支持多AZ。
-
代码示例 本示例用于获取名为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 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.WithSignature(obs.SignatureObs)/*, obs.WithSecurityToken(securityToken)*/)
if err != nil {
fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
}
// 指定桶名称
bucketname := "examplebucket"
// 获取桶的生命周期规则
output, err := obsClient.GetBucketLifecycleConfiguration(bucketname)
if err == nil {
fmt.Printf("Get LifecycleConfiguration successful with bucket:%s!\n", bucketname)
fmt.Printf("RequestId:%s\n", output.RequestId)
return
}
fmt.Printf("Get LifecycleConfiguration fail with bucket:%s!\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)
}
}
-
请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 bucketName string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
-
接口约束 您必须是桶拥有者或拥有获取桶的生命周期配置的权限,才能获取桶的生命周配置。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetLifecycleConfiguration权限,如果使用桶策略则需授予GetLifecycleConfiguration权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
-
代码示例 本示例用于配置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
45 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.SetBucketTaggingInput{}
// 指定存储桶名称
input.Bucket = "bucketname"
// 指定桶的标签
input.Tags = []obs.Tag{
{Key: "key0", Value: "value0"},
{Key: "key1", Value: "value1"},
}
// 设置桶标签
output, err := obsClient.SetBucketTagging(input)
if err == nil {
fmt.Printf("Set bucket(%s)'s tag configuration successful!\n", input.Bucket)
fmt.Printf("RequestId:%s\n", output.RequestId)
return
}
fmt.Printf("Set bucket(%s)'s tag configuration 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)
}
}
-
返回结果说明 表4 返回结果列表 参数名称 参数类型 描述 output *BaseModel type BaseModel struct 参数解释: 接口返回信息,详见BaseModel。 err error 参数解释: 接口返回错误信息。 表5 BaseModel 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无