华为云用户手册

  • 代码示例 本示例用于追加上传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 43 44 45 46 47 48 package main import ( "fmt" "os" "strings" 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.AppendObjectInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定追加上传对象,此处以 example/objectname 为例。 input.Key = "example/objectname" // 指定追加写位置。如果是首次上传时就必须指定position为0 input.Position = 0 // 指定待上传对象的数据流 input.Body = strings.NewReader("Hello OBS") // 追加上传对象 output, err := obsClient.AppendObject(input) if err == nil { fmt.Printf("Append object(%s) under the bucket(%s) successful!\n", input.Key, input.Bucket) fmt.Printf("ETag:%s, NextAppendPosition:%d\n", output.ETag, output.NextAppendPosition) return } fmt.Printf("Append objects under the 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) } }
  • 返回结果说明 表7 返回结果 参数名称 参数类型 描述 output *AppendObjectOutput 参数解释: 接口返回信息,详情参考AppendObjectOutput。 err error 参数解释: 接口返回错误信息。 表8 AppendObjectOutput 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无 VersionId string 参数解释: 对象的版本号。如果桶的多版本状态为开启,则会返回对象的版本号。 取值范围: 长度为32的字符串 默认取值: 无 ETag string 参数解释: 对象的ETag值,即base64编码的128位MD5摘要。ETag是对象内容的唯一标识,可以通过该值识别对象内容是否有变化。比如上传对象时ETag为A,下载对象时ETag为B,则说明对象内容发生了变化。ETag只反映变化的内容,而不是其元数据。上传的对象或拷贝操作创建的对象,都有唯一的ETag。 约束限制: 当对象是服务端加密的对象时,ETag值不是对象的MD5值。 取值范围: 长度为32的字符串。 默认取值: 无 SseHeader SseCHeader 或 SseKmsHeader 参数解释: 服务端加密头信息,选择SSE-C加密方式,则详见SseCHeader,选择SSE-KMS加密方式。则详见SseKmsHeader。 NextAppendPosition int64 参数解释: 下一次追加上传请求应该提供的追加写位置(position)。 约束限制: 需要追加写的对象首次上传时就必须指定为0,下次追加需要填写的值为服务端本次上传成功返回消息的NextAppendPosition。 取值范围: 0~(263-1),单位:字节。 默认取值: 0 表9 SseCHeader 参数名称 参数类型 是否必选 描述 Encryption string 作为请求参数时必选 参数解释: 表示服务端加密是SSE-C方式。对象使用SSE-C加密方式。 取值范围: 可选值:AES256,即选择SSE-C方式加密对象。 默认取值: 无 Key string 作为请求参数时必选 参数解释: SSE-C方式下加密使用的密钥,该密钥用于加密对象。 约束限制: 该头域由256bit的密钥经过Base64编码得到,示例:K7QkYpBkM5+hca27fsNkUnNVaobncnLht/rCB2o/9Cw= 默认取值: 无 KeyMD5 string 作为请求参数时可选 参数解释: SSE-C方式下加密使用密钥的MD5值,该值用于验证密钥传输过程中是否出错。 约束限制: 由密钥值经过MD5加密再经过Base64编码后得到,示例:4XvB3tbNTN+tIEVa0/fGaQ== 默认取值: 无 表10 SseKmsHeader 参数名称 参数类型 是否必选 描述 Encryption string 作为请求参数时必选 参数解释: 表示服务端加密是SSE-KMS方式。对象使用SSE-KMS方式加密。 取值范围: 可选值:kms,即选择SSE-KMS方式加密对象。 默认取值: 无 Key string 作为请求参数时可选 参数解释: SSE-KMS加密方式下使用的KMS主密钥的ID值。 取值范围: 有效值支持两种格式: regionID:domainID(账号ID):key/key_id key_id 其中: regionID是使用密钥所属region的ID,可在地区和终端节点页面获取; domainID是使用密钥所属账号的账号ID,获取方法参见如何获取账号ID和用户ID?; key_id是从数据加密服务创建的密钥ID,获取方法请参见查看密钥。 默认取值: 如果用户没有提供该头域,那么默认的主密钥将会被使用。 如果默认主密钥不存在,将默认创建并使用。
  • 接口约束 您必须是桶拥有者或拥有上传对象的权限,才能上传对象。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:object:PutObject权限,如果使用桶策略则需授予PutObject权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、配置对象策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。 用户上传的对象存储在桶中。用户必须对桶有WRITE权限,才可以在桶中上传对象。同一个桶中存储的对象名必须是唯一的。 每次追加上传都会更新该对象的最后修改时间。 服务端加密SSE-C方式,那么追加上传和初始化段一样,设置诸如x-obs-server-side-encryption之类的请求Header,后续追加上传也必须携带。 服务端加密SSE-KMS方式,有且只有第一次上传且桶内不存在同名对象时,才设置诸如x-obs-server-side-encryption之类的请求Header,后续追加上传不携带。 每次追加上传的长度不能超过对象长度上限5G的限制。 每个Appendable对象追加写次数最多为10000次。 如果对象存储类型为COLD(归档存储),则不能调用该接口。 如果桶设置了跨区域复制配置,则不能调用该接口。 并行文件系统不支持追加写对象。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *RestoreObjectInput 必选 参数解释: 恢复归档存储对象请求参数,详见RestoreObjectInput。 extensions extensionOptions 可选 参数解释: 拓展配置项。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。 表2 RestoreObjectInput 参数名称 参数类型 是否必选 描述 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的字符串。 默认取值: 无 VersionId string 可选 参数解释: 待恢复归档存储对象的版本号。 默认取值: 无,如果不设置则默认指定最新版本的对象。 Days int 必选 参数解释: 恢复对象后,会生成一个对象的标准存储副本,此参数指定恢复有效期,即标准存储副本的保存时间。 取值范围: [1, 30],单位:天。 默认取值: 无 Tier RestoreTierType 可选 参数解释: 恢复选项,用户可根据需求选择恢复选项,不同的恢复选项恢复耗时不同。 取值范围: 恢复选项可选值详见RestoreTierType。 默认取值: 标准恢复 表3 RestoreTierType 常量名 原始值 说明 RestoreTierExpedited Expedited 快速恢复,归档存储恢复耗时1~5 min。 RestoreTierStandard Standard 标准恢复,归档存储恢复耗时3~5 h。
  • 返回结果说明 表4 返回结果 参数名称 参数类型 描述 output *BaseModel 参数解释: 接口返回信息,详见BaseModel。 err error 参数解释: 接口返回错误信息。 表5 BaseModel 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无
  • 代码示例 本示例用于在恢复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 43 44 45 46 47 48 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.RestoreObjectInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定归档对象名称,此处以example/objectname为例。 input.Key = "example/objectname" // 指定待取回归档对象的对应版本号 input.VersionId = "G001117FCE89978B0000401205D5DC9A" // 指定恢复对象的保存时间,此处以1为例,单位天,取值范围:[1, 30]。 input.Days = 1 // 指定恢复选项,此处以obs.RestoreTierExpedited为例,默认为标准恢复。 input.Tier = obs.RestoreTierExpedited // 取回归档对象 output, err := obsClient.RestoreObject(input) if err == nil { fmt.Printf("Restore object(%s) under the bucket(%s) successful!\n", input.Key, input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Restore 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) } } ObsClient.RestoreObject中指定的对象必须是归档存储类型,否则调用该接口会报错。 使用RestoreObjectInput.Days指定恢复对象保存的时间,取值范围是1~30;使用RestoreObjectInput.Tier指定恢复选项,表示恢复选项。
  • 接口约束 您必须是桶拥有者或拥有恢复归档存储对象的权限,才能恢复归档存储对象。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:object:RestoreObject权限,如果使用桶策略则需授予RestoreObject权限。相关授权方式介绍可参见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 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)。
  • 上传对象简介(Go SDK) 在OBS中,用户操作的基本数据单元是对象。OBS Go SDK提供了丰富的对象上传接口,可以通过以下方式上传对象: 流式上传 文件上传 分段上传 断点续传上传 SDK支持上传0KB~5GB的对象。流式上传和文件上传的内容大小不能超过5GB;当上传较大文件时,请使用分段上传,分段上传每段内容大小不能超过5GB。 如果上传的对象权限设置为匿名用户读取权限,对象上传成功后,匿名用户可通过链接地址访问该对象数据。对象链接地址格式为:https://桶名.域名/文件夹目录层级/对象名。如果该对象存在于桶的根目录下,则链接地址将不需要有文件夹目录层级。 上传对象-流式上传(Go SDK) 上传对象-文件上传(Go SDK) 上传对象-追加上传(Go SDK) 上传对象-断点续传上传(Go SDK) 父主题: 对象相关接口(Go SDK)
  • 返回结果说明 表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的并行文件系统,并设置所在区域在华北-北京四(cn-north-4),桶的权限访问控制策略是私有桶,多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" // 指定存储桶的权限控制策略,此处以obs.AclPrivate为例。 input.ACL = obs.AclPrivate // 指定存储桶的AZ类型,此处以“3AZ”为例。不携带时默认为单AZ,如果对应region不支持多AZ存储,则该桶的存储类型仍为单AZ。 input.AvailableZone = "3az" // 指定桶的类型为并行文件系统 input.IsFSFileInterface = true // 创建桶 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) } }
  • 代码示例 本示例用于获取examplebucket并行文件系统中的目录Dirname的accesslabel。 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.GetDirAccesslabelInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定对象名,此处以 Dirname 为例。 input.Key = "Dirname" // 获取对象的AccessLabel output, err := obsClient.GetDirAccesslabel(input) if err == nil { fmt.Printf("Get dir(%s) accesslabel successful with bucket(%s)!\n", input.Key, input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Get dir(%s) accesslabel fail with bucket(%s)!\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) } }
  • 接口约束 建议使用IAM或策略进行授权,如果使用IAM则需授予obs:object:getAccessLabel权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、配置对象策略。 OBS支持的region以及region与endPoint的对应关系,详细信息请参见地区与终端节点。 目前接口仅在并行文件系统支持,普通对象桶不支持,如何创建并行文件系统请参考创建并行文件系统(Go SDK)。 仅支持并行文件系统的目录级别的设置,并行文件系统级和文件级均不支持。
  • 返回结果说明 表3 返回结果列表 参数名称 参数类型 描述 output *GetDirAccesslabelOutput 参数解释: 接口返回信息,详见GetDirAccesslabelOutput。 err error 参数解释: 接口返回错误信息。 表4 GetDirAccesslabelOutput 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无 AccessLabel []string 必选 参数解释: 目录的accesslabel内容。 约束限制: 仅包含0-9、a-z、 A-Z、下划线的字符串,最大长度为52个字符。 目录级别AccessLabel最大设置支持256条。 默认取值: 无
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *GetDirAccesslabelInput 必选 参数解释: 设置并行文件系统ACL请求参数,详见GetDirAccesslabelInput。 extensions extensionOptions 可选 参数解释: 并行文件系统相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 返回结果说明 表3 返回结果列表 参数名称 参数类型 描述 output *RenameFileOutput 参数解释: 接口返回信息,详见RenameFileOutput。 err error 参数解释: 接口返回错误信息。 表4 RenameFileOutput 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无
  • 接口约束 您必须是并行文件系统拥有者或拥有重命名文件的权限,才能重命名文件。建议使用IAM或策略进行授权,如果使用IAM则需授予obs:bucket:PutObject权限,如果使用策略则需授予PutObject权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、配置对象策略。 OBS支持的region以及region与endPoint的对应关系,详细信息请参见地区与终端节点。 目前接口仅在并行文件系统支持,普通对象桶不支持,如何创建并行文件系统请参考创建并行文件系统(Go SDK)。
  • 代码示例 本示例用于将examplebucket并行文件系统中的folderName/originalObjectName对象重命名为newFolderName/newObjectName。 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.RenameFileInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定原对象名,此处以 folderName/originalObjectName 为例。 input.Key = "folderName/originalObjectName" // 指定目标对象名,此处以 newFolderName/newObjectName 为例。 input.NewObjectKey = "newFolderName/newObjectName" // 重命名对象 output, err := obsClient.RenameFile(input) if err == nil { fmt.Printf("Rename File(%s) successful with bucket(%s)!\n", input.Key, input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Rename File(%s) fail with bucket(%s)!\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) } }
  • 接口约束 每次接口调用最多返回1000个对象信息。 您必须是并行文件系统拥有者或拥有列举并行文件系统内对象的权限,才能列举并行文件系统内对象。建议使用IAM或策略进行授权,如果使用IAM则需授予obs:bucket:ListBucket权限,如果使用策略则需授予ListBucket权限。相关授权方式介绍可参见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 46 47 48 49 50 51 52 53 54 55 56 // 本示例用于分页列举桶examplebucket的所有文件 package main import ( "fmt" "os" obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) func main() { // 您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入。如果使用硬编码可能会存在泄露风险。 // 您可以登录访问管理控制台获取访问密钥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.ListObjectsInput{} // 指定并行文件系统名称 input.Bucket = "examplebucket" // 指定返回的最大对象数,此处以 100 为例,返回的对象列表将是按照字典顺序的最多前max-keys个对象,默认值为1000。 input.MaxKeys = 100 // 指定编码方式,此处以“url”为例,如果列举对象中存在特殊字符,则该参数必传 input.EncodingType = "url" for { // 列举并行文件系统内对象 output, err := obsClient.ListObjects(input) if err != nil { fmt.Printf("List objects under the 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) } os.Exit(-1) } // 打印列举结果。默认情况下,一次返回100条记录。 fmt.Printf("List objects under the bucket(%s) successful!\n", input.Bucket) for index, val := range output.Contents { fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d\n", index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size) } if output.IsTruncated { input.Marker = output.NextMarker } else { break } } }
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *ListObjectsInput 必选 参数解释: 列举并行文件系统内对象接口入参,详情参考ListObjectsInput。 extensions extensionOptions 可选 参数解释: 并行文件系统相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 代码示例 本示例用于列举所有并行文件系统列表 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.POSIX为例,即获取所有并行文件系统,默认不设置则获取所有桶和并行文件系统列表 input.BucketType = obs.POSIX // 列举并行文件系统列表 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并行文件系统中的目录Dirname的accesslabel。 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.DeleteDirAccesslabelInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定对象名,此处以 Dirname 为例。 input.Key = "Dirname" // 对象的AccessLabel output, err := obsClient.DeleteDirAccesslabel(input) if err == nil { fmt.Printf("Delete dir(%s) accesslabel successful with bucket(%s)!\n", input.Key, input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Delete dir(%s) accesslabel fail with bucket(%s)!\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) } }
  • 接口约束 建议使用IAM或策略进行授权,如果使用IAM则需授予obs:object:deleteAccessLabel权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、配置对象策略。 OBS支持的region以及region与endPoint的对应关系,详细信息请参见地区与终端节点。 目前接口仅在并行文件系统支持,普通对象桶不支持,如何创建并行文件系统请参考创建并行文件系统(Go SDK)。 仅支持并行文件系统的目录级别的设置,并行文件系统级和文件级均不支持。
  • 返回结果说明 表3 返回结果列表 参数名称 参数类型 描述 output *BaseModel 参数解释: 接口返回信息,详见BaseModel。 err error 参数解释: 接口返回错误信息。 表4 BaseModel 参数名称 参数类型 描述 StatusCode int 参数解释: HTTP状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码。 默认取值: 无 RequestId string 参数解释: OBS服务端返回的请求ID。 默认取值: 无 ResponseHeaders map[string][]string 参数解释: HTTP响应头信息。 默认取值: 无
共100000条