oss.go 1012 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package util
  2. import (
  3. "dust-monitor/parser"
  4. "fmt"
  5. "io"
  6. "time"
  7. minio "github.com/minio/minio-go/v6"
  8. )
  9. func getOssUrl(objName string) string {
  10. return fmt.Sprintf("%s://%s/%s/%s", parser.Conf.Oss.Protocol, parser.Conf.Oss.Endpoint, parser.Conf.Oss.DustBucket, objName)
  11. }
  12. func UploadToMinio(fileName string, r io.Reader, size int64, contentType string) (objName string, err error) {
  13. endpoint := parser.Conf.Oss.Endpoint
  14. accessKeyID := parser.Conf.Oss.Id
  15. secretAccessKey := parser.Conf.Oss.Key
  16. useSSL := parser.Conf.Oss.UseSsl
  17. // Initialize minio client object.
  18. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  19. if err != nil {
  20. return "", err
  21. }
  22. bucketName := parser.Conf.Oss.DustBucket
  23. // Upload the zip file
  24. objectName := fmt.Sprintf("%d%s", time.Now().Unix(), fileName)
  25. _, err = minioClient.PutObject(bucketName, objectName, r, size, minio.PutObjectOptions{ContentType: contentType})
  26. if err != nil {
  27. return "", err
  28. }
  29. return getOssUrl(objectName), nil
  30. }