forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
13 changes: 13 additions & 0 deletions
13
lab-72-minio/src/main/java/cn/iocoder/springboot/lab72/MinIOApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
lab-72-minio/src/main/java/cn/iocoder/springboot/lab72/config/MinIOConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
lab-72-minio/src/main/java/cn/iocoder/springboot/lab72/controller/FileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters