说明
官方网站:docs.min.io/
关于minio,官网介绍如下:
MinIO is a high performance object storage solution that provides。
翻译过来的意思就是:
MinIO 是一种高性能对象存储解决方案。
通过官网的简单介绍,大概明白了,minio是可用于对象存储,且高性能。 本文主要介绍的是docker中如何部署单机minio服务,并且通过spring boot整合minio进行桶创建、文件的上传、删除和下载。
部署
通过docker命令部署单机minio,其中映射的端口有9000和9001,9000作为服务端口,而9001作为控制台端口;minio的存储位置被设置在/home/minio/data,配置文件设置在/home/minio/config;账号设置为admin,密码为12345678。
docker run -d \
-p 9000:9000 \
-p 9001:9001 \
--name minio \
-v /home/minio/data:/data \
-v /home/minio/config:/root/.minio \
-e "MINIO_ROOT_USER=admin" \
-e "MINIO_ROOT_PASSWORD=12345678" \
quay.io/minio/minio server /data --console-address ":9001"
整合
创建一个spring boot项目,引入依赖如下
<!-- 用得较多的minio封装类,内部包含丰富的API,可用于代替minio-client -->
<dependency>
<groupId>com.jvm123</groupId>
<artifactId>minio-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
这里引入了minio-spring-boot-starter依赖,内部封装了minio的许多api,看到博客的有缘人可以自行研究发展。这里主要介绍四个方法,分别是桶的创建、文件的上传、下载、删除。
引入minio-spring-boot-starter依赖,需要编写相应的配置文件(application.yaml),如下
file:
store:
minio:
endpoint: ip
bucket: oss-server
access-key: admin
secret-key: 12345678
service层编写如下
@Slf4j
@Service
public class MinioService {
@Autowired
private MinioFileService fileStoreService;
/**
* bucket
* @param bucketName 桶名,全局唯一
* @return 创建结果:成功或失败
*/
public ResultResponse create(@RequestParam("bucketName") String bucketName){
return fileStoreService.createBucket(bucketName)? ResultResponse.success(): ResultResponse.failure("创建oss bucket失败!");
}
/**
* 存储文件
* @param file 上传的文件
* @param bucketName 上传到哪个桶
* @return 上传结果:成功或失败
*/
public ResultResponse upload(@RequestParam("file") MultipartFile file, @RequestParam("bucketName") String bucketName){
try {
fileStoreService.save(bucketName,file.getInputStream(),file.getOriginalFilename());
} catch (IOException e) {
log.error("upload the file is error",e);
return ResultResponse.failure("upload the file is error");
}
return ResultResponse.success();
}
/**
* 删除文件
* @param bucketName 删除文件所在桶
* @param fileName 删除的文件
* @return 删除结果:成功或失败
*/
public ResultResponse delete(@RequestParam("bucketName") String bucketName, @RequestParam("fileName") String fileName){
return fileStoreService.delete(bucketName,fileName)? ResultResponse.success(): ResultResponse.failure("删除oss bucket文件失败!");
}
/**
* 下载文件
* @param bucketName 下载文件所在桶
* @param fileName 下载的文件
* @return 返回文件在响应中
*/
public void download(HttpServletResponse httpServletResponse, @RequestParam("bucketName") String bucketName, @RequestParam("fileName") String fileName){
try (InputStream ignored = fileStoreService.getStream(bucketName, fileName)){
httpServletResponse.addHeader("Content-Disposition","attachment;filename="+fileName);
ServletOutputStream os = httpServletResponse.getOutputStream();
fileStoreService.writeTo(bucketName, fileName, os);
} catch (IOException e) {
log.error("download file is failure!",e);
}
}
}
controller接口主要就是调用这些方法,可自行编写。本文主要是记录一下学习过程,以便后续需要时可翻查。如有雷同,有可能看的是同一个大佬的教程。