云服务器内容精选

  • 代码示例 本示例用于分页列举并行文件系统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 } } }
  • 接口约束 每次接口调用最多返回1000个对象信息。 您必须是并行文件系统拥有者或拥有列举并行文件系统内对象的权限,才能列举并行文件系统内对象。建议使用 IAM 或策略进行授权,如果使用IAM则需授予obs:bucket:ListBucket权限,如果使用策略则需授予ListBucket权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略、自定义创建桶策略。 OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *ListObjectsInput 必选 参数解释: 列举并行文件系统内对象接口入参,详情参考ListObjectsInput。 extensions extensionOptions 可选 参数解释: 并行文件系统相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 请求参数说明 表1 请求参数列表 参数名称 参数类型 是否必选 描述 input *SetDirAccesslabelInput 必选 参数解释: 设置并行文件系统ACL请求参数,详见SetDirAccesslabelInput。 extensions extensionOptions 可选 参数解释: 并行文件系统相关扩展信息。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions。
  • 代码示例 本示例用于设置并行文件系统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 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.SetDirAccesslabelInput{} // 指定存储桶名称 input.Bucket = "examplebucket" // 指定对象名,此处以 Dirname 为例。 input.Key = "Dirname" // 指定目录的accesslabel,这次以role_label_01和role_label_02为例 input.Accesslabel = []string{"role_label_01", "role_label_02"} // 设置对象的AccessLabel output, err := obsClient.SetDirAccesslabel(input) if err == nil { fmt.Printf("Set dir(%s) accesslabel successful with bucket(%s)!\n", input.Key, input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) return } fmt.Printf("Set 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:putAccessLabel权限。相关授权方式介绍可参见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响应头信息。 默认取值: 无