5分钟解决SpringBoot集成minio文件服务器的过程
现在流行的文件服务器基本上有minio,fastdfs,那么今天就说一下minio的安装及集成
<dependency>
<groupId>io.miniogroupId>
<artifactId>minioartifactId>
<version>7.0.2version>
dependency>
// 首先进入到你的目录
cd /ext/minio
wget https://dl.minio.io/server/minio/release/linux-amd64/minio
//给予可执行
chmod +x minio
//启动
minio server /ext/data
//或
minio server --config-dir=/ext/minio --address=:9000 //ext/data
//安装基本上就这样,启动起来访问一下 http://xxx.xx.xx.xx:9000/minio。一般默认安装的默认用户名密码为:minioadmin/minioadmin
/**
* 文件管理对象 minio_files
* 这里是获取配置文件中的实体信息
* @author xxc
* @date 2020-06-20
*/
public class MinioFiles extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** $column.columnComment */
private Long id;
@Excel(name = "用户id")
private Long userId;
/** 文件路径 */
@Excel(name = "文件路径")
private String backetName;
/** 文件名称 */
@Excel(name = "文件名称")
private String fileName;
/** 文件大小 */
@Excel(name = "文件大小")
private Long fileSize;
/** 文件后缀 */
@Excel(name = "文件后缀")
private String fileSuffix;
/** 删除标志(0代表存在 2代表删除) */
private String delFlag;
/** 文件类型 */
@Excel(name = "文件类型")
private String fileType;
@Excel(name = "文件原名")
private String fileOriginalName;
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFileOriginalName() {
return fileOriginalName;
}
public void setFileOriginalName(String fileOriginalName) {
this.fileOriginalName = fileOriginalName;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public void setId(Long id)
{
this.id = id;
}
public Long getId()
{
return id;
}
public void setBacketName(String backetName)
{
this.backetName = backetName;
}
public String getBacketName()
{
return backetName;
}
public void setFileName(String fileName)
{
this.fileName = fileName;
}
public String getFileName()
{
return fileName;
}
public void setFileSize(Long fileSize)
{
this.fileSize = fileSize;
}
public Long getFileSize()
{
return fileSize;
}
public void setFileSuffix(String fileSuffix)
{
this.fileSuffix = fileSuffix;
}
public String getFileSuffix()
{
return fileSuffix;
}
public void setDelFlag(String delFlag)
{
this.delFlag = delFlag;
}
public String getDelFlag()
{
return delFlag;
}
public void setFileType(String fileType)
{
this.fileType = fileType;
}
public String getFileType()
{
return fileType;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId()).append("userId", getUserId())
.append("createBy", getCreateBy())
.append("backetName", getBacketName())
.append("fileName", getFileName())
.append("fileSize", getFileSize())
.append("fileSuffix", getFileSuffix())
.append("createTime", getCreateTime())
.append("delFlag", getDelFlag())
.append("fileType", getFileType()).append("fileOriginalName", getFileOriginalName())
.toString();
}
}
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@Component
@Configuration
public class MinIoUtils {
private MinioConfig minIo;
public MinIoUtils(MinioConfig minIo) {
this.minIo = minIo;
}
private MinioClient instance;
@PostConstruct
public void init() {
try {
instance = new MinioClient(minIo.getUrl(),minIo.getAccessKey(),minIo.getSecretKey());
} catch (InvalidPortException e) {
e.printStackTrace();
} catch (InvalidEndpointException e) {
e.printStackTrace();
}
}
/**
* 判断 bucket是否存在
* @param bucketName
* @return
*/
public boolean bucketExists(String bucketName){
try {
return instance.bucketExists(bucketName);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 创建 bucket
* @param bucketName
*/
public void makeBucket(String bucketName){
try {
boolean isExist = instance.bucketExists(bucketName);
if(!isExist) {
instance.makeBucket(bucketName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
* @param bucketName
* @param objectName
* @param filename
*/
public void putObject(String bucketName, String objectName, String filename){
try {
instance.putObject(bucketName,objectName,filename,null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
* @param bucketName
* @param multipartFile
*/
public void putObject(String bucketName, MultipartFile multipartFile,String filename){
try {
// PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
// 文件的ContentType
putObjectOptions.setContentType(multipartFile.getContentType());
instance.putObject(bucketName,filename,multipartFile.getInputStream(),putObjectOptions);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除文件
* @param objectName
*/
public boolean removeObject(String objectName){
boolean flag=true;
try {
instance.removeObject(minIo.getBucketName(),objectName);
} catch (Exception e) {
flag=false;
}
return flag;
}
public void downloadFile(String fileName,String originalName, HttpServletResponse response){
try {
InputStream file = instance.getObject(minIo.getBucketName(),fileName);
String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
if(StringUtils.isNotEmpty(originalName)){
fileName=originalName;
}
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
ServletOutputStream servletOutputStream = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while((len=file.read(buffer)) > 0){
servletOutputStream.write(buffer, 0, len);
}
servletOutputStream.flush();
file.close();
servletOutputStream.close();
} catch (ErrorResponseException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//如果想使用直接注入使用既可以了。
@Service
public class MinioFilesServiceImpl implements IMinioFilesService {
@Autowired
private MinioFilesMapper minioFilesMapper;
@Autowired
private MinIoUtils minIoUtils;
、、、、、、
}