bucket_requestpayment.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package sample
  2. import (
  3. "fmt"
  4. "strings"
  5. "io/ioutil"
  6. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7. )
  8. // BucketrRequestPaymentSample shows how to set, get the bucket request payment.
  9. func BucketrRequestPaymentSample() {
  10. // New client
  11. client, err := oss.New(endpoint, accessID, accessKey)
  12. if err != nil {
  13. HandleError(err)
  14. }
  15. // Create the bucket with default parameters
  16. err = client.CreateBucket(bucketName)
  17. if err != nil {
  18. HandleError(err)
  19. }
  20. reqPayConf := oss.RequestPaymentConfiguration{
  21. Payer: string(oss.Requester),
  22. }
  23. // Case 1: Set bucket request payment.
  24. err = client.SetBucketRequestPayment(bucketName, reqPayConf)
  25. if err != nil {
  26. HandleError(err)
  27. }
  28. // Get bucket request payment configuration
  29. ret, err := client.GetBucketRequestPayment(bucketName)
  30. if err != nil {
  31. HandleError(err)
  32. }
  33. fmt.Println("Bucket request payer:", ret.Payer)
  34. if credentialUID == "" {
  35. fmt.Println("Please enter a credential User ID, if you want to test credential user.")
  36. clearData(client, bucketName)
  37. return
  38. }
  39. // Credential other User
  40. policyInfo := `
  41. {
  42. "Version":"1",
  43. "Statement":[
  44. {
  45. "Action":[
  46. "oss:*"
  47. ],
  48. "Effect":"Allow",
  49. "Principal":["` + credentialUID + `"],
  50. "Resource":["acs:oss:*:*:` + bucketName + `", "acs:oss:*:*:` + bucketName + `/*"]
  51. }
  52. ]
  53. }`
  54. err = client.SetBucketPolicy(bucketName, policyInfo)
  55. if err != nil {
  56. HandleError(err)
  57. }
  58. // New a Credential client
  59. creClient, err := oss.New(endpoint, credentialAccessID, credentialAccessKey)
  60. if err != nil {
  61. HandleError(err)
  62. }
  63. // Get credential bucket
  64. creBucket, err := creClient.Bucket(bucketName)
  65. if err != nil {
  66. HandleError(err)
  67. }
  68. // Put object by credential User
  69. key := "testCredentialObject"
  70. objectValue := "this is a test string."
  71. // Put object
  72. err = creBucket.PutObject(key, strings.NewReader(objectValue), oss.RequestPayer(oss.Requester))
  73. if err != nil {
  74. HandleError(err)
  75. }
  76. // Get object
  77. body, err := creBucket.GetObject(key, oss.RequestPayer(oss.Requester))
  78. if err != nil {
  79. HandleError(err)
  80. }
  81. defer body.Close()
  82. data, err := ioutil.ReadAll(body)
  83. if err != nil {
  84. HandleError(err)
  85. }
  86. fmt.Println(string(data))
  87. // Delete object
  88. err = creBucket.DeleteObject(key, oss.RequestPayer(oss.Requester))
  89. if err != nil {
  90. HandleError(err)
  91. }
  92. clearData(client, bucketName)
  93. }
  94. func clearData(client *oss.Client, bucketName string) {
  95. // Delete bucket
  96. err := client.DeleteBucket(bucketName)
  97. if err != nil {
  98. HandleError(err)
  99. }
  100. fmt.Println("BucketrRequestPaymentSample completed")
  101. }