oss.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/services/alarm_msg"
  6. "fmt"
  7. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  8. "time"
  9. "eta_gn/eta_api/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. func GetOssSTSToken() (item *STSToken, err error) {
  23. defer func() {
  24. if err != nil {
  25. utils.FileLog.Info(err.Error())
  26. go alarm_msg.SendAlarmMsg("获取STSToken失败, ErrMsg: "+err.Error(), 3)
  27. }
  28. }()
  29. item = new(STSToken)
  30. recent, _ := utils.Rc.RedisString(utils.STSTokenCacheKey)
  31. if recent != "" {
  32. lastToken := new(STSToken)
  33. if e := json.Unmarshal([]byte(recent), &lastToken); e != nil {
  34. err = errors.New("GetOssSTSToken lastToken Unmarshal Err: " + e.Error())
  35. return
  36. }
  37. afterTime := time.Now().Local().Add(10 * time.Minute)
  38. expired, e := time.ParseInLocation(utils.FormatDateTime, lastToken.ExpiredTime, time.Local)
  39. if e != nil {
  40. err = errors.New("GetOssSTSToken expiredTime Parse Err: " + e.Error())
  41. return
  42. }
  43. if expired.After(afterTime) {
  44. item.AccessKeyId = lastToken.AccessKeyId
  45. item.AccessKeySecret = lastToken.AccessKeySecret
  46. item.SecurityToken = lastToken.SecurityToken
  47. item.ExpiredTime = lastToken.ExpiredTime
  48. item.RegionId = utils.RegionId
  49. item.Bucketname = utils.Bucketname
  50. item.Endpoint = utils.Imghost
  51. item.Imghost = utils.Imghost
  52. return
  53. }
  54. }
  55. newToken, e := NewSTSToken()
  56. if e != nil {
  57. err = errors.New("GetOssSTSToken NewSTSToken Err: " + e.Error())
  58. return
  59. }
  60. newTokenJson, e := json.Marshal(newToken)
  61. if e != nil {
  62. err = errors.New("GetOssSTSToken NewToken JSON Err: " + e.Error())
  63. return
  64. }
  65. if e := utils.Rc.Put(utils.STSTokenCacheKey, newTokenJson, time.Hour); e != nil {
  66. err = errors.New("GetOssSTSToken SetRedis Err: " + e.Error())
  67. return
  68. }
  69. item = newToken
  70. return
  71. }
  72. func NewSTSToken() (item *STSToken, err error) {
  73. defer func() {
  74. if err != nil {
  75. utils.FileLog.Info(err.Error())
  76. }
  77. }()
  78. item = new(STSToken)
  79. client, e := sts.NewClientWithAccessKey("cn-shanghai", utils.RAMAccessKeyId, utils.RAMAccessKeySecret)
  80. if e != nil {
  81. err = errors.New("NewSTSToken NewClient Err: " + e.Error())
  82. return
  83. }
  84. request := sts.CreateAssumeRoleRequest()
  85. request.Scheme = utils.AliStsScheme
  86. request.RegionId = utils.RegionId
  87. request.RoleArn = utils.RoleArn
  88. now := time.Now().Format(utils.FormatDateTimeUnSpace)
  89. request.RoleSessionName = utils.RoleSessionName + now
  90. request.DurationSeconds = "3600"
  91. request.ConnectTimeout = 300 * time.Second
  92. request.ReadTimeout = 300 * time.Second
  93. response, e := client.AssumeRole(request)
  94. if e != nil {
  95. err = errors.New("NewSTSToken AssumeRole Err: " + e.Error())
  96. return
  97. }
  98. if response != nil {
  99. item.AccessKeyId = response.Credentials.AccessKeyId
  100. item.AccessKeySecret = response.Credentials.AccessKeySecret
  101. item.SecurityToken = response.Credentials.SecurityToken
  102. t, _ := time.Parse(time.RFC3339, response.Credentials.Expiration)
  103. expiration := t.In(time.Local)
  104. item.ExpiredTime = expiration.Format(utils.FormatDateTime)
  105. item.RegionId = utils.RegionId
  106. item.Bucketname = utils.Bucketname
  107. item.Endpoint = utils.Imghost
  108. item.Imghost = utils.Imghost
  109. }
  110. return
  111. }
  112. type AliOss struct{}
  113. func (m *AliOss) UploadFile(fileName, filePath, savePath string) (string, error) {
  114. if utils.AccessKeyId == `` {
  115. return "0", errors.New("阿里云信息未配置")
  116. }
  117. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  118. if err != nil {
  119. return "1", err
  120. }
  121. bucket, err := client.Bucket(utils.Bucketname)
  122. if err != nil {
  123. return "2", err
  124. }
  125. path := savePath
  126. if savePath == "" {
  127. path = utils.UploadDir + time.Now().Format("200601/20060102/") + fileName
  128. }
  129. err = bucket.PutObjectFromFile(path, filePath)
  130. if err != nil {
  131. return "3", err
  132. }
  133. resourceUrl := utils.Imghost + path
  134. return resourceUrl, err
  135. }
  136. func (m *AliOss) GetUploadToken() (token OssToken, err error) {
  137. stsToken, e := GetOssSTSToken()
  138. if e != nil {
  139. err = fmt.Errorf("GetOssSTSToken err: %s", e.Error())
  140. return
  141. }
  142. token.AccessKeyId = stsToken.AccessKeyId
  143. token.AccessKeySecret = stsToken.AccessKeySecret
  144. token.SecurityToken = stsToken.SecurityToken
  145. token.ExpiredTime = stsToken.ExpiredTime
  146. token.RegionId = stsToken.RegionId
  147. token.Bucketname = stsToken.Bucketname
  148. token.Endpoint = stsToken.Endpoint
  149. token.Imghost = stsToken.Imghost
  150. return
  151. }