oss.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package services
  2. import (
  3. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  4. "os"
  5. "time"
  6. "hongze/hongze_mfyx/utils"
  7. )
  8. /*
  9. 上传demo
  10. func init() {
  11. fmt.Println("start")
  12. randStr := utils.GetRandStringNoSpecialChar(28)
  13. fileName := randStr + ".jpg"
  14. fmt.Println("fileName:",fileName)
  15. fpath:="./1.png"
  16. resourceUrl,err:=UploadAliyun(fileName,fpath)
  17. if err!=nil {
  18. fmt.Println("UploadAliyun Err:",err.Error())
  19. return
  20. }
  21. fmt.Println("resourceUrl:",resourceUrl)
  22. fmt.Println("end")
  23. }
  24. */
  25. // 图片上传到阿里云
  26. func UploadAliyun(filename, filepath string) (string, error) {
  27. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  28. if err != nil {
  29. return "1", err
  30. }
  31. bucket, err := client.Bucket(utils.Bucketname)
  32. if err != nil {
  33. return "2", err
  34. }
  35. path := utils.Upload_dir + time.Now().Format("200601/20060102/")
  36. path += filename
  37. err = bucket.PutObjectFromFile(path, filepath)
  38. if err != nil {
  39. return "3", err
  40. }
  41. path = utils.Imghost + path
  42. return path, err
  43. }
  44. // 音频上传到阿里云
  45. func UploadAudioAliyun(filename, filepath string) (string, error) {
  46. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  47. if err != nil {
  48. return "1", err
  49. }
  50. bucket, err := client.Bucket(utils.Bucketname)
  51. if err != nil {
  52. return "2", err
  53. }
  54. path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  55. path += filename
  56. err = bucket.PutObjectFromFile(path, filepath)
  57. if err != nil {
  58. return "3", err
  59. }
  60. path = utils.Imghost + path
  61. return path, err
  62. }
  63. // 视频上传到阿里云
  64. func UploadVideoAliyun(filename, filepath, savePath string) error {
  65. defer func() {
  66. os.Remove(filepath)
  67. }()
  68. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  69. if err != nil {
  70. return err
  71. }
  72. bucket, err := client.Bucket(utils.Bucketname)
  73. if err != nil {
  74. return err
  75. }
  76. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  77. //path += filename
  78. err = bucket.PutObjectFromFile(savePath, filepath)
  79. if err != nil {
  80. return err
  81. }
  82. //path = utils.Imghost + path
  83. //return path,err
  84. return err
  85. }
  86. // PDF上传到阿里云
  87. func UploadPdfAliyun(filename, filepath string) (string, error) {
  88. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  89. if err != nil {
  90. return "1", err
  91. }
  92. bucket, err := client.Bucket(utils.Bucketname)
  93. if err != nil {
  94. return "2", err
  95. }
  96. path := utils.Upload_Pdf_Dir + time.Now().Format("200601/20060102/")
  97. path += filename
  98. err = bucket.PutObjectFromFile(path, filepath)
  99. if err != nil {
  100. return "3", err
  101. }
  102. path = utils.Imghost + path
  103. return path, err
  104. }
  105. const (
  106. HzEndpoint = "oss-cn-shanghai.aliyuncs.com"
  107. HzBucketName string = "hzchart"
  108. )
  109. // 上传文件到阿里云
  110. func UploadFileToAliyun(filename, filepath, savePath string) error {
  111. defer func() {
  112. os.Remove(filepath)
  113. }()
  114. client, err := oss.New(HzEndpoint, utils.AccessKeyId, utils.AccessKeySecret)
  115. if err != nil {
  116. return err
  117. }
  118. bucket, err := client.Bucket(HzBucketName)
  119. if err != nil {
  120. return err
  121. }
  122. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  123. //path += filename
  124. err = bucket.PutObjectFromFile(savePath, filepath)
  125. if err != nil {
  126. return err
  127. }
  128. //path = utils.Imghost + path
  129. //return path,err
  130. return err
  131. }
  132. // GetCloudDiskResourceFileTypeExtMap 常见文件类型图标
  133. func GetCloudDiskResourceFileTypeExtMap() map[string]string {
  134. return map[string]string{
  135. ".doc": "https://hzstatic.hzinsights.com/static/icon/file_type_docx.png",
  136. ".docx": "https://hzstatic.hzinsights.com/static/icon/file_type_docx.png",
  137. ".pdf": "https://hzstatic.hzinsights.com/static/icon/file_type_pdf.png",
  138. ".ppt": "https://hzstatic.hzinsights.com/static/icon/file_type_ppt.png",
  139. ".pptx": "https://hzstatic.hzinsights.com/static/icon/file_type_ppt.png",
  140. ".xls": "https://hzstatic.hzinsights.com/static/icon/file_type_xlsx.png",
  141. ".xlsx": "https://hzstatic.hzinsights.com/static/icon/file_type_xlsx.png",
  142. ".jpg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  143. ".jpeg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  144. ".png": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  145. ".bmp": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  146. ".svg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  147. ".gif": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  148. ".mp4": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  149. ".wmv": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  150. ".mov": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  151. ".mpeg": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  152. ".avi": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  153. ".flv": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  154. ".rm": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  155. ".rmvb": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  156. ".mp3": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
  157. ".wma": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
  158. }
  159. }