oss.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package services
  2. import (
  3. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  4. "os"
  5. "time"
  6. "eta/eta_pub/utils"
  7. )
  8. // UploadFileToAliyun 上传文件到阿里云
  9. func UploadFileToAliyun(filename, filepath, savePath string) error {
  10. defer func() {
  11. os.Remove(filepath)
  12. }()
  13. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  14. if err != nil {
  15. return err
  16. }
  17. bucket, err := client.Bucket(utils.Bucketname)
  18. if err != nil {
  19. return err
  20. }
  21. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  22. //path += filename
  23. err = bucket.PutObjectFromFile(savePath, filepath)
  24. if err != nil {
  25. return err
  26. }
  27. //path = utils.Imghost + path
  28. //return path,err
  29. return err
  30. }
  31. // UploadAliYun 图片上传到阿里云
  32. func UploadAliYun(filename, filepath string) (string, error) {
  33. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  34. if err != nil {
  35. return "1", err
  36. }
  37. bucket, err := client.Bucket(utils.Bucketname)
  38. if err != nil {
  39. return "2", err
  40. }
  41. path := utils.UploadDir + time.Now().Format("200601/20060102/")
  42. path += filename
  43. err = bucket.PutObjectFromFile(path, filepath)
  44. if err != nil {
  45. return "3", err
  46. }
  47. path = utils.Imghost + path
  48. return path, err
  49. }