oss.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package oss
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mini_crm_ht/utils"
  6. "fmt"
  7. "github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
  8. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  9. "github.com/gabriel-vasile/mimetype"
  10. "io"
  11. "os"
  12. "strings"
  13. "time"
  14. )
  15. type STSToken struct {
  16. AccessKeyId string
  17. AccessKeySecret string
  18. SecurityToken string
  19. ExpiredTime string
  20. RegionId string
  21. Bucketname string
  22. Endpoint string
  23. Imghost string
  24. }
  25. type OssClient interface {
  26. UploadFile(string, string, string) (string, error)
  27. GetUploadToken() (OssToken, error)
  28. GetFile(filePath, savePath string) (path string, err error)
  29. GetExt(string) (string, error)
  30. MultiUploadFile() error
  31. }
  32. func NewOssClient() OssClient {
  33. switch utils.ObjectStorageClient {
  34. case utils.STORAGESOURCE_OSS_NAME:
  35. return new(AliOss)
  36. default:
  37. // 默认使用minio
  38. return new(MinioOss)
  39. }
  40. }
  41. // OssToken 此处为了兼容前端那边所以有重复的
  42. type OssToken struct {
  43. AccessKeyId string
  44. SecretKeyId string
  45. RegionId string
  46. Bucketname string
  47. Endpoint string
  48. ImgHost string
  49. UseSSL string
  50. Port string
  51. //AccessKeyId string
  52. AccessKeySecret string
  53. SecurityToken string
  54. ExpiredTime string
  55. //RegionId string
  56. //Bucketname string
  57. //Endpoint string
  58. Imghost string
  59. S3ForceStyle bool
  60. S3Protocol string
  61. }
  62. // GetOssSTSToken 获取STSToken
  63. func GetOssSTSToken() (item *STSToken, err error) {
  64. defer func() {
  65. if err != nil {
  66. utils.FileLog.Info(err.Error())
  67. }
  68. }()
  69. item = new(STSToken)
  70. // 获取缓存中的Token
  71. recent, _ := utils.Rc.RedisString(utils.STSTokenCacheKey)
  72. if recent != "" {
  73. lastToken := new(STSToken)
  74. if e := json.Unmarshal([]byte(recent), &lastToken); e != nil {
  75. err = errors.New("GetOssSTSToken lastToken Unmarshal Err: " + e.Error())
  76. return
  77. }
  78. // 未防止正在上传大文件时Token过期, 将判定的过期时间提前10分钟
  79. afterTime := time.Now().Local().Add(10 * time.Minute)
  80. expired, e := time.ParseInLocation(utils.FormatDateTime, lastToken.ExpiredTime, time.Local)
  81. if e != nil {
  82. err = errors.New("GetOssSTSToken expiredTime Parse Err: " + e.Error())
  83. return
  84. }
  85. if expired.After(afterTime) {
  86. item.AccessKeyId = lastToken.AccessKeyId
  87. item.AccessKeySecret = lastToken.AccessKeySecret
  88. item.SecurityToken = lastToken.SecurityToken
  89. item.ExpiredTime = lastToken.ExpiredTime
  90. item.RegionId = utils.RegionId
  91. item.Bucketname = utils.Bucketname
  92. item.Endpoint = utils.Imghost
  93. item.Imghost = utils.Imghost
  94. return
  95. }
  96. }
  97. // 已过期则获取新的token
  98. newToken, e := NewSTSToken()
  99. if e != nil {
  100. err = errors.New("GetOssSTSToken NewSTSToken Err: " + e.Error())
  101. return
  102. }
  103. newTokenJson, e := json.Marshal(newToken)
  104. if e != nil {
  105. err = errors.New("GetOssSTSToken NewToken JSON Err: " + e.Error())
  106. return
  107. }
  108. // 覆盖缓存
  109. if e := utils.Rc.Put(utils.STSTokenCacheKey, newTokenJson, time.Hour); e != nil {
  110. err = errors.New("GetOssSTSToken SetRedis Err: " + e.Error())
  111. return
  112. }
  113. item = newToken
  114. return
  115. }
  116. // NewSTSToken 获取一个新的STSToken
  117. func NewSTSToken() (item *STSToken, err error) {
  118. defer func() {
  119. if err != nil {
  120. utils.FileLog.Info(err.Error())
  121. }
  122. }()
  123. item = new(STSToken)
  124. client, e := sts.NewClientWithAccessKey("cn-shanghai", utils.RAMAccessKeyId, utils.RAMAccessKeySecret)
  125. if e != nil {
  126. err = errors.New("NewSTSToken NewClient Err: " + e.Error())
  127. return
  128. }
  129. request := sts.CreateAssumeRoleRequest()
  130. request.Scheme = utils.AliStsScheme
  131. request.RegionId = utils.RegionId
  132. request.RoleArn = utils.RoleArn
  133. now := time.Now().Format(utils.FormatDateTimeUnSpace)
  134. request.RoleSessionName = utils.RoleSessionName + now
  135. request.DurationSeconds = "3600"
  136. request.ConnectTimeout = 300 * time.Second
  137. request.ReadTimeout = 300 * time.Second
  138. response, e := client.AssumeRole(request)
  139. if e != nil {
  140. err = errors.New("NewSTSToken AssumeRole Err: " + e.Error())
  141. return
  142. }
  143. if response != nil {
  144. item.AccessKeyId = response.Credentials.AccessKeyId
  145. item.AccessKeySecret = response.Credentials.AccessKeySecret
  146. item.SecurityToken = response.Credentials.SecurityToken
  147. t, _ := time.Parse(time.RFC3339, response.Credentials.Expiration)
  148. expiration := t.In(time.Local)
  149. item.ExpiredTime = expiration.Format(utils.FormatDateTime)
  150. item.RegionId = utils.RegionId
  151. item.Bucketname = utils.Bucketname
  152. item.Endpoint = utils.Imghost
  153. item.Imghost = utils.Imghost
  154. }
  155. return
  156. }
  157. type AliOss struct{}
  158. func (m *AliOss) GetExt(ossPath string) (ext string, err error) {
  159. if utils.AccessKeyId == `` {
  160. return "0", errors.New("阿里云信息未配置")
  161. }
  162. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  163. if err != nil {
  164. return
  165. }
  166. bucket, err := client.Bucket(utils.Bucketname)
  167. if err != nil {
  168. return
  169. }
  170. objectKey := ossPath[strings.Index(ossPath, utils.RESOURCE_DIR):]
  171. metaInfo, err := bucket.GetObjectMeta(objectKey)
  172. if err != nil {
  173. return
  174. }
  175. ext = GetFileExtensionFromContentType(metaInfo["Content-Type"][0])
  176. return
  177. }
  178. // UploadFile 上传文件
  179. func (m *AliOss) UploadFile(fileName, filePath, savePath string) (string, error) {
  180. if utils.AccessKeyId == `` {
  181. return "0", errors.New("阿里云信息未配置")
  182. }
  183. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  184. if err != nil {
  185. return "1", err
  186. }
  187. bucket, err := client.Bucket(utils.Bucketname)
  188. if err != nil {
  189. return "2", err
  190. }
  191. path := savePath
  192. if savePath == "" {
  193. path = utils.UploadDir + time.Now().Format("200601/20060102/") + fileName
  194. }
  195. err = bucket.PutObjectFromFile(path, filePath)
  196. if err != nil {
  197. return "3", err
  198. }
  199. resourceUrl := utils.Imghost + path
  200. return resourceUrl, err
  201. }
  202. func (m *AliOss) GetUploadToken() (token OssToken, err error) {
  203. stsToken, e := GetOssSTSToken()
  204. if e != nil {
  205. err = fmt.Errorf("GetOssSTSToken err: %s", e.Error())
  206. return
  207. }
  208. token.AccessKeyId = stsToken.AccessKeyId
  209. token.AccessKeySecret = stsToken.AccessKeySecret
  210. token.SecurityToken = stsToken.SecurityToken
  211. token.ExpiredTime = stsToken.ExpiredTime
  212. token.RegionId = stsToken.RegionId
  213. token.Bucketname = stsToken.Bucketname
  214. token.Endpoint = stsToken.Endpoint
  215. token.Imghost = stsToken.Imghost
  216. return
  217. }
  218. func (m *AliOss) MultiUploadFile() (err error) {
  219. return
  220. }
  221. func (m *AliOss) GetFile(filePath, savePath string) (path string, err error) {
  222. if utils.AccessKeyId == `` {
  223. return "", errors.New("阿里云信息未配置")
  224. }
  225. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  226. if err != nil {
  227. return
  228. }
  229. bucket, err := client.Bucket(utils.Bucketname)
  230. if err != nil {
  231. return
  232. }
  233. objectKey := filePath[strings.Index(filePath, utils.RESOURCE_DIR):]
  234. reader, err := bucket.GetObject(objectKey)
  235. if err != nil {
  236. return
  237. }
  238. buffer := make([]byte, 512)
  239. n, err := reader.Read(buffer)
  240. if err != nil && err != io.EOF {
  241. return "", fmt.Errorf("Error reading object: %s", err)
  242. }
  243. mime := mimetype.Detect(buffer[:n])
  244. fileExt := mime.Extension()
  245. path = savePath + fileExt
  246. defer reader.Close()
  247. // 打开或创建本地文件
  248. localFile, err := os.Create(path)
  249. if err != nil {
  250. utils.FileLog.Info("处理原始图片信息失败:Error create local dir: %s", err)
  251. return
  252. }
  253. defer localFile.Close()
  254. // 将io.ReadCloser中的数据复制到本地文件
  255. _, err = localFile.Write(buffer[:n])
  256. if err != nil {
  257. utils.FileLog.Info("处理原始图片信息失败:Error writing to local file: %s", err)
  258. return
  259. }
  260. _, err = io.Copy(localFile, reader)
  261. return
  262. }
  263. func GetFileExtensionFromContentType(contentType string) string {
  264. switch contentType {
  265. case "image/jpeg":
  266. return ".jpg"
  267. case "image/png":
  268. return ".png"
  269. case "application/pdf":
  270. return ".pdf"
  271. case "application/msword":
  272. return ".doc"
  273. case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
  274. return ".docx"
  275. case "application/vnd.ms-excel":
  276. return ".xls"
  277. case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
  278. return ".xlsx"
  279. case "text/plain":
  280. return ".txt"
  281. case "application/json":
  282. return ".json"
  283. case "application/octet-stream":
  284. return ".bin"
  285. default:
  286. return ""
  287. }
  288. }