0


Minio基本使用(Java)

1 Minio简介

MinIO 是一个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。

官方地址https://min.io/

1.1 支持非结构化的数据存储

1.2 分布式部署

分布式Minio可以让你将多块硬盘(甚至在不同的机器上)组成一个对象存储服务。由于硬盘分布在不同的节点上,分布式Minio避免了单点故障。

在大数据领域,通常的设计理念都是无中心和分布式。Minio分布式模式可以帮助你搭建一个高可用的对象存储服务,你可以使用这些存储设备,而不用考虑其真实物理位置。

Notes:分布式Minio至少需要4****个硬盘,使用分布式Minio自动引入了纠删码功能。

2 使用docker安装并启动Minio服务

2.1 docker安装minio

可以使用:docker search minio查看docker仓库中的各个版本,可选择装指定版本

docker pull minio/minio

docker run -p 9000:9000 minio/minio server /data

安装后使用浏览器访问http://127.0.0.1:9000,如果可以访问,则表示minio已经安装成功。登录名和密码默认:minioadmin

创建bucket test,在springboot中会使用。

2.2 其他安装方式

可参考https://docs.min.io/docs/minio-quickstart-guide.html

3 Springboot 中操作minio

3.1 添加操作依赖

<dependency>

  <groupId>com.jlefebure</groupId>

  <artifactId>spring-boot-starter-minio</artifactId>

  <version>1.1</version>

</dependency>

3.2 配置全局参数

# Minio Host
spring.minio.url=http://10.21.80.17:9000/
# Minio Bucket name for your application
spring.minio.bucket=test
# Minio access key (login)
spring.minio.access-key=minioadmin
# Minio secret key (password)
spring.minio.secret-key=minioadmin

## MULTIPART (MultipartProperties)
# Enable multipart uploads
spring.servlet.multipart.enabled=true
# Threshold after which files are written to disk.
spring.servlet.multipart.file-size-threshold=2KB
# Max file size.
spring.servlet.multipart.max-file-size=500MB
# Max Request Size
spring.servlet.multipart.max-request-size=1024MB

3.3 测试案例:


import com.jlefebure.spring.boot.minio.MinioConfigurationProperties;

import com.jlefebure.spring.boot.minio.MinioException;

import com.jlefebure.spring.boot.minio.MinioService;

import io.minio.messages.Item;

import org.apache.tomcat.util.http.fileupload.IOUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.io.InputStreamResource;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.io.InputStream;

import java.net.URLConnection;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.util.List;

@RestController

public class MinioController {

    @Autowired

    private MinioService minioService;

    @Autowired

    private MinioConfigurationProperties configurationProperties;

    @GetMapping("/files")

    public List<Item> testMinio() throws MinioException {

        return minioService.list();

    }

    @GetMapping("files/{object}")

    public void getObject(@PathVariable("object") String object, HttpServletResponse response) throws com.jlefebure.spring.boot.minio.MinioException, IOException {

        InputStream inputStream = minioService.get(Paths.get(object));

        InputStreamResource inputStreamResource = new InputStreamResource(inputStream);

        // Set the content type and attachment header.

        response.addHeader("Content-disposition", "attachment;filename=" + object);

        response.setContentType(URLConnection.guessContentTypeFromName(object));

        // Copy the stream to the response's output stream.

        IOUtils.copy(inputStream, response.getOutputStream());

        response.flushBuffer();

    }

    @PostMapping
    public void addAttachement(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println(file);
        String filename = file.getOriginalFilename();
    
       
        Path path = Paths.get("/png/"+filename);
        String url = configurationProperties.getUrl() + "/" + configurationProperties.getBucket() + path.toAbsolutePath();
         
        System.out.println(url);

        try {

            minioService.upload(path, file.getInputStream(), file.getContentType());

        } catch (MinioException e) {

            throw new IllegalStateException("The file cannot be upload on the internal storage. Please retry later", e);

        } catch (IOException e) {

            throw new IllegalStateException("The file cannot be read", e);

        }

    }

}
标签: spring boot 后端 java

本文转载自: https://blog.csdn.net/h4241778/article/details/125853546
版权归原作者 码灵 所有, 如有侵权,请联系我们删除。

“Minio基本使用(Java)”的评论:

还没有评论