123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package services
- import (
- "context"
- "errors"
- "eta/eta_report/utils"
- "github.com/minio/minio-go/v7"
- "github.com/minio/minio-go/v7/pkg/credentials"
- "log"
- "os"
- "time"
- )
- // UploadMinIoToDir 上传至hzchart
- //func UploadMinIoToDir(filename, filePath, uploadDir, fileDir string) (string, error) {
- // if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
- // return "0", errors.New("MinIo信息未配置")
- // }
- // ctx := context.Background()
- // endpoint := utils.MinIoEndpoint
- // accessKeyID := utils.MinIoAccessKeyId
- // secretAccessKey := utils.MinIoAccessKeySecret
- // useSSL := false
- // if utils.MinIoUseSSL == "true" {
- // useSSL = true
- // }
- // minioClient, err := minio.New(endpoint, &minio.Options{
- // Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
- // Secure: useSSL,
- // })
- // if err != nil {
- // log.Fatalln(err)
- // return "1", err
- // }
- // bucketName := utils.MinIoBucketname
- // // Check to see if we already own this bucket (which happens if you run this twice)
- // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
- // if errBucketExists == nil && exists {
- // log.Printf("We already own %s\n", bucketName)
- // } else {
- // log.Fatalln(err)
- // return "2", err
- // }
- // if uploadDir == "" {
- // uploadDir = utils.MinIoUploadDir
- // }
- // if fileDir == "" {
- // fileDir = time.Now().Format("200601/20060102/")
- // }
- // path := uploadDir + fileDir
- // path += filename
- // _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
- // if err != nil {
- // log.Fatalln(err)
- // return "3", err
- // }
- // path = utils.MinIoImghost + path
- // return path, err
- //}
- type MinioOss struct{}
- // UploadFile 上传文件
- func (m *MinioOss) UploadFile(fileName, filePath, savePath string) (string, error) {
- defer func() {
- _ = os.Remove(filePath)
- }()
- if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
- return "0", errors.New("MinIo信息未配置")
- }
- ctx := context.Background()
- endpoint := utils.MinIoEndpoint
- accessKeyID := utils.MinIoAccessKeyId
- secretAccessKey := utils.MinIoAccessKeySecret
- useSSL := false
- if utils.MinIoUseSSL == "true" {
- useSSL = true
- }
- minioClient, err := minio.New(endpoint, &minio.Options{
- Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
- Secure: useSSL,
- })
- if err != nil {
- log.Fatalln(err)
- return "1", err
- }
- bucketName := utils.MinIoBucketname
- exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
- if errBucketExists == nil && exists {
- log.Printf("We already own %s\n", bucketName)
- } else {
- log.Fatalln(err)
- return "2", err
- }
- path := savePath
- if savePath == "" {
- path = utils.MinIoUploadDir + time.Now().Format("200601/20060102/") + fileName
- }
- _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
- if err != nil {
- log.Fatalln(err)
- return "3", err
- }
- resourceUrl := utils.MinIoImghost + path
- return resourceUrl, err
- }
- func (m *MinioOss) GetUploadToken() (token OssToken, err error) {
- token.AccessKeyId = utils.MinIoAccessKeyId
- token.SecretKeyId = utils.MinIoAccessKeySecret
- token.Endpoint = utils.MinIoEndpoint
- token.ImgHost = utils.MinIoImghost
- token.Bucketname = utils.MinIoBucketname
- token.UseSSL = utils.MinIoUseSSL
- token.RegionId = utils.MinIoRegion
- token.Port = utils.MinIoPort
- return
- }
|