12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package services
- import (
- "context"
- "errors"
- "eta_gn/eta_report/utils"
- "github.com/minio/minio-go/v7"
- "github.com/minio/minio-go/v7/pkg/credentials"
- "log"
- "os"
- "time"
- )
- type MinioOss struct{}
- 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
- }
|