管理桶策略

更新时间: 2019-03-14 10:05

除了桶访问权限外,桶的拥有者还可以通过桶策略,提供对桶和桶内对象的集中访问控制。

更多关于桶策略的内容请参考桶策略

设置桶策略

您可以通过ObsClient.SetBucketPolicy设置桶策略。示例代码如下:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. )
  6.  
  7. var ak = "*** Provide your Access Key ***"
  8. var sk = "*** Provide your Secret Key ***"
  9. var endpoint = "https://your-endpoint"
  10.  
  11. // 创建ObsClient结构体
  12. var obsClient, _ = obs.New(ak, sk, endpoint)
  13.  
  14. func main() {
  15. input := &obs.SetBucketPolicyInput{}
  16. input.Bucket = "bucketname"
  17.  
  18. input.Policy = "your policy"
  19. output, err := obsClient.SetBucketPolicy(input)
  20. if err == nil {
  21. fmt.Printf("RequestId:%s\n", output.RequestId)
  22. } else if obsError, ok := err.(obs.ObsError); ok {
  23. fmt.Printf("Code:%s\n", obsError.Code)
  24. fmt.Printf("Message:%s\n", obsError.Message)
  25. }
  26. }

管理桶策略 - 图1 说明:

桶策略内容的具体格式(JSON格式字符串)请参考《对象存储服务API参考》。

获取桶策略

您可以通过ObsClient.GetBucketPolicy获取桶策略。示例代码如下:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. )
  6.  
  7. var ak = "*** Provide your Access Key ***"
  8. var sk = "*** Provide your Secret Key ***"
  9. var endpoint = "https://your-endpoint"
  10.  
  11. // 创建ObsClient结构体
  12. var obsClient, _ = obs.New(ak, sk, endpoint)
  13.  
  14. func main() {
  15. output, err := obsClient.GetBucketPolicy("bucketname")
  16. if err == nil {
  17. fmt.Printf("Policy:%d\n", output.Policy)
  18. } else if obsError, ok := err.(obs.ObsError); ok {
  19. fmt.Printf("Code:%s\n", obsError.Code)
  20. fmt.Printf("Message:%s\n", obsError.Message)
  21. }
  22. }

删除桶策略

您可以通过ObsClient.DeleteBucketPolicy删除桶策略。示例代码如下:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. )
  6.  
  7. var ak = "*** Provide your Access Key ***"
  8. var sk = "*** Provide your Secret Key ***"
  9. var endpoint = "https://your-endpoint"
  10.  
  11. // 创建ObsClient结构体
  12. var obsClient, _ = obs.New(ak, sk, endpoint)
  13.  
  14. func main() {
  15. output, err := obsClient.DeleteBucketPolicy("bucketname")
  16. if err == nil {
  17. fmt.Printf("RequestId:%s\n", output.RequestId)
  18. } else if obsError, ok := err.(obs.ObsError); ok {
  19. fmt.Printf("Code:%s\n", obsError.Code)
  20. fmt.Printf("Message:%s\n", obsError.Message)
  21. }
  22. }

父主题:管理桶