oss.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package services
  2. import (
  3. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  4. "hongze/hongze_yb/global"
  5. "os"
  6. "time"
  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. // UploadAliyun 图片上传到阿里云
  26. func UploadAliyun(filename, filepath string) (string, error) {
  27. client, err := oss.New(global.CONFIG.AliOss.EndPoint, global.CONFIG.AliOss.AccessKeyId, global.CONFIG.AliOss.AccessKeySecret)
  28. if err != nil {
  29. return "1", err
  30. }
  31. bucket, err := client.Bucket(global.CONFIG.AliOss.BucketName)
  32. if err != nil {
  33. return "2", err
  34. }
  35. path := global.CONFIG.AliOss.UploadDir + 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 = global.CONFIG.AliOss.ImgHost + path
  42. return path, err
  43. }
  44. // UploadAudioAliyun 音频上传到阿里云
  45. func UploadAudioAliyun(filename, filepath string) (string, error) {
  46. client, err := oss.New(global.CONFIG.AliOss.EndPoint, global.CONFIG.AliOss.AccessKeyId, global.CONFIG.AliOss.AccessKeySecret)
  47. if err != nil {
  48. return "1", err
  49. }
  50. bucket, err := client.Bucket(global.CONFIG.AliOss.BucketName)
  51. if err != nil {
  52. return "2", err
  53. }
  54. path := global.CONFIG.AliOss.UploadAudioDir + 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 = global.CONFIG.AliOss.ImgHost + path
  61. return path, err
  62. }
  63. // UploadVideoAliyun 视频上传到阿里云
  64. func UploadVideoAliyun(filename, filepath, savePath string) error {
  65. defer func() {
  66. os.Remove(filepath)
  67. }()
  68. client, err := oss.New(global.CONFIG.AliOss.EndPoint, global.CONFIG.AliOss.AccessKeyId, global.CONFIG.AliOss.AccessKeySecret)
  69. if err != nil {
  70. return err
  71. }
  72. bucket, err := client.Bucket(global.CONFIG.AliOss.BucketName)
  73. if err != nil {
  74. return err
  75. }
  76. //path := global.CONFIG.AliOss.UploadAudioDir + time.Now().Format("200601/20060102/")
  77. //path += filename
  78. err = bucket.PutObjectFromFile(savePath, filepath)
  79. if err != nil {
  80. return err
  81. }
  82. //path = global.CONFIG.AliOss.ImgHost + path
  83. //return path,err
  84. return err
  85. }
  86. var (
  87. Bucketname string = "hzchart"
  88. Endpoint string = "oss-cn-shanghai.aliyuncs.com"
  89. ResourceHost string = "https://hzstatic.hzinsights.com/"
  90. StaticDir string = "static/"
  91. AccessKeyId string = "LTAIFMZYQhS2BTvW"
  92. AccessKeySecret string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
  93. )
  94. // UploadAliyunToDir
  95. func UploadAliyunToDir(filename, filepath, fileDir string) (string, error) {
  96. client, err := oss.New(Endpoint, AccessKeyId, AccessKeySecret)
  97. if err != nil {
  98. return "1", err
  99. }
  100. bucket, err := client.Bucket(Bucketname)
  101. if err != nil {
  102. return "2", err
  103. }
  104. if fileDir == "" {
  105. fileDir = time.Now().Format("200601/20060102/")
  106. }
  107. path := StaticDir + fileDir
  108. path += filename
  109. err = bucket.PutObjectFromFile(path, filepath)
  110. if err != nil {
  111. return "3", err
  112. }
  113. path = ResourceHost + path
  114. return path, err
  115. }