oss.go 914 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package services
  2. import (
  3. "errors"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. "os"
  6. "time"
  7. "eta_gn/eta_report/utils"
  8. )
  9. type AliOss struct{}
  10. func (m *AliOss) UploadFile(fileName, filePath, savePath string) (string, error) {
  11. defer func() {
  12. _ = os.Remove(filePath)
  13. }()
  14. if utils.AccessKeyId == `` {
  15. return "0", errors.New("阿里云信息未配置")
  16. }
  17. client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
  18. if err != nil {
  19. return "1", err
  20. }
  21. bucket, err := client.Bucket(utils.Bucketname)
  22. if err != nil {
  23. return "2", err
  24. }
  25. path := savePath
  26. if savePath == "" {
  27. path = utils.UploadDir + time.Now().Format("200601/20060102/") + fileName
  28. }
  29. err = bucket.PutObjectFromFile(path, filePath)
  30. if err != nil {
  31. return "3", err
  32. }
  33. resourceUrl := utils.Imghost + path
  34. return resourceUrl, err
  35. }
  36. func (m *AliOss) GetUploadToken() (token OssToken, err error) {
  37. return
  38. }