|
@@ -2,14 +2,13 @@ package services
|
|
|
|
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
|
|
+ "errors"
|
|
"eta/eta_api/utils"
|
|
"eta/eta_api/utils"
|
|
- "fmt"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
- "io/ioutil"
|
|
|
|
"log"
|
|
"log"
|
|
- "net/http"
|
|
|
|
- "strings"
|
|
|
|
|
|
+ "os"
|
|
|
|
+ "time"
|
|
)
|
|
)
|
|
|
|
|
|
func GetMinIOSTSToken() (item *Token, err error) {
|
|
func GetMinIOSTSToken() (item *Token, err error) {
|
|
@@ -100,10 +99,10 @@ func UploadMinIo() {
|
|
} else {
|
|
} else {
|
|
log.Printf("Successfully created %s\n", bucketName)
|
|
log.Printf("Successfully created %s\n", bucketName)
|
|
}
|
|
}
|
|
- buckets, err := minioClient.ListBuckets(ctx)
|
|
|
|
- for _, bucket := range buckets {
|
|
|
|
- fmt.Println(bucket)
|
|
|
|
- }
|
|
|
|
|
|
+ //buckets, err := minioClient.ListBuckets(ctx)
|
|
|
|
+ //for _, bucket := range buckets {
|
|
|
|
+ // fmt.Println(bucket)
|
|
|
|
+ //}
|
|
// Upload the zip file
|
|
// Upload the zip file
|
|
objectName := "1111.xlsx"
|
|
objectName := "1111.xlsx"
|
|
filePath := "/Users/xi/Desktop/1111.xlsx"
|
|
filePath := "/Users/xi/Desktop/1111.xlsx"
|
|
@@ -118,21 +117,231 @@ func UploadMinIo() {
|
|
log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)
|
|
log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)
|
|
}
|
|
}
|
|
|
|
|
|
-func HttpPost(url, postData string, params ...string) ([]byte, error) {
|
|
|
|
- body := ioutil.NopCloser(strings.NewReader(postData))
|
|
|
|
- client := &http.Client{}
|
|
|
|
- req, err := http.NewRequest("POST", url, body)
|
|
|
|
|
|
+//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)
|
|
|
|
+ }
|
|
|
|
+ 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)
|
|
|
|
+ }
|
|
|
|
+ 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)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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 {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
|
|
|
|
- if len(params) > 0 && params[0] != "" {
|
|
|
|
- contentType = params[0]
|
|
|
|
- }
|
|
|
|
- req.Header.Set("Content-Type", contentType)
|
|
|
|
- resp, err := client.Do(req)
|
|
|
|
- defer resp.Body.Close()
|
|
|
|
- b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
- fmt.Println("HttpPost:" + string(b))
|
|
|
|
- return b, err
|
|
|
|
|
|
+ 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, fileName, 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
|
|
}
|
|
}
|