SpringBoot2.x + Minio 文件服务器 简单使用

Posted 在奋斗的大道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot2.x + Minio 文件服务器 简单使用相关的知识,希望对你有一定的参考价值。

介绍       

项目是为附件上传/文件上传模块,可以作为熟悉了解Minion文件服务器的项目也可以将本模块直接移植进自己的项目中

软件涉及技术

SpringBoot2.x + MyBatis3 +MyBatis3-Plus + mysql8 + Minio(文件服务器) +Swagger2(接口文档框架)

项目效果截图

 

 

 Minio 文件服务截图

项目整体结构:  

项目之pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.zzg</groupId>
	<artifactId>sb-minio</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<!-- 集中定义管理依赖版本号 -->
		<commons-lang.version>2.6</commons-lang.version>
		<commons-codec.version>1.10</commons-codec.version>
		<commons-lang3.version>3.9</commons-lang3.version>
		<commons-net.version>3.6</commons-net.version>
		<commons-io.version>2.6</commons-io.version>
		<commons-collections.version>3.2.1</commons-collections.version>
		<commons-text.version>1.8</commons-text.version>
		<common-fileupload.version>1.3.1</common-fileupload.version>
		<servlet-api.version>3.1.0</servlet-api.version>
		<httpclient.version>4.5.2</httpclient.version>
		<fastjson.version>1.2.48</fastjson.version>
	</properties>

	<dependencies>
		<!--starter -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<!-- test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!--web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 文件系统minio -->
		<dependency>
			<groupId>io.minio</groupId>
			<artifactId>minio</artifactId>
			<version>8.0.0</version>
		</dependency>
		<dependency>
			<groupId>com.squareup.okhttp3</groupId>
			<artifactId>okhttp</artifactId>
			<version>4.9.1</version>
		</dependency>
		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-stdlib</artifactId>
			<version>1.3.70</version>
		</dependency>
		<!-- 数据库连接池druid -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>
		<!--mysql 驱动程序 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!-- Swagger2 集成 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.7.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.7.0</version>
		</dependency>

		<!-- 数据库文档自动生成 -->
		<dependency>
			<groupId>cn.smallbun.screw</groupId>
			<artifactId>screw-core</artifactId>
			<version>1.0.5</version>
		</dependency>
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.30</version>
		</dependency>

		<!--lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<!-- mybatis-plus 集成 -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.4.1</version>
		</dependency>
		<!--validation -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>2.9.0</version>
		</dependency>

		<!--common-lang 常用工具包 -->
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>${commons-lang.version}</version>
		</dependency>
		<!--commons-lang3 工具包 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>${commons-lang3.version}</version>
		</dependency>

		<!--commons-codec 加密工具包 -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>${commons-codec.version}</version>
		</dependency>
		<!--commons-net 网络工具包 -->
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>${commons-net.version}</version>
		</dependency>
		<!--common-io 工具包 -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>${commons-io.version}</version>
		</dependency>
		<!--common-collection 工具包 -->
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>${commons-collections.version}</version>
		</dependency>
		<!--common-fileupload 工具包 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>${common-fileupload.version}</version>
		</dependency>
		<!-- common-text 工具包 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-text</artifactId>
			<version>${commons-text.version}</version>
		</dependency>
		<!-- 集成servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet-api.version}</version>
		</dependency>
		<!-- 集成Apache HttpClient -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>${httpclient.version}</version>
		</dependency>
		<!-- 集成fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>${fastjson.version}</version>
		</dependency>
	</dependencies>

</project>

  数据库 upload_file

CREATE TABLE `upload_file`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `file_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '文件名称',
  `file_path` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '文件路径',
  `file_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '文件类型',
  `file_size` int(11) NULL DEFAULT NULL COMMENT '文件大小',
  `file_desc` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '文件描述',
  `created_by` int(11) NULL DEFAULT NULL COMMENT '创建人',
  `created_dt` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

  upload_file表对应的类

com.zzg.entity.UploadFile.java

com.zzg.mapper.UploadFileMapper.java

com.zzg.service.UploadFileService.java

com.zzg.service.impl.UploadFileServiceImpl.java

mapper/UploadFileMapper.xml

 Minion核心代码片段:

package com.zzg.minio.core;

import java.io.InputStream;

/**
 * minio 核心接口服务定义
 * @author zzg
 *
 */
