From 45382d14468359d0abd83295739bed207d28c4d8 Mon Sep 17 00:00:00 2001 From: Brandon lz Date: Tue, 22 Oct 2024 07:00:51 +0000 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=A7=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=88=86=E7=89=87=E4=B8=8A=E4=BC=A0minio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/utils/upload/minio_oss.go | 41 +++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/server/utils/upload/minio_oss.go b/server/utils/upload/minio_oss.go index f56cb5cbf..3a6af72ce 100644 --- a/server/utils/upload/minio_oss.go +++ b/server/utils/upload/minio_oss.go @@ -1,8 +1,10 @@ package upload import ( + "bytes" "context" "errors" + "io" "mime/multipart" "path/filepath" "strings" @@ -15,10 +17,10 @@ import ( "go.uber.org/zap" ) -var MinioClient *Minio // 优化性能,但是不支持动态配置,如果修改了minio配置,需要重启后端 +var MinioClient *Minio // 优化性能,但是不支持动态配置 type Minio struct { - client *minio.Client + Client *minio.Client bucket string } @@ -45,26 +47,42 @@ func GetMinio(endpoint, accessKeyID, secretAccessKey, bucketName string, useSSL return nil, err } } - - return &Minio{client: minioClient, bucket: bucketName}, nil + MinioClient = &Minio{Client: minioClient, bucket: bucketName} + return MinioClient, nil } func (m *Minio) UploadFile(file *multipart.FileHeader) (filePathres, key string, uploadErr error) { - - // 读取本地文件。 f, openError := file.Open() + // mutipart.File to os.File if openError != nil { global.GVA_LOG.Error("function file.Open() Failed", zap.Any("err", openError.Error())) return "", "", errors.New("function file.Open() Failed, err:" + openError.Error()) } - defer f.Close() // 创建文件 defer 关闭 + + filecontent := bytes.Buffer{} + _, err := io.Copy(&filecontent, f) + if err != nil { + global.GVA_LOG.Error("读取文件失败", zap.Any("err", err.Error())) + return "", "", errors.New("读取文件失败, err:" + err.Error()) + } + f.Close() // 创建文件 defer 关闭 + + // 对文件名进行加密存储 ext := filepath.Ext(file.Filename) filename := utils.MD5V([]byte(strings.TrimSuffix(file.Filename, ext))) + ext - filePathres = global.GVA_CONFIG.Minio.BasePath + "/" + "uploads" + "/" + time.Now().Format("2006-01-02") + "/" + filename + if global.GVA_CONFIG.Minio.BasePath == "" { + filePathres = "uploads" + "/" + time.Now().Format("2006-01-02") + "/" + filename + } else { + filePathres = global.GVA_CONFIG.Minio.BasePath + "/" + time.Now().Format("2006-01-02") + "/" + filename + } - // Upload the file with FPutObject - info, err := m.client.PutObject(context.Background(), global.GVA_CONFIG.Minio.BucketName, filePathres, f, file.Size, minio.PutObjectOptions{ContentType: "application/octet-stream"}) + // 设置超时10分钟 + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10) + defer cancel() + + // Upload the file with PutObject 大文件自动切换为分片上传 + info, err := m.Client.PutObject(ctx, global.GVA_CONFIG.Minio.BucketName, filePathres, &filecontent, file.Size, minio.PutObjectOptions{ContentType: "application/octet-stream"}) if err != nil { global.GVA_LOG.Error("上传文件到minio失败", zap.Any("err", err.Error())) return "", "", errors.New("上传文件到minio失败, err:" + err.Error()) @@ -74,6 +92,7 @@ func (m *Minio) UploadFile(file *multipart.FileHeader) (filePathres, key string, func (m *Minio) DeleteFile(key string) error { // Delete the object from MinIO - err := m.client.RemoveObject(context.Background(), m.bucket, key, minio.RemoveObjectOptions{}) + ctx, _ := context.WithTimeout(context.Background(), time.Second*5) + err := m.Client.RemoveObject(ctx, m.bucket, key, minio.RemoveObjectOptions{}) return err }