comm.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. keyMarker := oss.KeyMarker("")
  88. uploadIDMarker := oss.UploadIDMarker("")
  89. for {
  90. lmur, err := bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  91. if err != nil {
  92. return err
  93. }
  94. for _, upload := range lmur.Uploads {
  95. var imur = oss.InitiateMultipartUploadResult{Bucket: bucket.BucketName,
  96. Key: upload.Key, UploadID: upload.UploadID}
  97. err = bucket.AbortMultipartUpload(imur)
  98. if err != nil {
  99. return err
  100. }
  101. }
  102. keyMarker = oss.KeyMarker(lmur.NextKeyMarker)
  103. uploadIDMarker = oss.UploadIDMarker(lmur.NextUploadIDMarker)
  104. if !lmur.IsTruncated {
  105. break
  106. }
  107. }
  108. // Delete objects
  109. marker := oss.Marker("")
  110. for {
  111. lor, err := bucket.ListObjects(marker)
  112. if err != nil {
  113. return err
  114. }
  115. for _, object := range lor.Objects {
  116. err = bucket.DeleteObject(object.Key)
  117. if err != nil {
  118. return err
  119. }
  120. }
  121. marker = oss.Marker(lor.NextMarker)
  122. if !lor.IsTruncated {
  123. break
  124. }
  125. }
  126. // Delete bucket
  127. err = client.DeleteBucket(bucketName)
  128. if err != nil {
  129. return err
  130. }
  131. return nil
  132. }
  133. // Object defines pair of key and value
  134. type Object struct {
  135. Key string
  136. Value string
  137. }
  138. // CreateObjects creates some objects
  139. func CreateObjects(bucket *oss.Bucket, objects []Object) error {
  140. for _, object := range objects {
  141. err := bucket.PutObject(object.Key, strings.NewReader(object.Value))
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. return nil
  147. }
  148. // DeleteObjects deletes some objects.
  149. func DeleteObjects(bucket *oss.Bucket, objects []Object) error {
  150. for _, object := range objects {
  151. err := bucket.DeleteObject(object.Key)
  152. if err != nil {
  153. return err
  154. }
  155. }
  156. return nil
  157. }