public interface MinioUploadInteface {
	/**
	 * 
	 * @Title: uploadFile   
	 * @Description: 文件上传  
	 * @param: @param file
	 * @param: @param bucket
	 * @param: @param contentType
	 * @param: @param suffix
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	String uploadFile(InputStream stream, String bucket, String contentType, String suffix);
	
	/**
	 * 
	 * @Title: removeFile   
	 * @Description: 文件删除   
	 * @param: @param objectKey
	 * @param: @param bucket
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	boolean removeFile(String objectKey, String bucket);
	
	/**
	 * 
	 * @Title: downloadFile   
	 * @Description: 文件下载  
	 * @param: @param objectKey
	 * @param: @param bucket
	 * @param: @return      
	 * @return: File      
	 * @throws
	 */
	InputStream downloadFile(String objectKey, String bucket);
	
	/**
	 * 
	 * @Title: createBucket   
	 * @Description: 创建桶,并指定桶策略   
	 * @param: @param bucket
	 * @param: @param policy
	 * @param: @return      
	 * @return: boolean      
	 * @throws
	 */
	boolean createBucket(String bucket);
	
	/**
	 * 
	 * @Title: removeBucket   
	 * @Description: 移除指定桶  
	 * @param: @param bucket
	 * @param: @return      
	 * @return: boolean      
	 * @throws
	 */
	boolean removeBucket(String bucket);
	
	/**
	 * 
	 * @Title: setBucketPolicy   
	 * @Description: 设置桶策略   
	 * @param: @param bucket
	 * @param: @param policy
	 * @param: @return      
	 * @return: boolean      
	 * @throws
	 */
	boolean setBucketPolicy(String bucket, String policy);

}

 

package com.zzg.minio.entity;

import java.io.Serializable;

/**
 * minio 服务器连接实体对象定义
 * @author zzg
 *
 */
@SuppressWarnings("serial")
public class MinioUploadEntity implements Serializable {
	private String url = "";
	private String access = "";
	private String secret = "";

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getAccess() {
		return access;
	}

	public void setAccess(String access) {
		this.access = access;
	}

	public String getSecret() {
		return secret;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

	public MinioUploadEntity(String url, String access, String secret) {
		super();
		this.url = url;
		this.access = access;
		this.secret = secret;
	}

}

 

package com.zzg.minio.policy;

import io.minio.MinioClient;

/**
 * minio 桶策略接口
 * @author zzg
 *
 */
public interface BucketPolicyInterface {
	/**
	 * 
	 * @Title: createBucketPolicy   
	 * @Description: 设置桶策略 
	 * @param: @param client
	 * @param: @param bucket
	 * @param: @return      
	 * @return: boolean      
	 * @throws
	 */
	boolean createBucketPolicy(MinioClient client, String bucket);

}

 

package com.zzg.minio.policy.impl;

import com.zzg.minio.policy.BucketPolicyInterface;

import io.minio.MinioClient;

/**
 * Minio 桶 默认策略
 * @author zzg
 *
 */
public class BuckerDefaultPolicy implements BucketPolicyInterface  {

	@Override
	public boolean createBucketPolicy(MinioClient client, String bucket) {
		// TODO Auto-generated method stub
		return false;
	}

}
package com.zzg.minio.policy.impl;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zzg.minio.policy.BucketPolicyInterface;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;

/**
 * Minio 桶 读策略
 * @author zzg
 *
 */
public class BuckerReadPolicy implements BucketPolicyInterface {

	// 日志记录
	public static final Logger logger = LoggerFactory.getLogger(BuckerReadPolicy.class);
	/**
	 * 桶占位符
	 */
	private static final String BUCKET_PARAM = "${bucket}";
	/**
	 * bucket权限-只读
	 */
	private static final String READ_ONLY = "{\\"Version\\":\\"2012-10-17\\",\\"Statement\\":[{\\"Effect\\":\\"Allow\\",\\"Principal\\":{\\"AWS\\":[\\"*\\"]},\\"Action\\":[\\"s3:GetBucketLocation\\",\\"s3:ListBucket\\"],\\"Resource\\":[\\"arn:aws:s3:::"
			+ BUCKET_PARAM
			+ "\\"]},{\\"Effect\\":\\"Allow\\",\\"Principal\\":{\\"AWS\\":[\\"*\\"]},\\"Action\\":[\\"s3:GetObject\\"],\\"Resource\\":[\\"arn:aws:s3:::"
			+ BUCKET_PARAM + "/*\\"]}]}";

	@Override
	public boolean createBucketPolicy(MinioClient client, String bucket) {
		// TODO Auto-generated method stub
		try {
			client.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucket)
					.config(READ_ONLY.replace(BUCKET_PARAM, bucket)).build());
			return true;
		} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
				| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
				| IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("error: {}", e.getMessage(), e);
		}
		return false;
	}

}
package com.zzg.minio.policy.impl;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zzg.minio.policy.BucketPolicyInterface;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
 /**
  * Minio 桶  读写策略
  * @author zzg
  *
  */
