oss.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. if utils.AccessKeyId == `` {
  51. return "0", errors.New("阿里云信息未配置")
  52. }
  53. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  54. if err != nil {
  55. return "1", err
  56. }
  57. bucket, err := client.Bucket(utils.Bucketname)
  58. if err != nil {
  59. return "2", err
  60. }
  61. path := utils.UploadDir + time.Now().Format("200601/20060102/")
  62. path += filename
  63. err = bucket.PutObjectFromFile(path, filepath)
  64. if err != nil {
  65. return "3", err
  66. }
  67. path = utils.Imghost + path
  68. return path, err
  69. }
  70. // UploadAudioAliyun 音频上传到阿里云
  71. func UploadAudioAliyun(filename, filepath string) (string, error) {
  72. if utils.AccessKeyId == `` {
  73. return "0", errors.New("阿里云信息未配置")
  74. }
  75. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  76. if err != nil {
  77. return "1", err
  78. }
  79. bucket, err := client.Bucket(utils.Bucketname)
  80. if err != nil {
  81. return "2", err
  82. }
  83. path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  84. path += filename
  85. err = bucket.PutObjectFromFile(path, filepath)
  86. if err != nil {
  87. return "3", err
  88. }
  89. path = utils.Imghost + path
  90. return path, err
  91. }
  92. // UploadVideoAliyun 视频上传到阿里云
  93. func UploadVideoAliyun(filename, filepath, savePath string) error {
  94. if utils.AccessKeyId == `` {
  95. return errors.New("阿里云信息未配置")
  96. }
  97. defer func() {
  98. os.Remove(filepath)
  99. }()
  100. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  101. if err != nil {
  102. return err
  103. }
  104. bucket, err := client.Bucket(utils.Bucketname)
  105. if err != nil {
  106. return err
  107. }
  108. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  109. //path += filename
  110. err = bucket.PutObjectFromFile(savePath, filepath)
  111. if err != nil {
  112. return err
  113. }
  114. //path = utils.Imghost + path
  115. //return path,err
  116. return err
  117. }
  118. // UploadFileToAliyun 上传文件到阿里云
  119. func UploadFileToAliyun(filename, filepath, savePath string) error {
  120. if utils.AccessKeyId == `` {
  121. return errors.New("阿里云信息未配置")
  122. }
  123. defer func() {
  124. os.Remove(filepath)
  125. }()
  126. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  127. if err != nil {
  128. return err
  129. }
  130. bucket, err := client.Bucket(utils.Bucketname)
  131. if err != nil {
  132. return err
  133. }
  134. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  135. //path += filename
  136. err = bucket.PutObjectFromFile(savePath, filepath)
  137. if err != nil {
  138. return err
  139. }
  140. //path = utils.Imghost + path
  141. //return path,err
  142. return err
  143. }
  144. type STSToken struct {
  145. AccessKeyId string
  146. AccessKeySecret string
  147. SecurityToken string
  148. ExpiredTime string
  149. RegionId string
  150. Bucketname string
  151. Endpoint string
  152. Imghost string
  153. }
  154. // GetOssSTSToken 获取STSToken
  155. func GetOssSTSToken() (item *STSToken, err error) {
  156. defer func() {
  157. if err != nil {
  158. utils.FileLog.Info(err.Error())
  159. go alarm_msg.SendAlarmMsg("获取STSToken失败, ErrMsg: "+err.Error(), 3)
  160. }
  161. }()
  162. item = new(STSToken)
  163. // 获取缓存中的Token
  164. recent, _ := utils.Rc.RedisString(utils.STSTokenCacheKey)
  165. if recent != "" {
  166. lastToken := new(STSToken)
  167. if e := json.Unmarshal([]byte(recent), &lastToken); e != nil {
  168. err = errors.New("GetOssSTSToken lastToken Unmarshal Err: " + e.Error())
  169. return
  170. }
  171. // 未防止正在上传大文件时Token过期, 将判定的过期时间提前10分钟
  172. afterTime := time.Now().Local().Add(10 * time.Minute)
  173. expired, e := time.ParseInLocation(utils.FormatDateTime, lastToken.ExpiredTime, time.Local)
  174. if e != nil {
  175. err = errors.New("GetOssSTSToken expiredTime Parse Err: " + e.Error())
  176. return
  177. }
  178. if expired.After(afterTime) {
  179. item.AccessKeyId = lastToken.AccessKeyId
  180. item.AccessKeySecret = lastToken.AccessKeySecret
  181. item.SecurityToken = lastToken.SecurityToken
  182. item.ExpiredTime = lastToken.ExpiredTime
  183. item.RegionId = utils.RegionId
  184. item.Bucketname = utils.Bucketname
  185. item.Endpoint = utils.Imghost
  186. item.Imghost = utils.Imghost
  187. return
  188. }
  189. }
  190. // 已过期则获取新的token
  191. newToken, e := NewSTSToken()
  192. if e != nil {
  193. err = errors.New("GetOssSTSToken NewSTSToken Err: " + e.Error())
  194. return
  195. }
  196. newTokenJson, e := json.Marshal(newToken)
  197. if e != nil {
  198. err = errors.New("GetOssSTSToken NewToken JSON Err: " + e.Error())
  199. return
  200. }
  201. // 覆盖缓存
  202. if e := utils.Rc.Put(utils.STSTokenCacheKey, newTokenJson, time.Hour); e != nil {
  203. err = errors.New("GetOssSTSToken SetRedis Err: " + e.Error())
  204. return
  205. }
  206. item = newToken
  207. return
  208. }
  209. // NewSTSToken 获取一个新的STSToken
  210. func NewSTSToken() (item *STSToken, err error) {
  211. defer func() {
  212. if err != nil {
  213. utils.FileLog.Info(err.Error())
  214. }
  215. }()
  216. item = new(STSToken)
  217. client, e := sts.NewClientWithAccessKey("cn-shanghai", utils.RAMAccessKeyId, utils.RAMAccessKeySecret)
  218. if e != nil {
  219. err = errors.New("NewSTSToken NewClient Err: " + e.Error())
  220. return
  221. }
  222. request := sts.CreateAssumeRoleRequest()
  223. request.Scheme = utils.AliStsScheme
  224. request.RegionId = utils.RegionId
  225. request.RoleArn = utils.RoleArn
  226. now := time.Now().Format(utils.FormatDateTimeUnSpace)
  227. request.RoleSessionName = utils.RoleSessionName + now
  228. request.DurationSeconds = "3600"
  229. request.ConnectTimeout = 300 * time.Second
  230. request.ReadTimeout = 300 * time.Second
  231. response, e := client.AssumeRole(request)
  232. if e != nil {
  233. err = errors.New("NewSTSToken AssumeRole Err: " + e.Error())
  234. return
  235. }
  236. if response != nil {
  237. item.AccessKeyId = response.Credentials.AccessKeyId
  238. item.AccessKeySecret = response.Credentials.AccessKeySecret
  239. item.SecurityToken = response.Credentials.SecurityToken
  240. t, _ := time.Parse(time.RFC3339, response.Credentials.Expiration)
  241. expiration := t.In(time.Local)
  242. item.ExpiredTime = expiration.Format(utils.FormatDateTime)
  243. item.RegionId = utils.RegionId
  244. item.Bucketname = utils.Bucketname
  245. item.Endpoint = utils.Imghost
  246. item.Imghost = utils.Imghost
  247. }
  248. return
  249. }
  250. // UploadAliyunToDir 上传至hzchart
  251. func UploadAliyunToDir(filename, filepath, uploadDir, fileDir string) (string, error) {
  252. if utils.AccessKeyId == `` {
  253. return "0", errors.New("阿里云信息未配置")
  254. }
  255. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  256. if err != nil {
  257. return "1", err
  258. }
  259. bucket, err := client.Bucket(utils.Bucketname)
  260. if err != nil {
  261. return "2", err
  262. }
  263. if uploadDir == "" {
  264. uploadDir = utils.UploadDir
  265. }
  266. if fileDir == "" {
  267. fileDir = time.Now().Format("200601/20060102/")
  268. }
  269. path := uploadDir + fileDir
  270. path += filename
  271. err = bucket.PutObjectFromFile(path, filepath)
  272. if err != nil {
  273. return "3", err
  274. }
  275. path = utils.Imghost + path
  276. return path, err
  277. }