comm.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 sample中的错误处理
  14. func HandleError(err error) {
  15. fmt.Println("occurred error:", err)
  16. os.Exit(-1)
  17. }
  18. // GetTestBucket 创建sample的Bucket并返回OssBucket对象,该函数为了简化sample,让sample代码更明了
  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. // DeleteTestBucketAndObject 删除sample的object和bucket,该函数为了简化sample,让sample代码更明了
  38. func DeleteTestBucketAndObject(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. // Delete Part
  50. lmur, err := bucket.ListMultipartUploads()
  51. if err != nil {
  52. return err
  53. }
  54. for _, upload := range lmur.Uploads {
  55. var imur = oss.InitiateMultipartUploadResult{Bucket: bucket.BucketName,
  56. Key: upload.Key, UploadID: upload.UploadID}
  57. err = bucket.AbortMultipartUpload(imur)
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. // Delete Objects
  63. lor, err := bucket.ListObjects()
  64. if err != nil {
  65. return err
  66. }
  67. for _, object := range lor.Objects {
  68. err = bucket.DeleteObject(object.Key)
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. // Delete Bucket
  74. err = client.DeleteBucket(bucketName)
  75. if err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. // Object pair of key and value
  81. type Object struct {
  82. Key string
  83. Value string
  84. }
  85. // CreateObjects 创建一组对象,该函数为了简化sample,让sample代码更明了
  86. func CreateObjects(bucket *oss.Bucket, objects []Object) error {
  87. for _, object := range objects {
  88. err := bucket.PutObject(object.Key, strings.NewReader(object.Value))
  89. if err != nil {
  90. return err
  91. }
  92. }
  93. return nil
  94. }
  95. // DeleteObjects 删除sample的object和bucket,该函数为了简化sample,让sample代码更明了
  96. func DeleteObjects(bucket *oss.Bucket, objects []Object) error {
  97. for _, object := range objects {
  98. err := bucket.DeleteObject(object.Key)
  99. if err != nil {
  100. return err
  101. }
  102. }
  103. return nil
  104. }