123456789101112131415161718192021222324252627282930313233343536373839 |
- package utils
- import (
- "access-control-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.StaffBucket, 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.StaffBucket
- // 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
- }
|