Skip to content

Commit

Permalink
新增 MinIO 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Mar 19, 2022
1 parent 9023aa0 commit f41616c
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 3 deletions.
30 changes: 30 additions & 0 deletions lab-72-minio/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-72-minio</artifactId>

<dependencies>
<!-- 实现对 Spring MVC 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- MinIO 客户端 -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.2</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab72;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MinIOApplication {

public static void main(String[] args) {
SpringApplication.run(MinIOApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cn.iocoder.springboot.lab72.config;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Configuration
public class MinIOConfiguration {

@Bean
public MinioClient minioClient() {
// Minio 配置。实际项目中,定义到 application.yml 配置文件中
String endpoint = "http://127.0.0.1:9000";
String accessKey = "admin";
String secretKey = "password";

// 创建 MinioClient 客户端
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cn.iocoder.springboot.lab72.controller;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class FileController {

@Resource
private MinioClient minioClient;

// Minio 配置。实际项目中,定义到 application.yml 配置文件中
private String endpoint = "http://127.0.0.1:9000";
private String bucket = "yudaoyuanma";

@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) throws Exception {
// 上传
String path = UUID.randomUUID().toString(); // 文件名,使用 UUID 随机
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucket) // 存储桶
.object(path) // 文件名
.stream(file.getInputStream(), file.getSize(), -1) // 文件内容
.contentType(file.getContentType()) // 文件类型
.build());
// 拼接路径
return String.format("%s/%s/%s", endpoint, bucket, path);
}

@DeleteMapping("/delete")
public void delete(@RequestParam("path") String path) throws Exception {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket(bucket) // 存储桶
.object(path) // 文件名
.build());
}

}
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<!-- <module>lab-06</module>-->
<!-- <module>lab-07</module>-->
<!-- <module>lab-11-spring-data-redis</module>-->
<module>lab-12-mybatis</module>
<!-- <module>lab-12-mybatis</module>-->
<!-- <module>lab-13-spring-data-jpa</module>-->
<!-- <module>lab-14-spring-jdbc-template</module>-->
<!-- <module>lab-15-spring-data-es</module>-->
Expand All @@ -31,7 +31,7 @@
<!-- <module>lab-21</module>-->
<!-- <module>lab-22</module>-->
<!-- <module>lab-23</module>-->
<module>lab-24</module>
<!-- <module>lab-24</module>-->
<!-- <module>lab-25</module>-->
<!-- <module>lab-26</module>-->
<!-- <module>lab-27</module>-->
Expand Down Expand Up @@ -78,6 +78,7 @@
<!-- <module>lab-68-spring-security-oauth</module>-->
<!-- <module>lab-69-proxy</module>-->
<!-- <module>lab-70-db-doc</module>-->
<!-- <module>lab-71-http-debug</module>-->

<!-- Spring Cloud 示例 -->
<!-- <module>labx-01-spring-cloud-alibaba-nacos-discovery</module>-->
Expand Down Expand Up @@ -111,7 +112,7 @@
<!-- <module>labx-28</module>-->
<!-- <module>labx-29-spring-cloud-consul-bus</module>-->
<!-- <module>labx-30-spring-cloud-grpc</module>-->
<module>lab-71-http-debug</module>
<module>lab-72-minio</module>
</modules>

</project>

0 comments on commit f41616c

Please sign in to comment.