aliyun_oss.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package svc
  2. import (
  3. "fmt"
  4. "git.i2edu.net/i2/i2-bill-api/internal/utils"
  5. "git.i2edu.net/i2/i2-bill-api/model"
  6. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7. "io"
  8. "mime/multipart"
  9. "path"
  10. "strings"
  11. "time"
  12. )
  13. var AliYunOss *AliYunOssUpDownloader
  14. type AliYunOssUpDownloader struct {
  15. bucket *oss.Bucket
  16. }
  17. func GetAliYunOssInstance() (*AliYunOssUpDownloader, error) {
  18. var err error
  19. if AliYunOss == nil {
  20. AliYunOss, err = newAliYunOssUpDownloader()
  21. }
  22. return AliYunOss, err
  23. }
  24. func newAliYunOssUpDownloader() (*AliYunOssUpDownloader, error) {
  25. // 创建OSSClient实例。
  26. client, err := oss.New(ServiceConfig.AliYunOss.EndPoint, ServiceConfig.AliYunOss.KeyId, ServiceConfig.AliYunOss.Secret)
  27. if err != nil {
  28. fmt.Println("Error:", err)
  29. return nil, err
  30. }
  31. // 获取存储空间。
  32. aliYunBucket, err := client.Bucket(ServiceConfig.AliYunOss.Bucket)
  33. if err != nil {
  34. fmt.Println("Error:", err)
  35. return nil, err
  36. }
  37. return &AliYunOssUpDownloader{bucket: aliYunBucket}, nil
  38. }
  39. /**
  40. * @brief: 附件上传接口
  41. * @param1 file: post 多媒体file
  42. * @return1: 附件信息,主要包括id,url, hash和大小。其他字段会自动设置
  43. * @return4: 错误信息
  44. */
  45. func (aoud *AliYunOssUpDownloader) Upload(file multipart.File, header *multipart.FileHeader) (*model.I2billSysAttachment, error) {
  46. attachId := utils.GetUUID()
  47. dateStr := time.Now().Format("2006/01/02")
  48. ext := strings.TrimSpace(path.Ext(header.Filename)) // 扩展名
  49. objectKey := ""
  50. if ext == "" {
  51. objectKey = fmt.Sprintf("customer/files/att/%s/%s", dateStr, attachId)
  52. } else {
  53. objectKey = fmt.Sprintf("customer/files/att/%s/%s%s", dateStr, attachId, ext)
  54. }
  55. err := aoud.bucket.PutObject(objectKey, file)
  56. if err != nil {
  57. fmt.Println("aoud.bucket.PutObject err", err.Error())
  58. return nil, err
  59. }
  60. attach := &model.I2billSysAttachment{}
  61. attach.Id = attachId
  62. attach.Url = objectKey
  63. attach.Hash = ""
  64. attach.Ext = ext
  65. attach.Name = header.Filename
  66. attach.Size = header.Size
  67. return attach, nil
  68. }
  69. func (aoud *AliYunOssUpDownloader) UploadRead(file io.Reader, ext string) (*model.I2billSysAttachment, error) {
  70. attachId := utils.GetUUID()
  71. dateStr := time.Now().Format("2006/01/02")
  72. objectKey := ""
  73. if ext == "" {
  74. objectKey = fmt.Sprintf("customer/files/att/%s/%s", dateStr, attachId)
  75. } else {
  76. objectKey = fmt.Sprintf("customer/files/att/%s/%s%s", dateStr, attachId, ext)
  77. }
  78. err := aoud.bucket.PutObject(objectKey, file)
  79. if err != nil {
  80. fmt.Println("aoud.bucket.PutObject err", err.Error())
  81. return nil, err
  82. }
  83. attach := &model.I2billSysAttachment{}
  84. attach.Id = attachId
  85. attach.Url = objectKey
  86. attach.Hash = ""
  87. attach.Ext = ext
  88. attach.Name = attachId
  89. attach.CreateTime = time.Now()
  90. return attach, nil
  91. }
  92. func (aoud *AliYunOssUpDownloader) Download(attach *model.I2billSysAttachment) (io.ReadCloser, error) {
  93. objectKey := attach.Url
  94. r, err := aoud.bucket.GetObject(objectKey)
  95. if err != nil {
  96. fmt.Println("aoud.bucket.GetObject 错误", err.Error())
  97. return r, err
  98. }
  99. defer r.Close()
  100. return r, nil
  101. }