|
@@ -7,60 +7,64 @@ import (
|
|
|
"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
|
|
|
-}
|
|
|
+//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信息未配置")
|
|
|
}
|