public class BuckerReadWriterPolicy implements BucketPolicyInterface {
	// 日志记录
	public static final Logger logger = LoggerFactory.getLogger(BuckerReadWriterPolicy.class);
	 /**
     * 桶占位符
     */
    private static final String BUCKET_PARAM = "${bucket}";
    
    /**
     * bucket权限-读写
     */
    private static final String READ_WRITE = "{\\"Version\\":\\"2012-10-17\\",\\"Statement\\":[{\\"Effect\\":\\"Allow\\",\\"Principal\\":{\\"AWS\\":[\\"*\\"]},\\"Action\\":[\\"s3:GetBucketLocation\\",\\"s3:ListBucket\\",\\"s3:ListBucketMultipartUploads\\"],\\"Resource\\":[\\"arn:aws:s3:::" + BUCKET_PARAM + "\\"]},{\\"Effect\\":\\"Allow\\",\\"Principal\\":{\\"AWS\\":[\\"*\\"]},\\"Action\\":[\\"s3:DeleteObject\\",\\"s3:GetObject\\",\\"s3:ListMultipartUploadParts\\",\\"s3:PutObject\\",\\"s3:AbortMultipartUpload\\"],\\"Resource\\":[\\"arn:aws:s3:::" + BUCKET_PARAM + "/*\\"]}]}";
 
	@Override
	public boolean createBucketPolicy(MinioClient client, String bucket) {
		// TODO Auto-generated method stub
		try {
			client.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucket).config(READ_WRITE.replace(BUCKET_PARAM, bucket)).build());
			return true;
		} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
				| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
				| IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("error: {}", e.getMessage(), e);
		}
		return false;
	}
 
}

package com.zzg.minio.policy.impl;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zzg.minio.policy.BucketPolicyInterface;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;

/**
 * Minio 桶 写策略
 * @author zzg
 *
 */
public class BuckerWritePoliy implements BucketPolicyInterface {
	// 日志记录
	public static final Logger logger = LoggerFactory.getLogger(BuckerWritePoliy.class);
	/**
	 * 桶占位符
	 */
	private static final String BUCKET_PARAM = "${bucket}";

	/**
	 * bucket权限-只读
	 */
	private static final String WRITE_ONLY = "{\\"Version\\":\\"2012-10-17\\",\\"Statement\\":[{\\"Effect\\":\\"Allow\\",\\"Principal\\":{\\"AWS\\":[\\"*\\"]},\\"Action\\":[\\"s3:GetBucketLocation\\",\\"s3:ListBucketMultipartUploads\\"],\\"Resource\\":[\\"arn:aws:s3:::"
			+ BUCKET_PARAM
			+ "\\"]},{\\"Effect\\":\\"Allow\\",\\"Principal\\":{\\"AWS\\":[\\"*\\"]},\\"Action\\":[\\"s3:AbortMultipartUpload\\",\\"s3:DeleteObject\\",\\"s3:ListMultipartUploadParts\\",\\"s3:PutObject\\"],\\"Resource\\":[\\"arn:aws:s3:::"
			+ BUCKET_PARAM + "/*\\"]}]}";

	@Override
	public boolean createBucketPolicy(MinioClient client, String bucket) {
		// TODO Auto-generated method stub
		try {
			client.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucket)
					.config(WRITE_ONLY.replace(BUCKET_PARAM, bucket)).build());
			return true;
		} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
				| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
				| IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("error: {}", e.getMessage(), e);
		}
		return false;
	}

}
package com.zzg.minio.policy.content;

/**
 * minio 常量属性定义
 * @author zzg
 *
 */
public class BuckerPolicyContent {
	public static final String READ ="read";
	
