华为云用户手册

  • 代码示例 本示例用于获取名为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.GetBucketLocation(bucketname) if err == nil { fmt.Printf("Get bucket(%s)'s location successful!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) fmt.Printf("Location:%s\n", output.Location) return } fmt.Printf("Get bucket(%s)'s location 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) } }
  • 返回结果 表2 返回结果列表 参数名称 参数类型 描述 output *BaseModel 参数解释: 接口返回信息,详情参考BaseModel。 err error 参数解释: 接口返回错误信息。 表3 BaseModel 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无
  • 代码示例 本示例用于删除名为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.WithSecurityToken(securityToken)*/) if err != nil { fmt.Printf("Create obsClient error, errMsg: %s", err.Error()) } // 指定存储桶名称 bucketname := "examplebucket" // 删除桶 output, err := obsClient.DeleteBucket(bucketname) if err == nil { fmt.Printf("Delete bucket:%s successful!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Delete bucket:%s 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) } }
  • 接口约束 待删除的桶必须为空,桶为空包含两方面含义: 桶内没有任何对象,没有对象的任何历史版本,没有对象的删除标记(删除标记也视作一个历史版本)。 桶内没有任何未合并的多段上传任务,即桶内不存在碎片。 您必须是桶拥有者或拥有删除桶的权限,才能删除桶。建议使用 IAM 或桶策略进行授权,如果使用IAM则需授予obs:bucket:DeleteBucket权限,如果使用桶策略则需授予DeleteBucket权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 bucketName string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 代码示例 本示例用于设置名为examplebucket桶的ACL权限控制为私有权限。 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 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.SetBucketAclInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定桶的Acl,此处以私有权限为例 input.ACL = obs.AclPrivate // 设置桶ACL output, err := obsClient.SetBucketAcl(input) if err == nil { fmt.Printf("Set bucket(%s)'s acl successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Set bucket(%s)'s acl 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) } }
  • 接口约束 单个桶最多支持100条ACL策略。 您必须是桶拥有者或拥有设置桶ACL的权限,才能设置桶ACL。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:PutBucketAcl权限,如果使用桶策略则需授予PutBucketAcl权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 功能说明 OBS支持对桶操作进行权限控制,您可以为桶设置访问策略,指定某一个用户对某一个桶是否有权行使某一项指定操作。OBS权限控制的方式有IAM、桶策略和ACL三种,ACL按照粒度又分为桶ACL和对象ACL,本节将对桶ACL接口进行详细介绍,更多权限相关内容可参见《 对象存储服务 权限配置指南》的OBS权限控制概述章节。 桶ACL是跨帐号场景的权限,设置授权的对象不是当前帐号,也不是当前帐号下的IAM用户,而是另一个华为云帐号及其帐号下的IAM用户;授权的范围是以桶为粒度的,一条ACL策略为一个桶设置策略,因此设置ACL策略时您必须明确指定桶名;桶ACL授予的权限包括桶的访问权限和桶ACL的访问权限两个方面,桶的访问权限包括对桶及桶内对象的查看和编辑权限,桶ACL的访问权限包括对桶ACL策略的查看和编辑权限,详情可参见ACL权限控制方式介绍。 调用设置桶ACL接口,您可以修改指定桶的ACL策略。
  • 返回结果说明 表9 返回结果列表 参数名称 参数类型 描述 output *BaseModel 参数解释: 接口返回信息,详见BaseModel。 err error 参数解释: 接口返回错误信息。 表10 BaseModel 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无
  • 功能介绍 OBS支持对桶操作进行权限控制,您可以为桶设置访问策略,指定某一个用户对某一个桶是否有权行使某一项指定操作。OBS权限控制的方式有IAM、桶策略和ACL三种,本节将对桶策略接口进行详细介绍,更多权限相关内容可参见《对象存储服务权限配置指南》的OBS权限控制概述章节。 桶策略是作用于所配置的OBS桶及桶内对象的,您可以通过桶策略可为IAM用户或其他帐号授权桶及桶内对象的操作权限。当不同的桶对于不同的IAM用户有不同的访问控制需求时,需使用桶策略分别授权IAM用户不同的权限。 调用获取桶策略接口,您可获取指定桶的桶策略。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 bucketName string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 代码示例 本示例用于获取桶名为bucketname的桶策略信息。 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.GetBucketPolicy(bucketname) if err == nil { fmt.Printf("Get policy successful with bucket:%s!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) fmt.Printf("Policy:%s\n", output.Policy) return } fmt.Printf("Get policy 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) } }
  • 接口约束 以下两种场景无法使用此接口获取桶策略,系统将返回“404 NoSuchBucketPolicy”的错误: 指定桶的策略不存在 指定桶的标准桶策略为私有且未设置高级桶策略 您必须是桶拥有者或拥有获取桶策略的权限,才能获取桶策略。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketPolicy权限,如果使用桶策略则需授予GetBucketPolicy权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 bucketName string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 接口约束 桶配额值必须为非负整数,单位为字节,支持的最大值为263-1。 桶的拥有者的状态是欠费冻结状态时不可以查询桶配额信息。 您必须是桶拥有者或拥有获取桶配额的权限,才能获取桶配额。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketQuota权限,如果使用桶策略则需授予GetBucketQuota权限。相关授权方式介绍可参见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 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.GetBucketQuota(bucketname) if err == nil { fmt.Printf("Get bucket(%s)'s quota successful!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) fmt.Printf("Quota:%d\n", output.Quota) return } fmt.Printf("Get bucket(%s)'s quota 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) } }
  • 代码示例 本示例用于删除examplebucket桶中的example/objectname对象 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 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.DeleteObjectInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定删除对象,此处以 example/objectname 为例。 input.Key = "example/objectname" // 删除对象 output, err := obsClient.DeleteObject(input) if err == nil { fmt.Printf("Delete object(%s) under the bucket(%s) successful!\n", input.Key, input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Delete object(%s) under the bucket(%s) fail!\n", input.Key, 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) } }
  • 代码示例 本示例用于删除桶名为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.DeleteBucketPolicy(bucketname) if err == nil { fmt.Printf("Delete policy successful with bucket:%s!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Delete policy 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) } }
  • 功能介绍 OBS支持对桶操作进行权限控制,您可以为桶设置访问策略,指定某一个用户对某一个桶是否有权行使某一项指定操作。OBS权限控制的方式有IAM、桶策略和ACL三种,本节将对桶策略接口进行详细介绍,更多权限相关内容可参见《对象存储服务权限配置指南》的OBS权限控制概述章节。 桶策略是作用于所配置的OBS桶及桶内对象的,您可以通过桶策略可为IAM用户或其他帐号授权桶及桶内对象的操作权限。当不同的桶对于不同的IAM用户有不同的访问控制需求时,需使用桶策略分别授权IAM用户不同的权限。 调用删除桶策略,您可删除指定桶的桶策略。无论桶的策略本身是否存在,删除成功后系统都直接返回“204 No Content”的结果。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 bucketName string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 返回结果说明 表2 返回结果列表 参数名称 参数类型 描述 output *BaseModel 参数解释: 接口返回信息,详情参考BaseModel。 err error 参数解释: 接口返回错误信息。 表3 BaseModel 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无
  • 接口约束 您必须是桶拥有者或拥有删除桶策略的权限,才能删除桶策略。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:DeleteBucketPolicy权限,如果使用桶策略则需授予DeleteBucketPolicy权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 bucketName string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 代码示例 本示例用于获取名为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) } }
  • 接口约束 您必须是桶拥有者或拥有获取桶的多版本状态的权限,才能获取桶的多版本状态。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketVersioning权限,如果使用桶策略则需授予GetBucketVersioning权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 代码示例 本示例用于查询一个任务所属的所有段信息。 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 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.ListPartsInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定对象名,此处以 example/objectname 为例。 input.Key = "example/objectname" // 指定多段上传任务号,此处以00000188677110424014075CC4A77xxx为例。 input.UploadId = "00000188677110424014075CC4A77xxx" // 列举已上传段 output, err := obsClient.ListParts(input) if err == nil { fmt.Printf("List part successful with bucket(%s) and object(%s)!\n", input.Bucket, input.Object) for index, part := range output.Parts { fmt.Printf("Part[%d]-ETag:%s, PartNumber:%d, LastModified:%s, Size:%d\n", index, part.ETag, part.PartNumber, part.LastModified, part.Size) } return } fmt.Printf("List part fail with bucket(%s) and object(%s)!\n", input.Bucket, input.Object) 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) } }
  • 功能说明 通过分段上传任务的ID,列举指定桶中已上传的段。 您可以列出特定多段上传任务或所有正在进行的多段上传任务的分段。列举已上传的段操作将返回您已为特定多段上传任务而上传的段信息。对于每个列举已上传的段请求,OBS将返回有关特定多段上传任务的分段信息,最多为1000个分段。如果多段上传中的段超过1000个,您必须发送一系列列举已上传的段请求以检索所有段。请注意,返回的分段列表不包括已合并的分段。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *ListPartsInput 必选 参数解释: 列举已上传的段请求参数,详见ListPartsInput。 extensions extensionOptions 可选 参数解释: 拓展配置项。 表2 ListPartsInput 参数名称 参数类型 是否必选 描述 Bucket string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 Key string 必选 参数解释: 对象名。对象名是对象在存储桶中的唯一标识。对象名是对象在桶中的完整路径,路径中不包含桶名。 例如,您对象的访问地址为examplebucket.obs.cn-north-4.myhuaweicloud.com/folder/test.txt 中,对象名为folder/test.txt。 取值范围: 长度大于0且不超过1024的字符串。 默认取值: 无 UploadId string 必选 参数解释: 分段上传任务的ID,例如:000001648453845DBB78F2340DD460D8 取值范围: 长度大于0且不超过32的字符串。 默认取值: 无 PartNumberMarker int 可选 参数解释: 列举已上传段的起始位置。 约束限制: 只有PartNumber数目大于该参数的Part会被列出 默认取值: 无 MaxParts int 可选 参数解释: 列举已上传段的返回结果最大段数目,即分页时每一页中段数目。 约束限制: 如果该参数超出1000时,则按照默认的1000进行处理。 取值范围: 范围是[1,1000] 默认取值: 1000 EncodingType string 可选 参数解释: 用于指定对响应中的Key进行指定类型的编码。如果Key包含xml 1.0标准不支持的控制字符,可通过设置该参数对响应中的Key进行编码。 取值范围: 可选值为url。 默认取值: 无,不设置则不编码。
  • 接口约束 您必须是桶拥有者或拥有列举已上传的段的权限,才能列举已上传的段。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:object:ListMultipartUploadParts权限,如果使用桶策略则需授予ListMultipartUploadParts权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、配置对象策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 接口约束 您必须是桶拥有者或拥有设置桶的多版本状态的权限,才能设置桶的多版本状态。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:PutBucketVersioning权限,如果使用桶策略则需授予PutBucketVersioning权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
共100000条