oss.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/services/alarm_msg"
  6. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7. "os"
  8. "time"
  9. "eta/eta_api/utils"
  10. "github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
  11. )
  12. // UploadAliyunV2 图片上传到阿里云
  13. func UploadAliyunV2(filename, filepath string) (string, error) {
  14. if utils.AccessKeyId == `` {
  15. return "0", errors.New("阿里云信息未配置")
  16. }
  17. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  18. if err != nil {
  19. return "1", err
  20. }
  21. bucket, err := client.Bucket(utils.Bucketname)
  22. if err != nil {
  23. return "2", err
  24. }
  25. path := utils.UploadDir + time.Now().Format("200601/20060102/")
  26. path += filename
  27. err = bucket.PutObjectFromFile(path, filepath)
  28. if err != nil {
  29. return "3", err
  30. }
  31. path = utils.Imghost + path
  32. return path, err
  33. }
  34. // UploadAudioAliyun 音频上传到阿里云
  35. func UploadAudioAliyun(filename, filepath string) (string, error) {
  36. if utils.AccessKeyId == `` {
  37. return "0", errors.New("阿里云信息未配置")
  38. }
  39. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  40. if err != nil {
  41. return "1", err
  42. }
  43. bucket, err := client.Bucket(utils.Bucketname)
  44. if err != nil {
  45. return "2", err
  46. }
  47. path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  48. path += filename
  49. err = bucket.PutObjectFromFile(path, filepath)
  50. if err != nil {
  51. return "3", err
  52. }
  53. path = utils.Imghost + path
  54. return path, err
  55. }
  56. // UploadVideoAliyun 视频上传到阿里云
  57. func UploadVideoAliyun(filename, filepath, savePath string) error {
  58. if utils.AccessKeyId == `` {
  59. return errors.New("阿里云信息未配置")
  60. }
  61. defer func() {
  62. os.Remove(filepath)
  63. }()
  64. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  65. if err != nil {
  66. return err
  67. }
  68. bucket, err := client.Bucket(utils.Bucketname)
  69. if err != nil {
  70. return err
  71. }
  72. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  73. //path += filename
  74. err = bucket.PutObjectFromFile(savePath, filepath)
  75. if err != nil {
  76. return err
  77. }
  78. //path = utils.Imghost + path
  79. //return path,err
  80. return err
  81. }
  82. // UploadFileToAliyun 上传文件到阿里云
  83. func UploadFileToAliyun(filename, filepath, savePath string) error {
  84. if utils.AccessKeyId == `` {
  85. return errors.New("阿里云信息未配置")
  86. }
  87. defer func() {
  88. os.Remove(filepath)
  89. }()
  90. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  91. if err != nil {
  92. return err
  93. }
  94. bucket, err := client.Bucket(utils.Bucketname)
  95. if err != nil {
  96. return err
  97. }
  98. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  99. //path += filename
  100. err = bucket.PutObjectFromFile(savePath, filepath)
  101. if err != nil {
  102. return err
  103. }
  104. //path = utils.Imghost + path
  105. //return path,err
  106. return err
  107. }
  108. type STSToken struct {
  109. AccessKeyId string
  110. AccessKeySecret string
  111. SecurityToken string
  112. ExpiredTime string
  113. RegionId string
  114. Bucketname string
  115. Endpoint string
  116. Imghost string
  117. }
  118. // GetOssSTSToken 获取STSToken
  119. func GetOssSTSToken() (item *STSToken, err error) {
  120. defer func() {
  121. if err != nil {
  122. utils.FileLog.Info(err.Error())
  123. go alarm_msg.SendAlarmMsg("获取STSToken失败, ErrMsg: "+err.Error(), 3)
  124. }
  125. }()
  126. item = new(STSToken)
  127. // 获取缓存中的Token
  128. recent, _ := utils.Rc.RedisString(utils.STSTokenCacheKey)
  129. if recent != "" {
  130. lastToken := new(STSToken)
  131. if e := json.Unmarshal([]byte(recent), &lastToken); e != nil {
  132. err = errors.New("GetOssSTSToken lastToken Unmarshal Err: " + e.Error())
  133. return
  134. }
  135. // 未防止正在上传大文件时Token过期, 将判定的过期时间提前10分钟
  136. afterTime := time.Now().Local().Add(10 * time.Minute)
  137. expired, e := time.ParseInLocation(utils.FormatDateTime, lastToken.ExpiredTime, time.Local)
  138. if e != nil {
  139. err = errors.New("GetOssSTSToken expiredTime Parse Err: " + e.Error())
  140. return
  141. }
  142. if expired.After(afterTime) {
  143. item.AccessKeyId = lastToken.AccessKeyId
  144. item.AccessKeySecret = lastToken.AccessKeySecret
  145. item.SecurityToken = lastToken.SecurityToken
  146. item.ExpiredTime = lastToken.ExpiredTime
  147. item.RegionId = utils.RegionId
  148. item.Bucketname = utils.Bucketname
  149. item.Endpoint = utils.Imghost
  150. item.Imghost = utils.Imghost
  151. return
  152. }
  153. }
  154. // 已过期则获取新的token
  155. newToken, e := NewSTSToken()
  156. if e != nil {
  157. err = errors.New("GetOssSTSToken NewSTSToken Err: " + e.Error())
  158. return
  159. }
  160. newTokenJson, e := json.Marshal(newToken)
  161. if e != nil {
  162. err = errors.New("GetOssSTSToken NewToken JSON Err: " + e.Error())
  163. return
  164. }
  165. // 覆盖缓存
  166. if e := utils.Rc.Put(utils.STSTokenCacheKey, newTokenJson, time.Hour); e != nil {
  167. err = errors.New("GetOssSTSToken SetRedis Err: " + e.Error())
  168. return
  169. }
  170. item = newToken
  171. return
  172. }
  173. // NewSTSToken 获取一个新的STSToken
  174. func NewSTSToken() (item *STSToken, err error) {
  175. defer func() {
  176. if err != nil {
  177. utils.FileLog.Info(err.Error())
  178. }
  179. }()
  180. item = new(STSToken)
  181. client, e := sts.NewClientWithAccessKey("cn-shanghai", utils.RAMAccessKeyId, utils.RAMAccessKeySecret)
  182. if e != nil {
  183. err = errors.New("NewSTSToken NewClient Err: " + e.Error())
  184. return
  185. }
  186. request := sts.CreateAssumeRoleRequest()
  187. request.Scheme = utils.AliStsScheme
  188. request.RegionId = utils.RegionId
  189. request.RoleArn = utils.RoleArn
  190. now := time.Now().Format(utils.FormatDateTimeUnSpace)
  191. request.RoleSessionName = utils.RoleSessionName + now
  192. request.DurationSeconds = "3600"
  193. request.ConnectTimeout = 300 * time.Second
  194. request.ReadTimeout = 300 * time.Second
  195. response, e := client.AssumeRole(request)
  196. if e != nil {
  197. err = errors.New("NewSTSToken AssumeRole Err: " + e.Error())
  198. return
  199. }
  200. if response != nil {
  201. item.AccessKeyId = response.Credentials.AccessKeyId
  202. item.AccessKeySecret = response.Credentials.AccessKeySecret
  203. item.SecurityToken = response.Credentials.SecurityToken
  204. t, _ := time.Parse(time.RFC3339, response.Credentials.Expiration)
  205. expiration := t.In(time.Local)
  206. item.ExpiredTime = expiration.Format(utils.FormatDateTime)
  207. item.RegionId = utils.RegionId
  208. item.Bucketname = utils.Bucketname
  209. item.Endpoint = utils.Imghost
  210. item.Imghost = utils.Imghost
  211. }
  212. return
  213. }
  214. // UploadAliyunToDir 上传至hzchart
  215. func UploadAliyunToDir(filename, filepath, uploadDir, fileDir string) (string, error) {
  216. if utils.AccessKeyId == `` {
  217. return "0", errors.New("阿里云信息未配置")
  218. }
  219. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  220. if err != nil {
  221. return "1", err
  222. }
  223. bucket, err := client.Bucket(utils.Bucketname)
  224. if err != nil {
  225. return "2", err
  226. }
  227. if uploadDir == "" {
  228. uploadDir = utils.UploadDir
  229. }
  230. if fileDir == "" {
  231. fileDir = time.Now().Format("200601/20060102/")
  232. }
  233. path := uploadDir + fileDir
  234. path += filename
  235. err = bucket.PutObjectFromFile(path, filepath)
  236. if err != nil {
  237. return "3", err
  238. }
  239. path = utils.Imghost + path
  240. return path, err
  241. }