oss.go 4.7 KB

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