oss.go 6.8 KB

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