123456789101112131415161718192021222324252627282930313233343536373839 |
- package util
- import (
- "dust-monitor/parser"
- "fmt"
- "io"
- "time"
- minio "github.com/minio/minio-go/v6"
- )
- func getOssUrl(objName string) string {
- return fmt.Sprintf("%s://%s/%s/%s", parser.Conf.Oss.Protocol, parser.Conf.Oss.Endpoint, parser.Conf.Oss.DustBucket, objName)
- }
- func UploadToMinio(fileName string, r io.Reader, size int64, contentType string) (objName string, err error) {
- endpoint := parser.Conf.Oss.Endpoint
- accessKeyID := parser.Conf.Oss.Id
- secretAccessKey := parser.Conf.Oss.Key
- useSSL := parser.Conf.Oss.UseSsl
- // Initialize minio client object.
- minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
- if err != nil {
- return "", err
- }
- bucketName := parser.Conf.Oss.DustBucket
- // Upload the zip file
- objectName := fmt.Sprintf("%d%s", time.Now().Unix(), fileName)
- _, err = minioClient.PutObject(bucketName, objectName, r, size, minio.PutObjectOptions{ContentType: contentType})
- if err != nil {
- return "", err
- }
- return getOssUrl(objectName), nil
- }
|