文章目录
- Springboot-cli 开发脚手架系列
- Minio简介
- 1. 环境配置
- 2. 配置MinioClient
- 3. 文件上传下载
- 4. web实战演示
- 6. 源码分享
Minio简介
- 高性能
MinIO 是全球领先的对象存储先锋,目前在全世界有数百万的用户. 在标准硬件上,读/写速度上高达183 GB / 秒 和 171 GB / 秒。
对象存储可以充当主存储层,以处理Spark、Presto、TensorFlow、H2O.ai等各种复杂工作负载以及成为Hadoop HDFS的替代品。
MinIO用作云原生应用程序的主要存储,与传统对象存储相比,云原生应用程序需要更高的吞吐量和更低的延迟。而这些都是MinIO能够达成的性能指标。 - 可扩展性
MinIO利用了Web缩放器的来之不易的知识,为对象存储带来了简单的缩放模型。 这是我们坚定的理念 “简单可扩展.” 在 MinIO, 扩展从单个群集开始,该群集可以与其他MinIO群集联合以创建全局名称空间, 并在需要时可以跨越多个不同的数据中心。 通过添加更多集群可以扩展名称空间, 更多机架,直到实现目标。 - 云的原生支持
MinIO 是在过去4年的时间内从0开始打造的一款软件 ,符合一切原生云计算的架构和构建过程,并且包含最新的云计算的全新的技术和概念。 其中包括支持Kubernetes 、微服和多租户的的容器技术。使对象存储对于 Kubernetes更加友好。
关于minio的介绍就到这,接下来我们进行实战演示。
1. 环境配置- pom.xml
org.springframework.boot spring-boot-starter-web cn.hutool hutool-all 5.7.22 io.minio minio 8.3.9 okhttp com.squareup.okhttp3 com.squareup.okhttp3 okhttp 4.9.0
- yml配置
server: port: 9999 minio: endpoint: http://localhost:20000 accessKey: admin secretKey: 12345678 secure: false bucketName: test2. 配置MinioClient
- MinioProperties
@Data
@Component
@ConfigurationProperties(prefix = MinioProperties.PRE)
public class MinioProperties {
public static final String PRE = "minio";
private String endpoint;
private String accessKey;
private String secretKey;
private Boolean secure;
private String bucketName;
@Bean
public MinioClient getMinioClient() throws Exception {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
3. 文件上传下载
- MinioHandler.java
@Component
@Slf4j
@RequiredArgsConstructor
public class MinioHandler {
private final MinioClient minioClient;
private final MinioProperties minioProperties;
public boolean existBucket(String name) {
try {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build());
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean createdBucket(String bucketName) throws Exception {
if (!this.existBucket(bucketName)) {
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName)
.build());
return true;
}
return false;
}
public boolean removeBucket(String bucketName) {
try {
minioClient.removeBucket(RemoveBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public String saveFile(MultipartFile file) throws Exception {
if (Objects.isNull(file)) {
return "";
}
String fileName;
try {
fileName = file.getOriginalFilename();
InputStream in = file.getInputStream();
minioClient.putObject(PutObjectArgs.builder()
.bucket(minioProperties.getBucketName())
.object(fileName)
.stream(in, in.available(), -1)
.contentType(file.getContentType())
.build()
);
} finally {
Optional.ofNullable(in).ifPresent(inputStream -> {
try {
inputStream.close();
} catch (IOException ignored) {
}
});
}
log.info("文件保存成功,文件名:{},类型:{}", fileName, file.getContentType());
return fileName;
}
public List saveFile(MultipartFile[] files) throws Exception {
List names = new ArrayList<>(files.length);
for (MultipartFile file : files) {
names.add(this.saveFile(file));
}
return names;
}
public InputStream getFile(String fileName) throws Exception {
return minioClient.getObject(GetObjectArgs.builder().bucket(minioProperties.getBucketName()).object(fileName).build());
}
public ResponseEntity download(String fileName) {
return this.download(fileName, MediaType.APPLICATION_OCTET_STREAM);
}
public ResponseEntity download(String fileName, MediaType mediaType) {
ResponseEntity responseEntity = null;
InputStream in = null;
try {
in = this.getFile(fileName);
// 封装返回值
byte[] bytes = IoUtil.readBytes(in);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
headers.setContentLength(bytes.length);
headers.setContentType(mediaType);
headers.setAccessControlExposeHeaders(List.of("*"));
responseEntity = new ResponseEntity<>(bytes, headers, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
} finally {
Optional.ofNullable(in).ifPresent(
i -> {
try {
i.close();
} catch (IOException e) {
e.printStackTrace();
}
}
);
}
return responseEntity;
}
public void listObjects(String bucketName) {
Iterable> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).build());
try {
for (Result- result : results) {
Item item = result.get();
String s = item.objectName();
long size = item.size();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Iterable
> removeObjects(String bucketName, List objects) {
List dos = objects.stream().map(DeleteObject::new).collect(Collectors.toList());
return minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
}
}
4. web实战演示
- 编写MinioController.java文件服务api
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("minio")
public class MinioController {
private final MinioHandler minioHandler;
@GetMapping("/createdBucket")
public boolean createdBucket(String bucketName) throws Exception {
return minioHandler.createdBucket(bucketName);
}
@GetMapping("/removeBucket")
public boolean removeBucket(String bucketName) {
return minioHandler.removeBucket(bucketName);
}
@PostMapping("upload")
public List saveFile(MultipartFile[] files) throws Exception {
return minioHandler.saveFile(files);
}
@GetMapping(value = "download")
public ResponseEntity download(String filename) {
return minioHandler.download(filename);
}
@GetMapping("get/image")
public ResponseEntity getImage(String filename) {
return minioHandler.download(filename, MediaType.IMAGE_JPEG);
}
}
- 以postman测试工具为例子测试
- 文件上传127.0.0.1:9999/minio/upload
- 文件下载``
- 以图片的形式获取
- Springboot-cli开发脚手架,集合各种常用框架使用案例,完善的文档,致力于让开发者快速搭建基础环境并让应用跑起来。
- 项目源码github地址
- 项目源码国内gitee地址



