ali_oss.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package oss
  2. import (
  3. "errors"
  4. "eta_gn/eta_obs/global"
  5. "eta_gn/eta_obs/services/alarm_msg"
  6. "fmt"
  7. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  8. "time"
  9. "eta_gn/eta_obs/utils"
  10. "github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
  11. )
  12. type STSToken struct {
  13. AccessKeyId string
  14. AccessKeySecret string
  15. SecurityToken string
  16. ExpiredTime string
  17. RegionId string
  18. Bucketname string
  19. Endpoint string
  20. Imghost string
  21. }
  22. // GetOssSTSToken 获取STSToken
  23. func GetOssSTSToken() (item *STSToken, err error) {
  24. defer func() {
  25. if err != nil {
  26. global.FILE_LOG.Info(err.Error())
  27. go alarm_msg.SendAlarmMsg("获取STSToken失败, ErrMsg: "+err.Error(), 3)
  28. }
  29. }()
  30. item = new(STSToken)
  31. // 已过期则获取新的token
  32. newToken, e := NewSTSToken()
  33. if e != nil {
  34. err = errors.New("GetOssSTSToken NewSTSToken Err: " + e.Error())
  35. return
  36. }
  37. item = newToken
  38. return
  39. }
  40. // NewSTSToken 获取一个新的STSToken
  41. func NewSTSToken() (item *STSToken, err error) {
  42. defer func() {
  43. if err != nil {
  44. global.FILE_LOG.Info("获取阿里云NewSTSToken失败", err.Error())
  45. }
  46. }()
  47. item = new(STSToken)
  48. client, e := sts.NewClientWithAccessKey(global.CONFIG.Oss.RegionId, global.CONFIG.Oss.Ram.AccessKeyId, global.CONFIG.Oss.Ram.AccessKeySecret)
  49. if e != nil {
  50. err = errors.New("NewSTSToken NewClient Err: " + e.Error())
  51. return
  52. }
  53. request := sts.CreateAssumeRoleRequest()
  54. request.Scheme = global.CONFIG.Oss.Ram.StsScheme
  55. request.RegionId = global.CONFIG.Oss.RegionId
  56. request.RoleArn = global.CONFIG.Oss.Ram.RoleArn
  57. now := time.Now().Format(utils.FormatDateTimeUnSpace)
  58. request.RoleSessionName = global.CONFIG.Oss.Ram.RoleSessionName + now
  59. request.DurationSeconds = "3600"
  60. request.ConnectTimeout = 300 * time.Second
  61. request.ReadTimeout = 300 * time.Second
  62. response, e := client.AssumeRole(request)
  63. if e != nil {
  64. err = errors.New("NewSTSToken AssumeRole Err: " + e.Error())
  65. return
  66. }
  67. if response != nil {
  68. item.AccessKeyId = response.Credentials.AccessKeyId
  69. item.AccessKeySecret = response.Credentials.AccessKeySecret
  70. item.SecurityToken = response.Credentials.SecurityToken
  71. t, _ := time.Parse(time.RFC3339, response.Credentials.Expiration)
  72. expiration := t.In(time.Local)
  73. item.ExpiredTime = expiration.Format(utils.FormatDateTime)
  74. item.RegionId = global.CONFIG.Oss.RegionId
  75. item.Bucketname = global.CONFIG.Oss.BucketName
  76. item.Endpoint = global.CONFIG.Oss.EndPoint
  77. item.Imghost = global.CONFIG.Oss.ImgHost
  78. }
  79. return
  80. }
  81. // UploadAliyunToDir 上传至hzchart
  82. //func UploadAliyunToDir(filename, filepath, uploadDir, fileDir string) (string, error) {
  83. // if utils.AccessKeyId == `` {
  84. // return "0", errors.New("阿里云信息未配置")
  85. // }
  86. // client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  87. // if err != nil {
  88. // return "1", err
  89. // }
  90. // bucket, err := client.Bucket(utils.Bucketname)
  91. // if err != nil {
  92. // return "2", err
  93. // }
  94. // if uploadDir == "" {
  95. // uploadDir = utils.UploadDir
  96. // }
  97. // if fileDir == "" {
  98. // fileDir = time.Now().Format("200601/20060102/")
  99. // }
  100. // path := uploadDir + fileDir
  101. // path += filename
  102. // err = bucket.PutObjectFromFile(path, filepath)
  103. // if err != nil {
  104. // return "3", err
  105. // }
  106. // path = utils.Imghost + path
  107. // return path, err
  108. //}
  109. type AliOss struct{}
  110. // UploadFile 上传文件
  111. func (m *AliOss) UploadFile(fileName, filePath, savePath string) (string, error) {
  112. if global.CONFIG.Oss.AccessKeyId == `` {
  113. return "0", errors.New("阿里云信息未配置")
  114. }
  115. client, err := oss.New(global.CONFIG.Oss.EndPoint, global.CONFIG.Oss.AccessKeyId, global.CONFIG.Oss.AccessKeySecret)
  116. if err != nil {
  117. return "1", err
  118. }
  119. bucket, err := client.Bucket(global.CONFIG.Oss.BucketName)
  120. if err != nil {
  121. return "2", err
  122. }
  123. path := savePath
  124. if savePath == "" {
  125. path = global.CONFIG.Oss.UploadDir + time.Now().Format("200601/20060102/") + fileName
  126. }
  127. err = bucket.PutObjectFromFile(path, filePath)
  128. if err != nil {
  129. return "3", err
  130. }
  131. resourceUrl := global.CONFIG.Oss.ImgHost + path
  132. return resourceUrl, err
  133. }
  134. func (m *AliOss) GetUploadToken() (token OssToken, err error) {
  135. stsToken, e := GetOssSTSToken()
  136. if e != nil {
  137. err = fmt.Errorf("GetOssSTSToken err: %s", e.Error())
  138. return
  139. }
  140. token.AccessKeyId = stsToken.AccessKeyId
  141. token.AccessKeySecret = stsToken.AccessKeySecret
  142. token.SecurityToken = stsToken.SecurityToken
  143. token.ExpiredTime = stsToken.ExpiredTime
  144. token.RegionId = stsToken.RegionId
  145. token.Bucketname = stsToken.Bucketname
  146. token.Endpoint = stsToken.Endpoint
  147. token.Imghost = stsToken.Imghost
  148. return
  149. }