• springboot+vue实现Minio文件存储


    安装minio

    首先点击进入MINIO官网,进行一个minio服务器的下载

    在这里插入图片描述

    下载好了之后在本地磁盘找一个文件夹,把下载的exe放入文件夹,再新建一个文件夹准备存放数据和文件

    在这里插入图片描述

    在当前目录cmd进入控制台,输入代码

    minio.exe server data
    
    • 1

    成功后会有访问路径以及账号密码
    在这里插入图片描述

    访问这个路径,输入刚刚控制台打印的账号密码就可以进入

    http://127.0.0.1:9000
    在这里插入图片描述

    进入后创建buckets

    在这里插入图片描述
    在这里插入图片描述

    后端

    在官网找到依赖并导入

    在这里插入图片描述

    <dependency>
        <groupId>io.minio</groupId>
        <artifactId>minio</artifactId>
        <version>8.4.3</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    添加yml文件配置

    # Minio配置 wyj配置
    minio:
      endpoint: http://127.0.0.1:9000
      accessKey: minioadmin
      secretKey: minioadmin
      bucketName: svt-ttt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    MinIoClientConfig配置文件

    这里bean文件只能有这一个,如果是多人团队项目开发记得在测试的时候协商好
    @Value的注解导入是spring的原生注解

    package com.wedu.config;
    
    
    import io.minio.MinioClient;
    import lombok.Data;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    
    /*
     * Minio的配置类
     * */
    @Data
    @Component
    public class MinIoClientConfig {
        @Value("${minio.endpoint}")
        private String endpoint;
        @Value("${minio.accessKey}")
        private String accessKey;
        @Value("${minio.secretKey}")
        private String secretKey;
        @Value("${minio.bucket-name}")
        private String bucketName;
        /**
         * 注入minio 客户端
         *
         * @return
         */
        @Bean
        public MinioClient minioClient() {
            return MinioClient.builder()
                    .endpoint(endpoint)
                    .credentials(accessKey, secretKey)
                    .build();
        }
    
    }
    
    • 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

    工具类MinIOUtil

    工具类里有很多可使用的方法,这里只用到了uploadFile

    package com.wedu.modules.tain.utils;
    
    import com.amazonaws.util.IOUtils;
    import com.wedu.modules.equi.entity.ObjectItem;
    import io.minio.*;
    import io.minio.messages.DeleteError;
    import io.minio.messages.DeleteObject;
    import io.minio.messages.Item;
    import lombok.SneakyThrows;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    @Component
    public class MinIOUtil {
        @Autowired
        private MinioClient minioClient;
    
        @Value("${minio.endpoint}")
        private String endpoint;
        @Value("${minio.access-key}")
        private String accessKey;
        @Value("${minio.secret-key}")
        private String secretKey;
        @Value("${minio.bucket-name}")
        private String bucketName;
    
        /**
         * 上传文件到指定的存储桶中
         *
         * @param bucketName 存储桶名称
         * @param file 上传的文件
         * @param objectName 存储对象的名称
         * @param contentType 文件的内容类型
         * @return 文件上传的响应对象
         * @throws Exception 如果上传过程中发生异常
         */
        @SneakyThrows(Exception.class)
        public ObjectWriteResponse uploadFile(String bucketName, MultipartFile file, String objectName, String contentType) {
            InputStream inputStream = file.getInputStream();
            return minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .contentType(contentType)
                            .stream(inputStream, inputStream.available(), -1)
                            .build());
        }
    
        /**
         * description: 判断bucket是否存在,不存在则创建
         *
         * @return: void
         */
        public void existBucket(String name) {
            try {
                boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build());
                if (!exists) {
                    minioClient.makeBucket(MakeBucketArgs.builder().bucket(name).build());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 创建存储bucket
         * @param bucketName 存储bucket名称
         * @return Boolean
         */
        public Boolean makeBucket(String bucketName) {
            try {
                minioClient.makeBucket(MakeBucketArgs.builder()
                        .bucket(bucketName)
                        .build());
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    
        /**
         * 删除存储bucket
         * @param bucketName 存储bucket名称
         * @return Boolean
         */
        public Boolean removeBucket(String bucketName) {
            try {
                minioClient.removeBucket(RemoveBucketArgs.builder()
                        .bucket(bucketName)
                        .build());
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
        /**
         * description: 上传文件
         *
         * @param multipartFile
         * @return: java.lang.String
    
         */
        public List<String> upload(MultipartFile[] multipartFile) {
            List<String> names = new ArrayList<>(multipartFile.length);
            for (MultipartFile file : multipartFile) {
                String fileName = file.getOriginalFilename();
                String[] split = fileName.split("\\.");
                if (split.length > 1) {
                    fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
                } else {
                    fileName = fileName + System.currentTimeMillis();
                }
                InputStream in = null;
                try {
                    in = file.getInputStream();
                    minioClient.putObject(PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .stream(in, in.available(), -1)
                            .contentType(file.getContentType())
                            .build()
                    );
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                names.add(fileName);
            }
            return names;
        }
    
        /**
         * description: 下载文件
         *
         * @param fileName
         * @return: org.springframework.http.ResponseEntity
         */
        public ResponseEntity<byte[]> download(String fileName) {
            ResponseEntity<byte[]> responseEntity = null;
            InputStream in = null;
            ByteArrayOutputStream out = null;
            try {
                in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
                out = new ByteArrayOutputStream();
                IOUtils.copy(in, out);
                //封装返回值
                byte[] bytes = out.toByteArray();
                HttpHeaders headers = new HttpHeaders();
                try {
                    headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                headers.setContentLength(bytes.length);
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                headers.setAccessControlExposeHeaders(Arrays.asList("*"));
                responseEntity = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return responseEntity;
        }
    
        /**
         * 查看文件对象
         * @param bucketName 存储bucket名称
         * @return 存储bucket内文件对象信息
         */
        public List<ObjectItem> listObjects(String bucketName) {
            Iterable<Result<Item>> results = minioClient.listObjects(
                    ListObjectsArgs.builder().bucket(bucketName).build());
            List<ObjectItem> objectItems = new ArrayList<>();
            try {
                for (Result<Item> result : results) {
                    Item item = result.get();
                    ObjectItem objectItem = new ObjectItem();
                    objectItem.setObjectName(item.objectName());
                    objectItem.setSize(item.size());
                    objectItems.add(objectItem);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return objectItems;
        }
    
        /**
         * 批量删除文件对象
         * @param bucketName 存储bucket名称
         * @param objects 对象名称集合
         */
        public Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
            List<DeleteObject> dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
            Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
            return results;
        }
    }
    
    
    • 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
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240

    controller调用uploadFile

        @Autowired
        private MinIOUtil minIOUtil;
        @Autowired
        private MinIoClientConfig minIoClientConfig;
        //minio
        @PostMapping("/minIoUpload")
        public R minIoUpload(@RequestParam("file") MultipartFile file) throws IOException {
            //文件名
            String fileName = file.getOriginalFilename();
            String newFileName = System.currentTimeMillis() + "." + StringUtils.substringAfterLast(fileName, ".");
            //类型
            String contentType = file.getContentType();
            minIOUtil.uploadFile(minIoClientConfig.getBucketName(), file, newFileName, contentType);
            return R.ok("上传成功");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    postman测试

    测试成功
    在这里插入图片描述
    在这里插入图片描述

    前端

    主页面和springboot+vue实现oss文件存储一样,在弹窗的下拉框中添加一个2,访问路径变为自己的post的访问路径即可

  • 相关阅读:
    记一次在amd架构打包arm64架构的镜像的试错经历
    MMDetection自定义模型训练
    Nacos安装指南(Windows环境)
    java开发四年之旅
    325.高端的电影资讯响应式网页 大学生期末大作业 Web前端网页制作 html+css+js
    9-6 Prometheus告警通知Alertmanager,结合邮箱,钉钉,企业微信实现告警,告警模板使用,告警分类发送
    基于Python实现的模拟退火算法
    【搭建私人图床】使用LightPicture开源搭建图片管理系统并远程访问
    各大搜索引擎的User-Agent
    03 编译Java虚拟机
  • 原文地址:https://blog.csdn.net/qq_52879387/article/details/136354793