管理桶策略
更新时间: 2019-03-14 10:05
除了桶访问权限外,桶的拥有者还可以通过桶策略,提供对桶和桶内对象的集中访问控制。
更多关于桶策略的内容请参考桶策略。
设置桶策略
您可以通过ObsClient.SetBucketPolicy设置桶策略。示例代码如下:
- // 引入依赖包
- 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.SetBucketPolicyInput{}
- input.Bucket = "bucketname"
- input.Policy = "your policy"
- output, err := obsClient.SetBucketPolicy(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)
- }
- }
说明:
桶策略内容的具体格式(JSON格式字符串)请参考《对象存储服务API参考》。
获取桶策略
您可以通过ObsClient.GetBucketPolicy获取桶策略。示例代码如下:
- // 引入依赖包
- 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() {
- output, err := obsClient.GetBucketPolicy("bucketname")
- if err == nil {
- fmt.Printf("Policy:%d\n", output.Policy)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
删除桶策略
您可以通过ObsClient.DeleteBucketPolicy删除桶策略。示例代码如下:
- // 引入依赖包
- 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() {
- output, err := obsClient.DeleteBucketPolicy("bucketname")
- 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)
- }
- }
父主题:管理桶