liusuyi
2026-05-18 8a7b666f0bed7c1dc03767166c7ecd602552a5c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.ard.file.service;
 
import java.io.InputStream;
 
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.nacos.common.utils.IoUtils;
import com.ard.common.core.utils.StringUtils;
import com.ard.file.config.MinioConfig;
import com.ard.file.utils.FileUploadUtils;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
 
/**
 * Minio 文件存储
 *
 * @author ard
 */
@Primary
@Service
public class MinioSysFileServiceImpl implements ISysFileService {
    @Autowired
    private MinioConfig minioConfig;
 
    @Autowired
    private MinioClient client;
 
    /**
     * Minio文件上传接口
     *
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        InputStream inputStream = null;
        try {
            String fileName = FileUploadUtils.extractFilename(file);
            inputStream = file.getInputStream();
            PutObjectArgs args = PutObjectArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(fileName)
                    .stream(inputStream, file.getSize(), -1)
                    .contentType(file.getContentType())
                    .build();
            client.putObject(args);
            return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
        } catch (Exception e) {
            throw new RuntimeException("Minio Failed to upload file", e);
        } finally {
            IoUtils.closeQuietly(inputStream);
        }
    }
 
    /**
     * Minio文件上传接口
     *
     * @param file       上传的文件
     * @param bucketName 桶名称
     * @return 访问地址
     * @author 刘苏义
     * @description 指定桶名称上传文件
     * @date 2024/8/5 11:37
     */
    @Override
    public String uploadFile(MultipartFile file, String bucketName) throws Exception {
        String fileName = FileUploadUtils.extractFilename(file);
        InputStream inputStream = file.getInputStream();
        PutObjectArgs args = PutObjectArgs.builder()
                .bucket(bucketName)
                .object(fileName)
                .stream(inputStream, file.getSize(), -1)
                .contentType(file.getContentType())
                .build();
        client.putObject(args);
        IoUtils.closeQuietly(inputStream);
        return minioConfig.getUrl() + "/" + bucketName + "/" + fileName;
    }
 
    @Override
    public String uploadFile(MultipartFile file, String bucketName, String path) throws Exception {
        //判断文件是否为空
        if (null == file || 0 == file.getSize()) {
            return "";
        }
        //文件名
        String fileName = path + "/" + FileUploadUtils.extractFilename(file);
        InputStream inputStream = file.getInputStream();
        /*上传对象*/
        PutObjectArgs putObjectArgs = PutObjectArgs
                .builder()
                .bucket(bucketName)
                .object(fileName)
                .stream(inputStream, file.getSize(), -1)
                .contentType(file.getContentType())
                .build();
        client.putObject(putObjectArgs);
        inputStream.close();
        /*获取url*/
        GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs
                .builder()
                .bucket(bucketName)
                .object(fileName)
                .method(Method.GET)
                .build();
        String presignedObjectUrl = client.getPresignedObjectUrl(getPresignedObjectUrlArgs);
        String ObjectUrl = presignedObjectUrl.substring(0, presignedObjectUrl.indexOf("?"));
        return ObjectUrl;
    }
 
    /**
     * Minio文件删除接口
     *
     * @param fileUrl 文件访问URL
     * @throws Exception
     */
    @Override
    public void deleteFile(String fileUrl) throws Exception {
        try {
            String minioFile = StringUtils.substringAfter(fileUrl, minioConfig.getBucketName() + "/");
            client.removeObject(RemoveObjectArgs.builder().bucket(minioConfig.getBucketName()).object(minioFile).build());
        } catch (Exception e) {
            throw new RuntimeException("Minio Failed to delete file", e);
        }
    }
}