SpringBoot集成MinIO文件服务器

docker-compose安装minio

version: '3' 
services:
  minio:
    image: minio/minio:latest                                    # 原镜像`minio/minio:latest`
    container_name: minio                                        # 容器名为'minio'
    restart: always                                              # 指定容器退出后的重启策略为始终重启
    volumes:                                                     # 数据卷挂载路径设置,将本机目录映射到容器目录
      - "./data:/data"
      - "./minio:/minio"
      - "./config:/root/.minio"
    environment: 
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
      MINIO_PROMETHEUS_AUTH_TYPE: "public"
      MINIO_ACCESS_KEY: "root"                        # 登录账号
      MINIO_SECRET_KEY: "password"                    # 登录密码
    command: server /data
    logging:
      driver: "json-file"
      options:
        max-size: "100m"
    ports:                              # 映射端口
      - "9293:9000"
      - "42330:42330"

  • 运行
docker-compose  -f ./docker-compose-minio.yml -p minio  up -d --build

设置minio基本参数

  • 登录http://localhost:9293/minio输入rootpassword
  • 点击右下角加号选择创建桶create buket
  • 设置桶的权限


集成SpringBoot

  • 引入依赖

        
            io.minio
            minio
            6.0.11
        

        
        
            commons-io
            commons-io
            2.6
        
  • 配置文件
@Getter
@Setter
@ConfigurationProperties(prefix = "minio")
public class MinioProp {

    public static final String MINIO_BUCKET = "mxf";

    private String endpoint;//连接url
    private String accesskey;//用户名
    private String secretKey;//密码
}

@Configuration
@EnableConfigurationProperties(MinioProp.class)
public class MinioConfiguration {
    @Autowired
    private MinioProp minioProp;


    @Bean
    public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
        MinioClient  client =new MinioClient(minioProp.getEndpoint(),minioProp.getAccesskey(),minioProp.getSecretKey());
        return  client;
    }

}
@RestController
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private MinioClient minioClient;


    @GetMapping("/upload")
    public String upload(){
        return "/upload.jsp";
    }

    @ResponseBody
    @PostMapping("/upload")
    public Object upload(@RequestParam(name = "file", required = false) MultipartFile file){
        if(file==null || file.getSize()==0){
            return "上传文件不能为空";
        }
        String orgfileName = file.getOriginalFilename();
        try {
            InputStream in = file.getInputStream();
            String contentType= file.getContentType();
            minioClient.putObject(MinioProp.MINIO_BUCKET,orgfileName,in,null, null, null, contentType);
            Map data=new HashMap<>();
            data.put("bucketName",MinioProp.MINIO_BUCKET);
            data.put("fileName",orgfileName);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "上传失败";
    }

    @RequestMapping("/download/{fileName}")
    public void download(HttpServletResponse response, @PathVariable(name = "fileName") String fileName) {
        InputStream in=null;
        try {
            //获取文件对象 stat原信息
            ObjectStat stat =minioClient.statObject(MinioProp.MINIO_BUCKET, fileName);
            response.setContentType(stat.contentType());
            response.setContentType("application/octet-stream; charset=UTF-8");
            in =   minioClient.getObject(MinioProp.MINIO_BUCKET, fileName);
            IOUtils.copy(in,response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
  • 使用postman测试



  • 浏览器直接访问


你可能感兴趣的:(SpringBoot集成MinIO文件服务器)