|
@@ -0,0 +1,191 @@
|
|
|
+package oss
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "errors"
|
|
|
+ "eta/eta_mini_crm_ht/utils"
|
|
|
+ "fmt"
|
|
|
+ "github.com/minio/minio-go/v7"
|
|
|
+ "github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+type MinioOss struct{}
|
|
|
+
|
|
|
+// UploadFile 上传文件
|
|
|
+func (m *MinioOss) UploadFile(fileName, filePath, savePath string) (string, error) {
|
|
|
+ if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
|
|
|
+ return "0", errors.New("MinIo信息未配置")
|
|
|
+ }
|
|
|
+
|
|
|
+ ctx := context.Background()
|
|
|
+ // 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
|
|
|
+ endpoint := utils.MinIoEndpoint
|
|
|
+ if utils.MinIoBackEndpoint != "" {
|
|
|
+ endpoint = utils.MinIoBackEndpoint
|
|
|
+ }
|
|
|
+ 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 {
|
|
|
+ utils.FileLog.Error(err.Error())
|
|
|
+ return "1", err
|
|
|
+ }
|
|
|
+ bucketName := utils.MinIoBucketname
|
|
|
+ exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
|
|
|
+ if errBucketExists != nil || !exists {
|
|
|
+ err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
|
|
|
+ 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 {
|
|
|
+ utils.FileLog.Error(err.Error())
|
|
|
+ return "3", err
|
|
|
+ }
|
|
|
+ resourceUrl := utils.MinIoPdfhost + path
|
|
|
+ return resourceUrl, err
|
|
|
+}
|
|
|
+
|
|
|
+func (m *MinioOss) GetExt(string) (ext string, err error) {
|
|
|
+ if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
|
|
|
+ err = errors.New("MinIo信息未配置")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ctx := context.Background()
|
|
|
+ // 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
|
|
|
+ endpoint := utils.MinIoEndpoint
|
|
|
+ if utils.MinIoBackEndpoint != "" {
|
|
|
+ endpoint = utils.MinIoBackEndpoint
|
|
|
+ }
|
|
|
+ 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 {
|
|
|
+ utils.FileLog.Error(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ bucketName := utils.MinIoBucketname
|
|
|
+ exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
|
|
|
+ if errBucketExists != nil || !exists {
|
|
|
+ err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ info, minioErr := minioClient.StatObject(ctx, bucketName, "test.txt", minio.GetObjectOptions{})
|
|
|
+ if minioErr != nil {
|
|
|
+ err = fmt.Errorf("StatObject: %v; err: %v", info, minioErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ext = GetFileExtensionFromContentType(info.ContentType)
|
|
|
+ return
|
|
|
+}
|
|
|
+func (m *MinioOss) GetUploadToken() (token OssToken, err error) {
|
|
|
+ token.AccessKeyId = utils.MinIoAccessKeyId
|
|
|
+ token.SecretKeyId = utils.MinIoAccessKeySecret
|
|
|
+ token.Endpoint = utils.MinIoEndpoint
|
|
|
+ token.ImgHost = utils.MinIoPdfhost
|
|
|
+ token.Bucketname = utils.MinIoBucketname
|
|
|
+ token.UseSSL = utils.MinIoUseSSL
|
|
|
+ token.RegionId = utils.MinIoRegion
|
|
|
+ token.Port = utils.MinIoPort
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func (m *MinioOss) GetFile(filePath, savePath string) (path string, err error) {
|
|
|
+ if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
|
|
|
+ err = errors.New("MinIo信息未配置")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ctx := context.Background()
|
|
|
+ // 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
|
|
|
+ endpoint := utils.MinIoEndpoint
|
|
|
+ if utils.MinIoBackEndpoint != "" {
|
|
|
+ endpoint = utils.MinIoBackEndpoint
|
|
|
+ }
|
|
|
+ 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 {
|
|
|
+ utils.FileLog.Error(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ bucketName := utils.MinIoBucketname
|
|
|
+ exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
|
|
|
+ if errBucketExists != nil || !exists {
|
|
|
+ err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
|
|
|
+ utils.FileLog.Error(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ext, err := m.GetExt(filePath)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("GetExt: %v; err: %v", ext, err)
|
|
|
+ utils.FileLog.Error(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ path = savePath + ext
|
|
|
+ err = minioClient.FGetObject(ctx, bucketName, filePath, path, minio.GetObjectOptions{})
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+func (m *MinioOss) MultiUploadFile() (err error) {
|
|
|
+ if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
|
|
|
+ err = errors.New("MinIo信息未配置")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ctx := context.Background()
|
|
|
+ // 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
|
|
|
+ endpoint := utils.MinIoEndpoint
|
|
|
+ if utils.MinIoBackEndpoint != "" {
|
|
|
+ endpoint = utils.MinIoBackEndpoint
|
|
|
+ }
|
|
|
+ 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 {
|
|
|
+ utils.FileLog.Error(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ bucketName := utils.MinIoBucketname
|
|
|
+ exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
|
|
|
+ if errBucketExists != nil || !exists {
|
|
|
+ err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|