多版本对象权限
更新时间: 2019-03-14 10:05
设置多版本对象访问权限
您可以通过ObsClient.SetObjectAcl接口传入版本号(VersionId)设置多版本对象的访问权限,示例代码如下:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-point"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.SetObjectAclInput{}
- input.Bucket = "bucketname"
- input.Key = "objectkey"
- input.VersionId = "versionid"
- // 通过预定义访问策略设置指定版本对象的访问权限为公共读
- input.ACL = obs.AclPublicRead
- output, err := obsClient.SetObjectAcl(input)
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- // 直接设置指定版本对象的访问权限
- input.Owner.ID = "ownerid"
- var grants [3]obs.Grant
- // 为所有用户设置写权限
- grants[0].Grantee.Type = obs.GranteeGroup
- grants[0].Grantee.URI = obs.GroupAllUsers
- grants[0].Permission = obs.PermissionWrite
- // 为指定用户设置完全控制权限
- grants[1].Grantee.Type = obs.GranteeUser
- grants[1].Grantee.ID = "granteeid"
- grants[1].Permission = obs.PermissionFullControl
- // 为所有用户设置读权限
- grants[2].Grantee.Type = obs.GranteeGroup
- grants[2].Grantee.URI = obs.GroupAllUsers
- grants[2].Permission = obs.PermissionRead
- input.Grants = grants[0:3]
- output, err = obsClient.SetObjectAcl(input)
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
说明:
所有者ID或者被授权用户ID,是指用户的账户ID,可通过OBS控制台“我的凭证”页面查看。
获取多版本对象访问权限
您可以通过ObsClient.GetObjectAcl接口传入版本号(VersionId)获取多版本对象的访问权限,示例代码如下:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- input := &obs.GetObjectAclInput{}
- input.Bucket = "bucketname"
- input.Key = "objectkey"
- input.VersionId = "versionid"
- output, err := obsClient.GetObjectAcl(input)
- if err == nil {
- 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)
- }
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
父主题:多版本控制