bucket_referer.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package sample
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. // BucketRefererSample shows how to set, get and delete the bucket referer.
  7. func BucketRefererSample() {
  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. var referers = []string{
  19. "http://www.aliyun.com",
  20. "http://www.???.aliyuncs.com",
  21. "http://www.*.com",
  22. }
  23. // Case 1: Set referers. The referers are with wildcards ? and * which could represent one and zero to multiple characters
  24. err = client.SetBucketReferer(bucketName, referers, false)
  25. if err != nil {
  26. HandleError(err)
  27. }
  28. // Case 2: Clear referers
  29. referers = []string{}
  30. err = client.SetBucketReferer(bucketName, referers, true)
  31. if err != nil {
  32. HandleError(err)
  33. }
  34. // Get bucket referer configuration
  35. gbr, err := client.GetBucketReferer(bucketName)
  36. if err != nil {
  37. HandleError(err)
  38. }
  39. fmt.Println("Bucket Referers:", gbr.RefererList,
  40. "AllowEmptyReferer:", gbr.AllowEmptyReferer)
  41. // Delete bucket
  42. err = client.DeleteBucket(bucketName)
  43. if err != nil {
  44. HandleError(err)
  45. }
  46. fmt.Println("BucketRefererSample completed")
  47. }