云服务器内容精选

  • 接口约束 您必须是桶拥有者或拥有获取桶存储类型的权限,才能获取桶存储类型。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketStoragePolicy权限,如果使用桶策略则需授予GetBucketStoragePolicy权限。相关授权方式介绍可参见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.GetBucketStoragePolicy(bucketname) if err == nil { fmt.Printf("Get bucket(%s)'s storage-class successful!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) fmt.Printf("StorageClass:%s\n", output.StorageClass) return } fmt.Printf("Get bucket(%s)'s storage-class 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) } }
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 bucketName string 必选 参数解释: 桶名。 约束限制: 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。 桶命名规则如下: 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。 禁止使用IP地址。 禁止以“-”或“.”开头及结尾。 禁止两个“.”相邻(如:“my..bucket”)。 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。 默认取值: 无 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.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)。
  • 请求参数说明 表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 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()) } // 指定存储桶名称 bucketname := "examplebucket" // 获取桶的网站配置 output, err := obsClient.GetBucketWebsiteConfiguration(bucketname) if err == nil { fmt.Printf("Get bucket(%s)'s website configuration successful!\n", bucketname) fmt.Printf("Get bucket metadata successful!\n") fmt.Printf("RequestId:%s\n", output.RequestId) fmt.Printf("Suffix:%s\n", output.IndexDocument.Suffix) fmt.Printf("Key:%s\n", output.ErrorDocument.Key) for index, routingRule := range output.RoutingRules { fmt.Printf("Condition[%d]-KeyPrefixEquals:%s, HttpErrorCodeReturnedEquals:%s\n", index, routingRule.Condition.KeyPrefixEquals, routingRule.Condition.HttpErrorCodeReturnedEquals) fmt.Printf("Redirect[%d]-Protocol:%s, HostName:%s, ReplaceKeyPrefixWith:%s, HttpRedirectCode:%s\n", index, routingRule.Redirect.Protocol, routingRule.Redirect.HostName, routingRule.Redirect.ReplaceKeyPrefixWith, routingRule.Redirect.HttpRedirectCode) } return } fmt.Printf("Get 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) } }
  • 接口约束 您必须是桶拥有者或拥有获取桶的网站配置的权限,才能获取桶的网站配置。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketWebsite权限,如果使用桶策略则需授予GetBucketWebsite权限。相关授权方式介绍可参见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 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()) } // 指定存储桶名称 bucketname := "examplebucket" // 获取桶标签 output, err := obsClient.GetBucketTagging(bucketname) if err == nil { fmt.Printf("Get bucket(%s)'s tagging configuration successful!\n", bucketname) fmt.Printf("RequestId:%s\n", output.RequestId) for index, tag := range output.Tags { fmt.Printf("Tag[%d]-Key:%s, Value:%s\n", index, tag.Key, tag.Value) } return } fmt.Printf("Get bucket(%s)'s tagging 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) } }
  • 接口约束 您必须是桶拥有者或拥有获取桶标签的权限,才能获取桶标签。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:bucket:GetBucketTagging权限,如果使用桶策略则需授予GetBucketTagging权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 按文件夹分组列举所有对象 OBS本身是没有文件夹的概念的,桶中存储的元素只有对象。文件夹对象实际上是一个大小为0且对象名以“/”结尾的对象,将这个文件夹对象名作为前缀,即可模拟列举文件夹中对象的功能。以下代码展示如何列举文件夹中的对象: 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() { // 您可以通过环境变量获取访问密钥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 // 指定对象名分组的分隔符,这里以/为例。 input.Delimiter = "/" // 列举桶内对象 output, err := obsClient.ListObjects(input) if err == nil { fmt.Printf("List objects under the bucket(%s) successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) 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) } return } 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) } }
  • 指定前缀列举 本示例用于指定前缀列举对象。 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() { // 您可以通过环境变量获取访问密钥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" // 指定列举对象前缀,此处以“test/”前缀为例,满足指定前缀的对象会被列举。 input.Prefix = "test/" // 指定返回的最大对象数,此处以 100 为例,返回的对象列表将是按照字典顺序的最多前max-keys个对象,默认值为1000。 input.MaxKeys = 100 // 列举桶内对象 output, err := obsClient.ListObjects(input) if err == nil { fmt.Printf("List objects under the bucket(%s) successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) 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) } return } 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) } }
  • 指定数目列举 本示例用于指定数目列举对象。 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 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 // 列举桶内对象 output, err := obsClient.ListObjects(input) if err == nil { fmt.Printf("List objects under the bucket(%s) successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) 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) } return } 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) } }