123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- package services
- import (
- "context"
- "errors"
- "eta/eta_api/utils"
- "fmt"
- "github.com/minio/minio-go/v7"
- "github.com/minio/minio-go/v7/pkg/credentials"
- "time"
- )
- //func GetMinIOSTSToken() (item *Token, err error) {
- // // MinIO服务的访问信息
- // item = new(Token)
- // //useSSL := false
- // //if utils.MinIoUseSSL == "true" {
- // // useSSL = true
- // //}
- // // 创建MinIO客户端
- // //minioClient, err := minio.New(utils.MinIoEndpoint, &minio.Options{
- // // Creds: credentials.NewStaticV4(utils.MinIoAccessKeyId, utils.MinIoAccessKeySecret, ""),
- // // Secure: useSSL,
- // //})
- // //if err != nil {
- // // return nil, err
- // //}
- // // 设置STS凭证请求参数
- // //policy := `{
- // // "Version": "2012-10-17",
- // // "Statement": [
- // // {
- // // "Sid": "",
- // // "Effect": "Allow",
- // // "Principal": {"AWS": "arn:aws:iam::1234567890:root"},
- // // "Action": "s3:GetObject",
- // // "Resource": "arn:aws:s3:::<YourBucketName>/*"
- // // }
- // // ]
- // //}`
- // //expiry := time.Hour * 24 // STS凭证的过期时间
- // //获取STS凭证
- // //stsCredentials, err := minioClient.PresignedPutObject(context.Background(), "etastatic", "myobject", expiry)
- // //if err != nil {
- // // return
- // //}
- // item.AccessKeyId = utils.MinIoAccessKeyId
- // item.SecretKeyId = utils.MinIoAccessKeySecret
- // item.Endpoint = utils.MinIoEndpoint
- // item.ImgHost = utils.MinIoImghost
- // item.Bucketname = utils.MinIoBucketname
- // item.UseSSL = utils.MinIoUseSSL
- // item.RegionId = utils.MinIoRegion
- // item.Port = utils.MinIoPort
- // return
- //}
- //type Token struct {
- // AccessKeyId string
- // SecretKeyId string
- // RegionId string
- // Bucketname string
- // Endpoint string
- // ImgHost string
- // UseSSL string
- // Port string
- //}
- //func UploadMinIo() {
- // ctx := context.Background()
- // endpoint := "8.136.199.33:9000/"
- // accessKeyID := "LfQ8uiJiLP7vLxjRrmNW"
- // secretAccessKey := "IszGVHsNicJMQxHC46cYFtbrOiapo0ynwOIJ6c2R"
- // useSSL := false
- //
- // // Initialize minio client object.
- // minioClient, err := minio.New(endpoint, &minio.Options{
- // Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
- // Secure: useSSL,
- // })
- // if err != nil {
- // log.Fatalln(err)
- // }
- //
- // // Make a new bucket called mymusic.
- // bucketName := "etastatic"
- // location := "/"
- //
- // err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
- // if err != nil {
- // // 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)
- // }
- // } else {
- // log.Printf("Successfully created %s\n", bucketName)
- // }
- // //buckets, err := minioClient.ListBuckets(ctx)
- // //for _, bucket := range buckets {
- // // fmt.Println(bucket)
- // //}
- // // Upload the zip file
- // objectName := "1111.xlsx"
- // filePath := "/Users/xi/Desktop/1111.xlsx"
- // contentType := "application/xlsx"
- //
- // // Upload the zip file with FPutObject
- // info, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
- // if err != nil {
- // log.Fatalln(err)
- // }
- //
- // log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)
- //}
- // UploadImgToMinIo 图片上传
- //func UploadImgToMinIo(fileName, filePath 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
- // }
- // path := utils.MinIoUploadDir + time.Now().Format("200601/20060102/")
- // path += fileName
- // // Upload the zip file with FPutObject
- // //contentType := "application/xlsx"
- // _, 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
- //}
- // UploadAudioToMinIo 音频上传
- //func UploadAudioToMinIo(fileName, filePath 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
- // }
- //
- // path := utils.MinIoUpload_Audio_Dir + time.Now().Format("200601/20060102/")
- // path += fileName
- //
- // // Upload the zip file with FPutObject
- // //contentType := "application/xlsx"
- // _, 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
- //}
- // UploadVideoToMinIo 视频上传
- //func UploadVideoToMinIo(filename, filePath, savePath string) error {
- // if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
- // return errors.New("MinIo信息未配置")
- // }
- // defer func() {
- // os.Remove(filePath)
- // }()
- //
- // 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 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 err
- // }
- //
- // //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
- // //path += filename
- // _, err = minioClient.FPutObject(ctx, bucketName, savePath, filePath, minio.PutObjectOptions{})
- // if err != nil {
- // log.Fatalln(err)
- // return err
- // }
- // //path = utils.Imghost + path
- // //return path,err
- // return err
- //}
- // UploadFileToMinIo 上传文件
- //func UploadFileToMinIo(filename, filePath, savePath string) error {
- // if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
- // return errors.New("MinIo信息未配置")
- // }
- // defer func() {
- // os.Remove(filePath)
- // }()
- // 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 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 err
- // }
- // //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
- // //path += filename
- // _, err = minioClient.FPutObject(ctx, bucketName, savePath, filePath, minio.PutObjectOptions{})
- // if err != nil {
- // log.Fatalln(err)
- // return err
- // }
- // //path = utils.Imghost + path
- // //return path,err
- // return err
- //}
- // 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
- //}
- //func UploadImgToMinIoTest(fileName, filePath string) (string, error) {
- // ctx := context.Background()
- // endpoint := utils.Endpoint
- // accessKeyID := utils.AccessKeyId
- // secretAccessKey := utils.AccessKeySecret
- // 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.Bucketname
- // // Check to see if we already own this bucket (which happens if you run this twice)
- //
- // buckets, e := minioClient.ListBuckets(ctx)
- // if e != nil {
- // fmt.Println("ListBuckets: ", e.Error())
- // return "", e
- // }
- // for k := range buckets {
- // fmt.Println(k)
- // }
- //
- // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
- // fmt.Println("exists: ", exists)
- // fmt.Println("errBucketExists: ", errBucketExists)
- // if errBucketExists == nil && exists {
- // log.Printf("We already own %s\n", bucketName)
- // } else {
- // log.Fatalln(err)
- // return "2", err
- // }
- // path := utils.UploadDir + time.Now().Format("200601/20060102/")
- // path += fileName
- // // Upload the zip file with FPutObject
- // //contentType := "application/xlsx"
- // _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
- // if err != nil {
- // log.Fatalln(err)
- // return "3", err
- // }
- //
- // path = utils.Imghost + path
- // return path, err
- //}
- 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("MinIo连接失败:", err)
- 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)
- utils.FileLog.Error("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("MinIo上传文件失败:", 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
- }
|