MinIO:入门

MinIO安装

wget http://dl.minio.org.cn/server/minio/release/linux-amd64/minio
chmod +x minio
nohup ./minio server /data > minio.log 2>&1 &
cat minio.log

image-20210510133355103

默认的账号密码都是minioadmin,进入网址http://192.168.1.40:9000

image-20210510133448005

基本概念

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

MinIO是一个非常轻量的服务,可以很简单的和其他应用的结合,类似 NodeJS, Redis 或者 MySQL。

存储桶可以简单理解为“根文件夹”,每个存储桶都是minio服务下的一个一级结点,其下可以有多个子文件夹。

对象在minio服务里每个存储内容都是一个对象。

在分布式对象存储中,存储桶有一定的限制,具体可见Bucket restrictions and limitations - Amazon Simple Storage Service

存储桶的命名也有一定的限制:

  • 存储桶名称的长度必须介于3到63个字符之间。
  • Bucket名称只能由小写字母、数字、点(.)和连字符(-)组成。
  • Bucket名称必须以字母或数字开头和结尾。
  • Bucket名称不能格式化为IP地址(例如192.168.5.4)。
  • 在分区中,Bucket名称必须是唯一的。分区是一组区域
  • 与amazons3传输加速一起使用的bucket名称中不能有点(.)。

SpringBoot整合Minio

基本配置

其余依赖省略

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.2.1</version>
</dependency>

配置类:

/**
 * @author wen.jie
 * @date 2021/5/10 14:22
 */
@Data
@ConfigurationProperties(prefix = "minio")
public class MinIoProperties {

    private String endpoint;

    private String accessKey;

    private String secretKey;

}

/**
 * @author wen.jie
 * @date 2021/5/10 13:44
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(MinIoProperties.class)
public class MinIOConfig {

    @Bean
    public MinioClient minioClient(MinIoProperties minIoProperties){

        HttpUrl httpUrl = HttpUrl.get(minIoProperties.getEndpoint());
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint(httpUrl)
                        .credentials(minIoProperties.getAccessKey(), minIoProperties.getSecretKey())
                        .build();
        return minioClient;
    }

}

application.yml:

minio:
  endpoint: http://192.168.1.40:9000
  secret-key: minioadmin
  access-key: minioadmin

工具类

其实在MinioClient类中有各种操作文件对象的方法,并且有使用的例子,对照着封装就行了。

@Component
public class MinIOUtil implements ApplicationContextAware {

    private static MinioClient minioClient;

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MinIOUtil.applicationContext = applicationContext;
        MinIOUtil.minioClient = applicationContext.getBean(MinioClient.class);
    }


    /**
     * 查看存储bucket是否存在
     * @param bucketName 存储bucket
     * @return boolean
     */
    public static Boolean bucketExists(String bucketName) {
        Boolean found;
        try {
            found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return found;
    }

    /**
     * 创建存储bucket
     * @param bucketName 存储bucket名称
     * @return Boolean
     */
    public static 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 static Boolean removeBucket(String bucketName) {
        try {
            minioClient.removeBucket(RemoveBucketArgs.builder()
                    .bucket(bucketName)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 文件上传
     * @param file 文件
     * @param bucketName 存储bucket
     * @return Boolean
     */
    public static Boolean upload(MultipartFile file, String bucketName) {
        try {
            PutObjectArgs objectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(file.getOriginalFilename())
                    .stream(file.getInputStream(),file.getSize(),-1)
                    .contentType(file.getContentType())
                    .build();
            //文件名称相同会覆盖
            minioClient.putObject(objectArgs);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 文件下载
     * @param bucketName 存储bucket名称
     * @param fileName 文件名称
     * @param res response
     */
    public static void download(String bucketName, String fileName, HttpServletResponse res) {
        GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
                .object(fileName).build();
        try (GetObjectResponse response = minioClient.getObject(objectArgs)){
            byte[] buf = new byte[1024];
            int len;
            try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()){
                while ((len=response.read(buf))!=-1){
                    os.write(buf,0,len);
                }
                os.flush();
                byte[] bytes = os.toByteArray();
                res.setCharacterEncoding("utf-8");
                //设置强制下载不打开
                res.setContentType("application/force-download");
                res.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
                try (ServletOutputStream stream = res.getOutputStream()){
                    stream.write(bytes);
                    stream.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查看文件对象
     * @param bucketName 存储bucket名称
     * @return 存储bucket内文件对象信息
     */
    public static List<MinIOFile> listObjects(String bucketName) {
        Iterable<Result<Item>> results = minioClient.listObjects(
                ListObjectsArgs.builder().bucket(bucketName).build());
        List<MinIOFile> files = new ArrayList<>();
        try {
            for (Result<Item> result : results) {
                Item item = result.get();
                MinIOFile minIOFile = new MinIOFile();
                minIOFile.setObjectName(URLDecoder.decode(item.objectName(), "utf-8"));
                minIOFile.setIsDir(item.isDir());
                minIOFile.setOwner(item.owner().displayName());
                minIOFile.setSize(item.size());
                minIOFile.setLastModified(item.lastModified().toString());
                files.add(minIOFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
        return files;
    }

    /**
     * 批量删除文件对象
     * @param bucketName 存储bucket名称
     * @param objects 对象名称集合
     */
    public static Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
        List<DeleteObject> dos = objects.stream().map(DeleteObject::new).collect(Collectors.toList());
        return minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
    }

}

文件类:

@Data
@ToString
public class MinIOFile {

    private String objectName;

    private String lastModified;

    private String owner;

    private Long size;

    private Boolean isDir;
}

测试

@RestController
@RequestMapping(path = "/minio", produces = "text/html;charset=UTF-8")
public class MinIOController {

    @RequestMapping("/makeBucket/{bucketName}")
    public String makeBucket(@PathVariable String bucketName){
        Boolean aBoolean = MinIOUtil.makeBucket(bucketName);
        return aBoolean.toString();
    }

    @RequestMapping("/removeBucket/{bucketName}")
    public String removeBucket(@PathVariable String bucketName){
        Boolean aBoolean = MinIOUtil.removeBucket(bucketName);
        return aBoolean.toString();
    }

    @RequestMapping("/{bucketName}/upload")
    public String upload(@PathVariable String bucketName, MultipartFile file){
        Boolean upload = MinIOUtil.upload(file, bucketName);
        return upload.toString();
    }

    @RequestMapping("/listObjects/{bucketName}")
    public List<MinIOFile> listObjects(@PathVariable String bucketName){
        return MinIOUtil.listObjects(bucketName);
    }

    @RequestMapping("/download/{bucketName}/{objectName}")
    public void download(@PathVariable String bucketName, @PathVariable String objectName, HttpServletResponse response){
        MinIOUtil.download(bucketName,objectName,response);
    }

}

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>Title</title>
</head>
<body>
<form action="/minio/wjbucket/upload" method="post" enctype="multipart/form-data">
    <p><input type="file" name="file"></p>
    <p><input type="submit" value="submit"></p>
</form>
</body>
</html>
原文地址:https://www.cnblogs.com/wwjj4811/p/14751553.html