comm.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package sample
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  8. )
  9. var (
  10. pastDate = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  11. futureDate = time.Date(2049, time.January, 10, 23, 0, 0, 0, time.UTC)
  12. )
  13. // HandleError is the error handling method in the sample code
  14. func HandleError(err error) {
  15. fmt.Println("occurred error:", err)
  16. os.Exit(-1)
  17. }
  18. // GetTestBucket creates the test bucket
  19. func GetTestBucket(bucketName string) (*oss.Bucket, error) {
  20. // New client
  21. client, err := oss.New(endpoint, accessID, accessKey)
  22. if err != nil {
  23. return nil, err
  24. }
  25. // Create bucket
  26. err = client.CreateBucket(bucketName)
  27. if err != nil {
  28. return nil, err
  29. }
  30. // Get bucket
  31. bucket, err := client.Bucket(bucketName)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return bucket, nil
  36. }
  37. // DeleteTestBucketAndLiveChannel 删除sample的channelname和bucket,该函数为了简化sample,让sample代码更明了
  38. func DeleteTestBucketAndLiveChannel(bucketName string) error {
  39. // New Client
  40. client, err := oss.New(endpoint, accessID, accessKey)
  41. if err != nil {
  42. return err
  43. }
  44. // Get Bucket
  45. bucket, err := client.Bucket(bucketName)
  46. if err != nil {
  47. return err
  48. }
  49. marker := ""
  50. for {
  51. result, err := bucket.ListLiveChannel(oss.Marker(marker))
  52. if err != nil {
  53. HandleError(err)
  54. }
  55. for _, channel := range result.LiveChannel {
  56. err := bucket.DeleteLiveChannel(channel.Name)
  57. if err != nil {
  58. HandleError(err)
  59. }
  60. }
  61. if result.IsTruncated {
  62. marker = result.NextMarker
  63. } else {
  64. break
  65. }
  66. }
  67. // Delete Bucket
  68. err = client.DeleteBucket(bucketName)
  69. if err != nil {
  70. return err
  71. }
  72. return nil
  73. }
  74. // DeleteTestBucketAndObject deletes the test bucket and its objects
  75. func DeleteTestBucketAndObject(bucketName string) error {
  76. // New client
  77. client, err := oss.New(endpoint, accessID, accessKey)
  78. if err != nil {
  79. return err
  80. }
  81. // Get bucket
  82. bucket, err := client.Bucket(bucketName)
  83. if err != nil {
  84. return err
  85. }
  86. // Delete part
  87. lmur, err := bucket.ListMultipartUploads()
  88. if err != nil {
  89. return err
  90. }
  91. for _, upload := range lmur.Uploads {
  92. var imur = oss.InitiateMultipartUploadResult{Bucket: bucket.BucketName,
  93. Key: upload.Key, UploadID: upload.UploadID}
  94. err = bucket.AbortMultipartUpload(imur)
  95. if err != nil {
  96. return err
  97. }
  98. }
  99. // Delete objects
  100. lor, err := bucket.ListObjects()
  101. if err != nil {
  102. return err
  103. }
  104. for _, object := range lor.Objects {
  105. err = bucket.DeleteObject(object.Key)
  106. if err != nil {
  107. return err
  108. }
  109. }
  110. // Delete bucket
  111. err = client.DeleteBucket(bucketName)
  112. if err != nil {
  113. return err
  114. }
  115. return nil
  116. }
  117. // Object defines pair of key and value
  118. type Object struct {
  119. Key string
  120. Value string
  121. }
  122. // CreateObjects creates some objects
  123. func CreateObjects(bucket *oss.Bucket, objects []Object) error {
  124. for _, object := range objects {
  125. err := bucket.PutObject(object.Key, strings.NewReader(object.Value))
  126. if err != nil {
  127. return err
  128. }
  129. }
  130. return nil
  131. }
  132. // DeleteObjects deletes some objects.
  133. func DeleteObjects(bucket *oss.Bucket, objects []Object) error {
  134. for _, object := range objects {
  135. err := bucket.DeleteObject(object.Key)
  136. if err != nil {
  137. return err
  138. }
  139. }
  140. return nil
  141. }