龚焕茏
2024-03-06 77f99800075afbfe398d1e9b8b567407ff9f539b
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
package org.dromara.demo.util;
 
 
import cn.hutool.core.util.IdUtil;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.demo.domain.MinioResult;
import org.dromara.demo.exception.UploadFailException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.ByteArrayInputStream;
 
/**
 * @author gonghl
 * @date 2024-3-6
 */
@Component
@ConfigurationProperties(prefix = "minio")
@Slf4j
@Data
public class MinioUtil {
 
 
    private String url;
    private String accessKey;
    private String secretKey;
    private MinioClient client;
 
 
    public MinioResult uploadFile(MultipartFile file, String bucketName) {
        try {
            if (!client.bucketExists(bucketName)) {
                client.makeBucket(bucketName);
                client.setBucketPolicy(bucketName, "public");
            }
 
            String[] split = file.getOriginalFilename().split("\\.");
            String suffix = split[split.length - 1];
 
            if (split.length < 2) {
                suffix = "";
            }
 
            String fileName = StringUtils.join(IdUtil.randomUUID(), ".", suffix);
 
            client.putObject(bucketName, fileName, file.getInputStream(), new PutObjectOptions(file.getSize(), 5 * 1024 * 1024));
            String fileUrl = getFilePath(bucketName, fileName);
            if (StringUtils.isNotBlank(fileUrl)) {
                return new MinioResult(bucketName, fileName, fileUrl, file.getOriginalFilename(), null);
            }
        } catch (Exception e) {
            log.error("上传失败,失败详情信息:{}", e.getMessage());
            throw new UploadFailException();
        }
        return null;
 
    }
 
    public MinioResult uploadFile(ByteArrayInputStream in, String name, String bucketName) {
 
        String[] split = name.split("\\.");
        String suffix = split[split.length - 1];
        if (split.length < 2) {
            suffix = "";
        }
        String fileName = StringUtils.join(IdUtil.randomUUID(), ".", suffix);
        try {
            client.putObject(bucketName, fileName, in, new PutObjectOptions(in.available(), 5 * 1024 * 1024));
            String fileUrl = getFilePath(bucketName, fileName);
            if (StringUtils.isNotBlank(fileUrl)) {
                return new MinioResult(bucketName, fileName, fileUrl, name, null);
            }
        } catch (Exception e) {
            log.error("上传失败,失败详情信息:{}", e.getMessage());
            throw new UploadFailException();
        }
 
        return null;
 
    }
 
    public String uploadImage(ByteArrayInputStream in, String bucketName) {
        try {
            String fileName = IdUtil.randomUUID() + ".jpg";
            client.putObject(bucketName, fileName, in, new PutObjectOptions(in.available(), 5 * 1024 * 1024));
            String fileUrl = getFilePath(bucketName, fileName);
            if (StringUtils.isNotBlank(fileUrl)) {
                return fileUrl;
            }
        } catch (Exception e) {
            log.error("上传失败,失败详情信息:{}", e.getMessage());
            throw new UploadFailException();
        }
 
        return null;
 
    }
 
    public String getFilePath(String bucketName, String fileName) {
        String wordUrl = null;
        try {
            if (client.getObject(bucketName, fileName) == null) {
                return null;
            }
 
            return StringUtils.join("/", bucketName, "/", fileName);
        } catch (Exception e) {
            log.error("获取bucket为:{},文件名为{}时出错,原因:{}", bucketName, fileName, e.getMessage());
        }
 
        return null;
    }
 
 
    @PostConstruct
    public void creatConnect() throws Exception {
        this.client = new MinioClient(url, accessKey, secretKey);
 
 
    }
 
}