bucket_policy.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package sample
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. // BucketPolicySample shows how to set, get and delete the bucket policy configuration
  7. func BucketPolicySample() {
  8. // New client
  9. client, err := oss.New(endpoint, accessID, accessKey)
  10. if err != nil {
  11. HandleError(err)
  12. }
  13. // Create the bucket with default parameters
  14. err = client.CreateBucket(bucketName)
  15. if err != nil {
  16. HandleError(err)
  17. }
  18. // the policy string
  19. var policyInfo string
  20. policyInfo = `
  21. {
  22. "Version":"1",
  23. "Statement":[
  24. {
  25. "Action":[
  26. "oss:GetObject",
  27. "oss:PutObject"
  28. ],
  29. "Effect":"Deny",
  30. "Principal":"[123456790]",
  31. "Resource":["acs:oss:*:1234567890:*/*"]
  32. }
  33. ]
  34. }`
  35. // Set policy
  36. err = client.SetBucketPolicy(bucketName, policyInfo)
  37. if err != nil {
  38. HandleError(err)
  39. }
  40. // Get Bucket policy
  41. ret, err := client.GetBucketPolicy(bucketName)
  42. if err != nil {
  43. HandleError(err)
  44. }
  45. fmt.Println("Bucket policy:", ret)
  46. // Delete Bucket policy
  47. err = client.DeleteBucketPolicy(bucketName)
  48. if err != nil {
  49. HandleError(err)
  50. }
  51. // Delete bucket
  52. err = client.DeleteBucket(bucketName)
  53. if err != nil {
  54. HandleError(err)
  55. }
  56. fmt.Println("BucketPolicySample completed")
  57. }