springboot接入minio
时间: 2023-11-04 11:59:45 浏览: 118
minio 安装步骤以及springboot 如何集成minio
好的,你的问题是springboot如何接入minio,这个问题涉及到在springboot中引入minio依赖、配置连接信息以及使用minio进行文件上传等方面。具体步骤可以参考以下内容:
1. 在pom.xml中引入minio依赖:
```xml
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.1.0</version>
</dependency>
```
2. 在application.yml或application.properties中配置minio连接信息:
```yaml
spring:
minio:
endpoint: http://minio.example.com
accessKey: accesskey
secretKey: secretkey
```
3. 创建MinioTemplate类用于封装MinioClient操作:
```java
@Configuration
public class MinioConfig {
@Value("${spring.minio.endpoint}")
private String endpoint;
@Value("${spring.minio.accessKey}")
private String accessKey;
@Value("${spring.minio.secretKey}")
private String secretKey;
@Value("${spring.minio.bucketName}")
private String bucketName;
@Bean
public MinioClient minioClient() throws Exception {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
@Bean
public MinioTemplate minioTemplate(MinioClient minioClient) {
return new MinioTemplate(minioClient, bucketName);
}
}
public class MinioTemplate {
private final MinioClient minioClient;
private final String bucketName;
public MinioTemplate(MinioClient minioClient, String bucketName) {
this.minioClient = minioClient;
this.bucketName = bucketName;
}
public void upload(String objectName, InputStream inputStream, long size, String contentType) throws Exception {
PutObjectArgs args = PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.stream(inputStream, size, -1)
.contentType(contentType)
.build();
minioClient.putObject(args);
}
public void remove(String objectName) throws Exception {
RemoveObjectArgs args = RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build();
minioClient.removeObject(args);
}
public String getObjectUrl(String objectName) {
return minioClient.getObjectUrl(bucketName, objectName);
}
}
```
4. 在Controller中使用MinioTemplate上传文件:
```java
@RestController
public class FileController {
@Autowired
private MinioTemplate minioTemplate;
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) throws Exception {
String objectName = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
long size = file.getSize();
String contentType = file.getContentType();
minioTemplate.upload(objectName, inputStream, size, contentType);
String objectUrl = minioTemplate.getObjectUrl(objectName);
return objectUrl;
}
}
```
以上就是springboot接入minio的大致步骤,具体细节还需要根据实际情况进行调整。
阅读全文