	public static final String WRITE ="write";
	
	public static final String READ_WRITE ="read-write";
}
package com.zzg.minio.factory;

import java.util.HashMap;
import java.util.Map;

import com.zzg.minio.policy.BucketPolicyInterface;
import com.zzg.minio.policy.content.BuckerPolicyContent;
import com.zzg.minio.policy.impl.BuckerDefaultPolicy;
import com.zzg.minio.policy.impl.BuckerReadPolicy;
import com.zzg.minio.policy.impl.BuckerReadWriterPolicy;
import com.zzg.minio.policy.impl.BuckerWritePoliy;
/**
 * 桶策略工厂
 * @author zzg
 *
 */
public class BuckerPolicyFactory {
	static Map<String, BucketPolicyInterface> operationMap = new HashMap<>();
	static {
		operationMap.put(BuckerPolicyContent.READ, new BuckerReadPolicy());// 只读
		operationMap.put(BuckerPolicyContent.WRITE, new BuckerWritePoliy());// 只写
		operationMap.put(BuckerPolicyContent.READ_WRITE, new BuckerReadWriterPolicy());// 读写
	}
	
	public static BucketPolicyInterface getBucketPolicyInterface(String poliy){
		BucketPolicyInterface object = operationMap.get(poliy);
		if(object == null){
			object = new BuckerDefaultPolicy();
		}
		return object;
	}
}
package com.zzg.minio.util;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zzg.common.util.UUIDUtils;
import com.zzg.minio.core.MinioUploadInteface;
import com.zzg.minio.entity.MinioUploadEntity;
import com.zzg.minio.factory.BuckerPolicyFactory;
import com.zzg.minio.policy.BucketPolicyInterface;

import io.minio.BucketExistsArgs;
import io.minio.GetObjectArgs;
import io.minio.GetObjectResponse;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveBucketArgs;
import io.minio.RemoveObjectArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.minio.http.Method;
import io.minio.messages.Bucket;

/**
 * Minio 封装工具类方法:包含桶创建、删除、文件上传、文件预览、文件下载、文件删除等相关基础功能
 * @author zzg
 *
 */
public class MinioUploadUtil implements MinioUploadInteface {
	// 日志记录
		public static final Logger logger = LoggerFactory.getLogger(MinioUploadUtil.class);
		
		private MinioUploadEntity entity;
		
		public MinioUploadEntity getEntity() {
			return entity;
		}
	 
		public void setEntity(MinioUploadEntity entity) {
			this.entity = entity;
		}
		
		
		public MinioUploadUtil(MinioUploadEntity entity) {
			super();
			this.entity = entity;
		}
		
