更新时间:2024-02-01 GMT+08:00
分享

获取对象ACL(Go SDK)

功能说明

用户执行获取对象ACL的操作,返回信息包含指定对象的权限控制列表信息。

接口约束

  • 您必须是桶拥有者或拥有获取对象ACL的权限,才能获取对象ACL。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:object:GetObjectAcl权限,如果使用桶策略则需授予GetObjectAcl权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略配置对象策略
  • OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点
  • 用户必须拥有对指定对象读ACP(access control policy)的权限,才能执行获取对象ACL的操作。

方法定义

func (obsClient ObsClient) GetObjectAcl(input *GetObjectAclInput, extensions...extensionOptions) (output *GetObjectAclOutput, err error)

使用带授权信息URL的方法定义

func (obsClient ObsClient) GetObjectAclWithSignedUrl(signedUrl string, actualSignedRequestHeaders http.Header) (output *GetObjectAclOutput, err error)

请求参数说明

表1 请求参数列表

参数名称

参数类型

是否必选

描述

input

*GetObjectAclInput

必选

参数解释:

获取对象ACL请求参数,详情参考GetObjectAclInput

extensions

extensionOptions

可选

参数解释:

拓展配置项。通过调用拓展配置项为对应请求配置额外的拓展请求头,详情参考extensionOptions

表2 GetObjectAclInput

参数名称

参数类型

是否必选

描述

Bucket

string

必选

桶名。

Key

string

必选

参数解释:

对象名。对象名是对象在存储桶中的唯一标识。对象名是对象在桶中的完整路径,路径中不包含桶名。

例如,您对象的访问地址为examplebucket.obs.cn-north-4. myhuaweicloud.com/folder/test.txt 中,对象名为folder/test.txt。

取值范围:

长度大于0且不超过1024的字符串。

默认取值:

VersionId

string

可选

对象的版本号。例如:G001117FCE89978B0000401205D5DC9A。

默认值:无

返回结果说明

表3 返回结果

参数名称

参数类型

描述

output

*GetObjectAclOutput

参数解释:

接口返回信息,GetObjectAclOutput

err

error

参数解释:

接口返回错误信息。

表4 GetObjectAclOutput

参数名称

参数类型

描述

StatusCode

int

参数解释:

HTTP状态码。

取值范围:

状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。完整的状态码列表请参见状态码

默认取值:

RequestId

string

参数解释:

OBS服务端返回的请求ID。

默认取值:

ResponseHeaders

map[string][]string

参数解释:

HTTP响应头信息。

默认取值:

VersionId

string

参数解释:

对象的版本号。例如:G001117FCE89978B0000401205D5DC9A。

取值范围:

长度为32的字符串。

默认取值:

Owner

Owner

参数解释:

对象的所有者账号户ID,详见Owner

约束限制:

Owner和Grants必须配套使用,且与ACL互斥。

Grants

[]Grant

参数解释:

被授权用户权限信息,详livered见Grant

默认取值:

表5 Owner

参数名称

参数类型

是否必选

描述

ID

string

作为请求参数时必选

参数解释:

所有者的账号ID,即domain_id。

取值范围:

如何获取账号ID请参见如何获取账号ID和用户ID?

默认取值:

表6 Grant

参数名称

参数类型

是否必选

描述

Grantee

Grantee

作为请求参数时必选

参数解释:

被授权用户相关信息,详见Grantee

Permission

PermissionType

作为请求参数时必选

参数解释:

被授予的权限。

取值范围:

权限取值范围详见PermissionType

默认取值:

表7 Grantee

参数名称

参数类型

是否必选

描述

Type

GranteeType

作为请求参数时必选

参数解释:

被授权用户的类型,详见GranteeType

ID

string

作为请求参数时,如果Type为用户类型则必选。

参数解释:

被授权用户的账号ID,即domain_id。

取值范围:

如何获取账号ID请参见如何获取账号ID和用户ID?

默认取值:

DisplayName

string

作为请求参数时可选

参数解释:

被授权用户的账号名。

约束限制:

  • 不支持中文。
  • 只能以字母开头。
  • 长度为6-32个字符。
  • 只能包含英文字母、数字或特殊字符(-_)。

默认取值:

URI

GroupUriType

作为请求参数时,如果Type为用户组类型则必选。

参数解释:

被授权的用户组。

取值范围:

授权用户组取值范围详见GroupUriType

默认取值:

表8 PermissionType

常量名

原始值

说明

PermissionRead

READ

读权限。

PermissionWrite

WRITE

写权限。

PermissionReadAcp

READ_ACP

读取ACL配置的权限。

PermissionWriteAcp

WRITE_ACP

修改ACL配置的权限。

PermissionFullControl

FULL_CONTROL

完全控制权限,包括对桶或对象的读写权限,以及对桶或对象ACL配置的读写权限。

表9 GranteeType

常量名

原始值

说明

GranteeGroup

Group

用户组。

GranteeUser

CanonicalUser

单个用户。

表10 GroupUriType

常量名

原始值

说明

GroupAllUsers

AllUsers

所有用户。

代码示例

本示例用于获取名为examplebucket桶下example/objectname对象的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
43
44
45
46
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.GetObjectAclInput{}
    // 指定存储桶名称
    input.Bucket = "examplebucket"
    // 指定对象,此处以 example/objectname 为例。
    input.Key = "example/objectname"
    // 获取对象ACL
    output, err := obsClient.GetObjectAcl(input)
    if err == nil {
        fmt.Printf("Get object(%s)'s acl successful with bucket(%s)!\n", input.Key, input.Bucket)
        fmt.Printf("Owner.ID:%s\n", output.Owner.ID)
        for index, grant := range output.Grants {
            fmt.Printf("Grant[%d]-Type:%s, ID:%s, URI:%s, Permission:%s\n",
                index, grant.Grantee.Type, grant.Grantee.ID, grant.Grantee.URI, grant.Permission)
        }
        return
    }
    fmt.Printf("Get object(%s)'s acl 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)
    }
}
 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() {
    //推荐通过环境变量获取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.GetObjectAclInput{}
    // 指定存储桶名称
    input.Bucket = "examplebucket"
    // 指定对象,此处以 example/objectname 为例。
    input.Key = "example/objectname"
    // 获取对象ACL
    output, err := obsClient.GetObjectAcl(input)
    if err == nil {
        fmt.Printf("Get object(%s)'s acl successful with bucket(%s)!\n", input.Key, input.Bucket)
        fmt.Printf("Owner.ID:%s\n", output.Owner.ID)
        for index, grant := range output.Grants {
            fmt.Printf("Grant[%d]-Type:%s, ID:%s, URI:%s, Permission:%s\n",
                index, grant.Grantee.Type, grant.Grantee.ID, grant.Grantee.URI, grant.Permission)
        }
        return
    }
    fmt.Printf("Get object(%s)'s acl 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)
    }
}

相关链接

  • 关于获取对象ACL的API说明,请参见获取对象ACL
  • 更多关于获取对象ACL的示例代码,请参见Github示例
  • 获取对象ACL过程中返回的错误码含义、问题原因及处理措施可参考OBS错误码
分享:

    相关文档

    相关产品