oss.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package services
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. "os"
  6. "strings"
  7. "time"
  8. "hongze/hongze_cygx/utils"
  9. )
  10. /*
  11. 上传demo
  12. func init() {
  13. fmt.Println("start")
  14. randStr := utils.GetRandStringNoSpecialChar(28)
  15. fileName := randStr + ".jpg"
  16. fmt.Println("fileName:",fileName)
  17. fpath:="./1.png"
  18. resourceUrl,err:=UploadAliyun(fileName,fpath)
  19. if err!=nil {
  20. fmt.Println("UploadAliyun Err:",err.Error())
  21. return
  22. }
  23. fmt.Println("resourceUrl:",resourceUrl)
  24. fmt.Println("end")
  25. }
  26. */
  27. // 图片上传到阿里云
  28. func UploadAliyun(filename, filepath string) (string, error) {
  29. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  30. if err != nil {
  31. return "1", err
  32. }
  33. bucket, err := client.Bucket(utils.Bucketname)
  34. if err != nil {
  35. return "2", err
  36. }
  37. path := utils.Upload_dir + time.Now().Format("200601/20060102/")
  38. path += filename
  39. err = bucket.PutObjectFromFile(path, filepath)
  40. if err != nil {
  41. return "3", err
  42. }
  43. path = utils.Imghost + path
  44. return path, err
  45. }
  46. // 音频上传到阿里云
  47. func UploadAudioAliyun(filename, filepath string) (string, error) {
  48. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  49. if err != nil {
  50. return "1", err
  51. }
  52. bucket, err := client.Bucket(utils.Bucketname)
  53. if err != nil {
  54. return "2", err
  55. }
  56. path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  57. path += filename
  58. err = bucket.PutObjectFromFile(path, filepath)
  59. if err != nil {
  60. return "3", err
  61. }
  62. path = utils.Imghost + path
  63. return path, err
  64. }
  65. // 视频上传到阿里云
  66. func UploadVideoAliyun(filename, filepath, savePath string) error {
  67. defer func() {
  68. os.Remove(filepath)
  69. }()
  70. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  71. if err != nil {
  72. return err
  73. }
  74. bucket, err := client.Bucket(utils.Bucketname)
  75. if err != nil {
  76. return err
  77. }
  78. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  79. //path += filename
  80. err = bucket.PutObjectFromFile(savePath, filepath)
  81. if err != nil {
  82. return err
  83. }
  84. //path = utils.Imghost + path
  85. //return path,err
  86. return err
  87. }
  88. // PDF上传到阿里云
  89. func UploadPdfAliyun(filename, filepath string) (string, error) {
  90. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  91. if err != nil {
  92. return "1", err
  93. }
  94. bucket, err := client.Bucket(utils.Bucketname)
  95. if err != nil {
  96. return "2", err
  97. }
  98. path := utils.Upload_Pdf_Dir + time.Now().Format("200601/20060102/")
  99. path += filename
  100. err = bucket.PutObjectFromFile(path, filepath)
  101. if err != nil {
  102. return "3", err
  103. }
  104. path = utils.Imghost + path
  105. return path, err
  106. }
  107. const (
  108. HzEndpoint = "oss-cn-shanghai.aliyuncs.com"
  109. HzBucketName string = "hzchart"
  110. )
  111. // 上传文件到阿里云
  112. func UploadFileToAliyun(filename, filepath, savePath string) error {
  113. defer func() {
  114. os.Remove(filepath)
  115. }()
  116. client, err := oss.New(HzEndpoint, utils.AccessKeyId, utils.AccessKeySecret)
  117. if err != nil {
  118. return err
  119. }
  120. bucket, err := client.Bucket(HzBucketName)
  121. if err != nil {
  122. return err
  123. }
  124. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  125. //path += filename
  126. err = bucket.PutObjectFromFile(savePath, filepath)
  127. if err != nil {
  128. return err
  129. }
  130. //path = utils.Imghost + path
  131. //return path,err
  132. return err
  133. }
  134. // GetCloudDiskResourceFileTypeExtMap 常见文件类型图标
  135. func GetCloudDiskResourceFileTypeExtMap() map[string]string {
  136. return map[string]string{
  137. ".doc": "https://hzstatic.hzinsights.com/static/icon/file_type_docx.png",
  138. ".docx": "https://hzstatic.hzinsights.com/static/icon/file_type_docx.png",
  139. ".pdf": "https://hzstatic.hzinsights.com/static/icon/file_type_pdf.png",
  140. ".ppt": "https://hzstatic.hzinsights.com/static/icon/file_type_ppt.png",
  141. ".pptx": "https://hzstatic.hzinsights.com/static/icon/file_type_ppt.png",
  142. ".xls": "https://hzstatic.hzinsights.com/static/icon/file_type_xlsx.png",
  143. ".xlsx": "https://hzstatic.hzinsights.com/static/icon/file_type_xlsx.png",
  144. ".jpg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  145. ".jpeg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  146. ".png": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  147. ".bmp": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  148. ".svg": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  149. ".gif": "https://hzstatic.hzinsights.com/static/icon/file_type_pic.png",
  150. ".mp4": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  151. ".wmv": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  152. ".mov": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  153. ".mpeg": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  154. ".avi": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  155. ".flv": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  156. ".rm": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  157. ".rmvb": "https://hzstatic.hzinsights.com/static/icon/file_type_video.png",
  158. ".mp3": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
  159. ".wma": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
  160. }
  161. }
  162. //func init() {
  163. // DeleteUploadFileToAliyun("https://hzstatic.hzinsights.com/static/images/htm2img/9iElUd8oxpWgggf5pcKOCB8LZTiB.jpg")
  164. //}
  165. // 根据图片路径删除阿里云上的文件
  166. func DeleteUploadFileToAliyun(imgUrl string) error {
  167. var err error
  168. defer func() {
  169. if err != nil {
  170. fmt.Println("err:", err)
  171. go utils.SendAlarmMsg("删除Oss图片失败, Err:"+err.Error()+"图片路径:"+imgUrl, 3)
  172. }
  173. }()
  174. client, err := oss.New(HzEndpoint, utils.AccessKeyId, utils.AccessKeySecret)
  175. if err != nil {
  176. return err
  177. }
  178. bucket, err := client.Bucket(HzBucketName)
  179. if err != nil {
  180. return err
  181. }
  182. var objectName string
  183. sliceObj := strings.Split(imgUrl, ".com/")
  184. if len(sliceObj) == 2 {
  185. objectName = sliceObj[1]
  186. }
  187. if objectName == "" {
  188. return err
  189. }
  190. fmt.Println(objectName)
  191. //return err
  192. // 如需删除文件夹,请将objectName设置为对应的文件夹名称。如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
  193. //objectName = "static/images/htm2img/G32q2Lgp6soeh7xOW12yfZ4zC2uK.jpg"
  194. // 删除单个文件。
  195. err = bucket.DeleteObject(objectName)
  196. if err != nil {
  197. return err
  198. }
  199. return err
  200. }