oss.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package services
  2. import (
  3. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  4. "os"
  5. "time"
  6. "hongze/hongze_public_api/utils"
  7. )
  8. const (
  9. HzEndpoint = "oss-cn-shanghai.aliyuncs.com"
  10. HzBucketName string = "hzchart"
  11. )
  12. // 后台用于传研报小程序二维码
  13. var (
  14. Bucketname string = "hzchart"
  15. Endpoint string = "oss-cn-shanghai.aliyuncs.com"
  16. Imghost string = "https://hzstatic.hzinsights.com/"
  17. UploadDir string = "static/images/"
  18. AccessKeyId string = "LTAIFMZYQhS2BTvW"
  19. AccessKeySecret string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
  20. )
  21. // 上传文件到阿里云
  22. func UploadFileToAliyun(filename, filepath, savePath string) error {
  23. defer func() {
  24. os.Remove(filepath)
  25. }()
  26. client, err := oss.New(HzEndpoint, utils.AccessKeyId, utils.AccessKeySecret)
  27. if err != nil {
  28. return err
  29. }
  30. bucket, err := client.Bucket(HzBucketName)
  31. if err != nil {
  32. return err
  33. }
  34. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  35. //path += filename
  36. err = bucket.PutObjectFromFile(savePath, filepath)
  37. if err != nil {
  38. return err
  39. }
  40. //path = utils.Imghost + path
  41. //return path,err
  42. return err
  43. }
  44. // UploadAliYun 图片上传到阿里云
  45. func UploadAliYun(filename, filepath string) (string, error) {
  46. client, err := oss.New(Endpoint, AccessKeyId, AccessKeySecret)
  47. if err != nil {
  48. return "1", err
  49. }
  50. bucket, err := client.Bucket(Bucketname)
  51. if err != nil {
  52. return "2", err
  53. }
  54. path := UploadDir + 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 = Imghost + path
  61. return path, err
  62. }