		public MinioClient createMinioClient(){
			//创建MinioClient对象
	        MinioClient minioClient = MinioClient.builder()
	                .endpoint(entity.getUrl())
	                .credentials(entity.getAccess(), entity.getSecret())
	                .build();
			return minioClient;
		}
		
	 
		@Override
		public String uploadFile(InputStream stream, String bucket, String contentType, String suffix) {
			// TODO Auto-generated method stub
			//创建MinioClient对象
	        MinioClient minioClient = this.createMinioClient();
	        // 生成文件上传唯一名称
	        String objectKey =UUIDUtils.getUUID().concat(".").concat(suffix);
	        // 文件流上传
	        try {
				minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectKey).stream(stream, stream.available(), -1).contentType(contentType).build());
				// 返回文件上传的唯一标识
				return objectKey;
			} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
					| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
					| IllegalArgumentException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			}
			return null;
		}
	 
		@Override
		public boolean removeFile(String objectKey, String bucket) {
			// TODO Auto-generated method stub
			//创建MinioClient对象
	        MinioClient minioClient = this.createMinioClient();
	        try {
				minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectKey).build());
				return true;
			} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
					| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
					| IllegalArgumentException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			}
			return false;
		}
	 
		@Override
		public GetObjectResponse downloadFile(String objectKey, String bucket) {
			// TODO Auto-generated method stub
			//创建MinioClient对象
	        MinioClient minioClient = this.createMinioClient();
	        try {
	        	// 返回文件流
	        	GetObjectResponse response = minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectKey).build());
	        	System.out.print("Headers:--------"+ response.headers().toString());
				return response;
			} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
					| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
					| IllegalArgumentException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			}
			return null;
		}
		
		/**
		 * 生成上传图片预览地址
		 * @param objectKey
		 * @param bucket
		 * @return
		 * @throws InvalidKeyException
		 * @throws ErrorResponseException
		 * @throws InsufficientDataException
		 * @throws InternalException
		 * @throws InvalidResponseException
		 * @throws NoSuchAlgorithmException
		 * @throws XmlParserException
		 * @throws ServerException
		 * @throws IllegalArgumentException
		 * @throws IOException
		 */
		public String getPreviewAddress(String objectKey, String bucket) throws InvalidKeyException, ErrorResponseException, InsufficientDataException, InternalException, InvalidResponseException, NoSuchAlgorithmException, XmlParserException, ServerException, IllegalArgumentException, IOException{
			//创建MinioClient对象
	        MinioClient minioClient = this.createMinioClient();
	        return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucket).object(objectKey).build());
		}
		
		public List<String> getBucketName(){
			List<String> list = new ArrayList<String>();
			//创建MinioClient对象
	        MinioClient minioClient = this.createMinioClient();
			List<Bucket> bucketList = null;
			try {
				bucketList = minioClient.listBuckets();
			} catch (InvalidKeyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ErrorResponseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InsufficientDataException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InternalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidResponseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XmlParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for (Bucket bucket : bucketList) {
			  System.out.println(bucket.creationDate() + ", " + bucket.name());
			  list.add(bucket.name());
			}
			return list;
		}
		
		@Override
		public boolean createBucket(String bucket) {
			// TODO Auto-generated method stub
			boolean target = false;
			//创建MinioClient对象
	        MinioClient minioClient = this.createMinioClient();
	        // 判断桶是否存在
	        BucketExistsArgs exist = BucketExistsArgs.builder().bucket(bucket).build();
	        
	        try {
	        	boolean result = minioClient.bucketExists(exist);
	        	if(!result){
	        		MakeBucketArgs create =MakeBucketArgs.builder().bucket(bucket).build();
	        		minioClient.makeBucket(create);
	        		target = true;
	        	}
				
			} catch (InvalidKeyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			} catch (ErrorResponseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			} catch (InsufficientDataException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			} catch (InternalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			} catch (InvalidResponseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			} catch (ServerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			} catch (XmlParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			}
	 
			return target;
		}
	 
		@Override
		public boolean removeBucket(String bucket) {
			// TODO Auto-generated method stub
			//创建MinioClient对象
	        MinioClient minioClient = this.createMinioClient();
	        // 判断桶是否存在
	        BucketExistsArgs exist = BucketExistsArgs.builder().bucket(bucket).build();
	        try {
				boolean result = minioClient.bucketExists(exist);
				if(result){
					minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
					return true;
				}
			} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
					| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
					| IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error("error: {}", e.getMessage(), e);
			}
			return false;
		}
		
		@Override
		public boolean setBucketPolicy(String bucket, String policy) {
			// TODO Auto-generated method stub
			//创建MinioClient对象
	        MinioClient minioClient = this.createMinioClient();
	        // 判断桶是否存在
	        BucketExistsArgs exist = BucketExistsArgs.builder().bucket(bucket).build();
	        
	        try {
				boolean result = minioClient.bucketExists(exist);
				if(result){
					BucketPolicyInterface bucketPolicy = BuckerPolicyFactory.getBucketPolicyInterface(policy);
					return bucketPolicy.createBucketPolicy(minioClient, bucket);
				} 
			} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
					| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
					| IOException e) {
				// TODO Auto-generated catch block
				logger.error("error: {}", e.getMessage(), e);
			}
			return false;
		}

}

MiniO 文件服务器相关资料

Minio 开发者文档:https://docs.min.io/docs/

Windows 搭建Minion:https://blog.csdn.net/zhouzhiwengang/article/details/115542399

项目源码地址:

链接:https://pan.baidu.com/s/1SaiUE36p8YTyDAuP_970Nw 
提取码:1234

以上是关于SpringBoot2.x + Minio 文件服务器 简单使用的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot2.x配置文件讲解

SpringBoot2.x文件上传实战

SpringBoot2.x文件上传大小单位设置

SpringBoot2.X 项目使用外置绝对路径的配置文件

文件上传到minio,获取文件列表,下载文件

SpringBoot2.x访问不了application.properties文件中servlet.context-path上下文路径