华为云用户手册

  • 代码示例 本示例用于获取名为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.GetBucketStorageInfo(bucketname) if err == nil { fmt.Printf("Get bucket(%s)'s storage-info successful!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) fmt.Printf("Size:%d, ObjectNumber:%d\n", output.Size, output.ObjectNumber) return } fmt.Printf("Get bucket(%s)'s storage-info 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桶的加密配置,并指定加密方式是kms,使用指定密钥为ce706220-a1f1-4e78-9ebb-xxxxxxxxxx32 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 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.SetBucketEncryptionInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定服务端加密算法,这里以kms为例。 input.SSEAlgorithm = "kms" // 指定SSE-KMS加密方式下使用的KMS主密钥,如果未指定,则默认的主密钥将会被使用。 input.KMSMasterKeyID = "ce706220-a1f1-4e78-9ebb-xxxxxxxxxx32" // 设置桶加密配置 output, err := obsClient.SetBucketEncryption(input) if err == nil { fmt.Printf("Set bucket(%s)'s encryption configuration successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Set bucket(%s)'s encryption 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) } }
  • 返回结果说明 表3 返回结果列表 参数名称 参数类型 描述 output *BaseModel 参数解释: 接口返回信息,详见BaseModel。 err error 参数解释: 接口返回错误信息。 表4 BaseModel 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无
  • 接口约束 您必须是桶拥有者或拥有设置桶加密配置的权限,才能设置桶加密配置。建议使用 IAM 或桶策略进行授权,如果使用IAM则需授予obs:bucket:PutEncryptionConfiguration权限,如果使用桶策略则需授予PutEncryptionConfiguration权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *SetBucketEncryptionInput 必选 参数解释: 设置桶加密配置请求参数,详见SetBucketEncryptionInput。 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。 表2 SetBucketEncryptionInput 参数名称 参数类型 是否可选 描述 Bucket string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 SSEAlgorithm string 必选 参数解释: 表示服务端加密是SSE-KMS方式。对象使用SSE-KMS方式加密。 取值范围: 可选值:kms,即选择SSE-KMS方式加密对象。 默认取值: 无 KMSMasterKeyID string 可选 参数解释: SSE-KMS加密方式下使用的KMS主密钥。 取值范围: 有效值支持两种格式: regionID:domainID(账号ID):key/key_id key_id 其中: regionID是使用密钥所属region的ID,可在地区和终端节点页面获取; domainID是使用密钥所属账号的账号ID,获取方法参见如何获取账号ID和用户ID?; key_id是从 数据加密 服务创建的密钥ID,获取方法请参见查看密钥。 默认取值: 如果用户没有提供该头域,那么默认的主密钥将会被使用。 ProjectID string 可选 参数解释: SSE-KMS加密方式下KMS主密钥所属的项目ID。 约束限制: 项目ID需要与KMSMasterKeyID相匹配。 在未设置KMSMasterKeyID的情况下,不可设置项目ID。 取值范围: 项目ID获取方式请参见如何获取项目ID。 默认取值: 无
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 bucketName string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 接口约束 您必须是桶拥有者或拥有删除桶的CORS配置的权限,才能删除桶的CORS配置。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:PutBucketCORS权限,如果使用桶策略则需授予PutBucketCORS权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 代码示例 本示例用于删除名为examplebucket桶的CORS规则 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.DeleteBucketWebsiteConfiguration(bucketname) if err == nil { fmt.Printf("Delete bucket(%s)'s website configuration successful!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Delete bucket(%s)'s website configuration 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响应头信息。 默认取值: 无
  • 功能介绍 跨域资源共享(Cross Origin Resource Sharing,CORS)是由W3C标准化组织提出的一种网络浏览器的规范机制,定义了一个域中加载的客户端Web应用程序与另一个域中的资源交互的方式。而在通常的网页请求中,由于同源安全策略(Same Origin Policy,SOP)的存在,不同域之间的网站脚本和内容是无法进行交互的。OBS支持CORS规范,允许跨域请求访问OBS中的资源。 调用删除桶的CORS配置接口,您可删除指定桶的跨域资源共享规则。
  • 返回结果说明 表10 返回结果列表 参数名称 参数类型 描述 output *BaseModel 参数解释: 接口返回信息,详见BaseModel。 err error 参数解释: 接口返回错误信息。 表11 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 40 41 42 43 44 45 46 47 48 49 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.SetBucketWebsiteConfigurationInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定默认页面配置,此处以index.html为例,含义是请求examplebucket/images/时,返回的数据将是samplebucket桶内名为images/index.html的对象的内容 input.IndexDocument.Suffix = "index.html" // 指定错误页面配置,此处以error.html为例 input.ErrorDocument.Key = "error.html" // 指定请求重定向规则列表 input.RoutingRules = []obs.RoutingRule{ {Redirect: obs.Redirect{HostName: "www.a.com", Protocol: obs.ProtocolHttp, ReplaceKeyPrefixWith: "prefix", HttpRedirectCode: "304"}}, {Redirect: obs.Redirect{HostName: "www.b.com", Protocol: obs.ProtocolHttps, ReplaceKeyWith: "replaceKey", HttpRedirectCode: "304"}}, } // 设置桶的Website配置 output, err := obsClient.SetBucketWebsiteConfiguration(input) if err == nil { fmt.Printf("Set bucket(%s)'s website successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Set bucket(%s)'s website 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) } }
  • 接口约束 尽量避免目标桶名中带有“.”,否则通过HTTPS访问时可能出现客户端校验证书出错。 设置桶的网站配置请求消息体的上限是10KB。 您必须是桶拥有者或拥有设置桶的网站配置的权限,才能设置桶的网站配置。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:PutBucketWebsite权限,如果使用桶策略则需授予PutBucketWebsite权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *SetBucketWebsiteConfigurationInput 必选 参数解释: 设置桶的网站配置请求参数,详见SetBucketWebsiteConfigurationInput。 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 方式一:手动下载开发包使用GoLand新建工程并编译 以安装OBS Go SDK最新版本为例,步骤如下: 下载OBS Go SDK开发包。 解压该开发包,可以看到其中包含obs文件夹(SDK源码)、main文件夹和examples文件夹(示例代码)和README.MD(SDK版本特性描述文件)。 使用GoLand新建Go工程,将obs、examples、main文件夹拷贝至新建工程中的src文件夹下。 右键单击建好的Go工程,运行弹出菜单中的“Build Project”命令编译工程并等待编译完成。 编译成功后,您的目录结构应该像下面这样: ├── bin ├── pkg ├── src -----├── examples -----├── main -----└── obs └── README.MD
  • 返回结果说明 表2 返回结果列表 参数名称 参数类型 描述 output *GetBucketEncryptionOutput 参数解释: 接口返回信息,详情参考GetBucketEncryptionOutput。 err error 参数解释: 接口返回错误信息。 表3 GetBucketEncryptionOutput 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无 SSEAlgorithm string 参数解释: 表示服务端加密是SSE-KMS方式。对象使用SSE-KMS方式加密。 取值范围: 可选值:kms,即选择SSE-KMS方式加密对象。 默认取值: 无 KMSMasterKeyID string 参数解释: SSE-KMS加密方式下使用的KMS主密钥ID。 取值范围: 有效值支持两种格式: regionID:domainID(账号ID):key/key_id key_id 其中: regionID是使用密钥所属region的ID,可在地区和终端节点页面获取; domainID是使用密钥所属账号的账号ID,获取方法参见如何获取账号ID和用户ID?; key_id是从数据加密服务创建的密钥ID,获取方法请参见查看密钥。 默认取值: 如果用户没有提供该头域,那么默认的主密钥将会被使用。 ProjectID string 参数解释: SSE-KMS加密方式下KMS主密钥所属的项目ID。 约束限制: 项目ID需要与KMSMasterKeyID相匹配。 在未设置KMSMasterKeyID的情况下,不可设置项目ID。 取值范围: 项目ID获取方式请参见如何获取项目ID。 默认取值: 无
  • 接口约束 您必须是桶拥有者或拥有获取桶加密配置的权限,才能获取桶加密配置。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetEncryptionConfiguration权限,如果使用桶策略则需授予GetEncryptionConfiguration权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 bucketName string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 取值范围: 长度为3~63个字符。 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 41 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.GetBucketEncryption(bucketname) if err == nil { fmt.Printf("Get bucket(%s)'s encryption configuration successful!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) fmt.Printf("Encryption:%s\n", output.SSEAlgorithm) fmt.Printf("KeyID:%s\n", output.KMSMasterKeyID) return } fmt.Printf("Get bucket(%s)'s encryption configuration 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) } }
  • 支持接口 OBS Go SDK支持服务端加密的接口见下表: OBS Go SDK接口方法 描述 支持加密类型 ObsClient.PutObject 上传对象时设置加密算法、密钥,对对象启用服务端加密。 SSE-KMS SSE-C ObsClient.PutFile 上传文件时设置加密算法、密钥,对对象启用服务端加密。 SSE-KMS SSE-C ObsClient.GetObject 下载对象时设置解密算法、密钥,用于解密对象。 SSE-C ObsClient.CopyObject 复制对象时设置源对象的解密算法、密钥,用于解密源对象。 复制对象时设置目标对象的加密算法、密钥,对目标对象启用加密算法。 SSE-KMS SSE-C ObsClient.GetObjectMetadata 获取对象元数据时设置解密算法、密钥,用于解密对象。 SSE-C ObsClient.InitiateMultipartUpload 初始化分段上传任务时设置加密算法、密钥,对分段上传任务最终生成的对象启用服务端加密。 SSE-KMS SSE-C ObsClient.UploadPart 上传段时设置加密算法、密钥,对分段数据启用服务端加密。 SSE-C ObsClient.CopyPart 复制段时设置源对象的解密算法、密钥,用于解密源对象。 复制段时设置目标段的加密算法、密钥,对目标段启用加密算法。 SSE-C
  • 功能说明 用户可以使用普通方式上传、下载对象,也可以使用服务端加密方式进行上传、下载对象。 OBS支持服务端加密功能,使加密的行为在服务端进行。 用户可以根据自身的需求,使用不同的密钥管理方式来使用服务端加密功能。当前支持两种服务端加密方式: KMS托管密钥的服务端加密(SSE-KMS)和客户提供加密密钥的服务端加密(SSE-C)。上述两种方式都采用行业标准的AES256加密算法。 SSE-KMS方式,OBS使用KMS(Key Management Service)服务提供的密钥进行服务端加密。 SSE-C方式,OBS使用用户提供的密钥和密钥的MD5值进行服务端加密。 使用服务端加密,返回的ETag值不是对象的MD5值。无论是否使用服务端加密上传对象,请求消息头中加入Content-MD5参数时,OBS均会对对象进行MD5校验。
  • 代码示例 本示例用于列举所有桶列表 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.ListBucketsInput{} // 指定桶列表中是否存在Location字段,此处以true为例,默认是false input.QueryLocation = true // 指定桶类型,此处以为obs.OBJECT为例,即获取所有桶列表,默认不设置则获取所有桶和并行文件系统列表 input.BucketType = obs.OBJECT // 列举桶列表 output, err := obsClient.ListBuckets(input) if err == nil { fmt.Printf("List buckets successful!\n") fmt.Printf("RequestId:%s\n", output.RequestId) for index, val := range output.Buckets { fmt.Printf("Bucket[%d]-Name:%s,CreationDate:%s\n", index, val.Name, val.CreationDate) } return } fmt.Printf("List buckets fail!\n") 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 *ListBucketsOutput 参数解释: 接口返回信息,详情参见ListBucketsOutput。 err error 参数解释: 接口返回错误信息。 表5 ListBucketsOutput 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无 Owner Owner 参数解释: 桶的所有者。 取值范围: 桶所有者取值参见Owner。 Buckets []Bucket 参数解释: 桶信息列表。 取值范围: 桶信息取值参见Bucket。 表6 Owner 参数名称 参数类型 是否必选 描述 ID string 作为请求参数时必选 参数解释: 所有者的账号ID,即domain_id。 取值范围: 如何获取账号ID请参见如何获取账号ID和用户ID? 默认取值: 无 表7 Bucket 参数名称 参数类型 描述 Name string 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用类IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 默认取值: 无 CreationDate time.Time 参数解释: 桶的创建时间。 默认取值: 无 Location string 参数解释: 桶所在的区域。 约束限制: 该参数定义了桶将会被创建在哪个区域,如果使用的终端节点是obs.myhuaweicloud.com,可以不携带此参数;如果使用的终端节点不是obs.myhuaweicloud.com,则必须携带此参数。 取值范围: 当前有效的OBS区域和终端节点的更多信息,请参考地区和终端节点。终端节点即调用API的请求地址,不同服务不同区域的终端节点不同,您可以向企业管理员获取区域和终端节点信息。 默认取值: 终端节点为obs.myhuaweicloud.com且用户未设定区域时,默认为华北-北京一(cn-north-1)。 BucketType string 参数解释: 桶类型。 取值范围: 对象桶:OBJECT 并行文件系统:POSIX。 默认取值: 无
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *ListBucketsInput 可选 参数解释: 获取桶列表入参,详情参考ListBucketsInput。 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。 表2 ListBucketsInput 参数名称 参数类型 是否必选 描述 QueryLocation bool 可选 参数解释: 是否同时查询桶的区域位置。 取值范围: true:同时查询桶的区域位置 false:不查询桶的区域位置 默认取值: false BucketType BucketType 可选 参数解释: 列举指定类型的桶。 取值范围: 桶类型取值可参见BucketType。 默认取值: 如果不赋值则默认获取所有桶和并行文件系统列表。 表3 BucketType 常量名 原始值 说明 OBJECT OBJECT 对应类型为对象存储桶。 POSIX POSIX 对应类型为并行文件系统(POSIX)。
  • 代码示例 本示例用于删除名为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.DeleteBucketEncryption(bucketname) if err == nil { fmt.Printf("Delete bucket(%s)'s encryption configuration successful!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Delete bucket(%s)'s encryption configuration 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响应头信息。 默认取值: 无
  • 接口约束 您必须是桶拥有者或拥有删除桶加密配置的权限,才能删除桶加密配置。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:PutEncryptionConfiguration权限,如果使用桶策略则需授予PutEncryptionConfiguration权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 bucketName string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 extensions extensionOptions 可选 参数解释: 桶相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 代码示例 本示例用于删除名为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 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.DeleteBucketCustomDomainInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定桶的自定义域名,此处以www.example.com为例 input.CustomDomain = "www.example.com" // 删除桶的自定义域名 output, err := obsClient.DeleteBucketCustomDomain(input) if err == nil { fmt.Printf("Delete bucket(%s)'s customdomain successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Delete bucket(%s)'s customdomain 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) } }
  • 返回结果说明 表3 返回结果列表 参数名称 参数类型 描述 output *BaseModel 参数解释: 接口返回信息,详情参考BaseModel。 err error 参数解释: 接口返回错误信息。 表4 BaseModel 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无
共100000条