springboot minio上传 下载
时间: 2023-10-16 16:08:51 浏览: 134
Spring Boot与Minio结合可以实现文件的上传和下载。首先,需要在application.yaml中配置文件上传的大小限制和Minio的参数。接下来,引入Minio依赖并编写配置类和工具类。配置类需要注入Minio的参数,并使用@Bean注解将MinioClient实例化。工具类可以通过MinioClient实现文件的上传和下载操作。
相关问题
Springboot minio上传文件夹
Spring Boot 与 MinIO 结合可以方便地处理文件上传,包括整个文件夹。MinIO 是一个开源的对象存储服务器,适合做为云存储解决方案。在 Spring Boot 中,你可以使用第三方库如 `spring-cloud-storage` 或者直接 MinIO 官方提供的 SDK 来实现文件上传。
以下是简单的步骤:
1. **添加依赖**:
- 添加 `spring-cloud-starter-netflix-eureka-client` 如果你在微服务架构中,并且需要与 Eureka 注册中心交互。
- 添加 `spring-cloud-starter-minio` 或者 `minio-java` 作为 MinIO 的客户端支持。
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
</dependency>
```
2. **配置 MinIO**:
- 配置 application.properties 或者 application.yml 文件,提供 MinIO 的 endpoint、access key 和 secret key 等信息。
```yaml
spring:
cloud:
minio:
endpoint: http://your-minio-server:9000
accessKey: YOUR_ACCESS_KEY
secretKey: YOUR_SECRET_KEY
```
3. **创建上传服务**:
- 使用 `@Service` 或 `@RestController` 创建一个上传方法,接受文件或文件夹路径,然后通过 MinIO SDK 将其上传到指定的 bucket(存储桶)。
```java
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
@Service
public class FileUploader {
private final MinioClient minioClient;
public FileUploader(MinioClient minioClient) {
this.minioClient = minioClient;
}
@PostMapping("/upload-folder")
public void uploadFolder(@RequestParam("folderPath") String folderPath, MultipartFile[] files) {
for (MultipartFile file : files) {
if (file.getSize() > 0) {
try {
minioClient.putObject(bucketName, file.getOriginalFilename(), file.getInputStream());
} catch (Exception e) {
// handle exceptions
}
}
}
}
}
```
4. **处理文件夹上传**:
- 如果你想上传整个文件夹,你需要遍历目录,将每个子文件作为单独的请求上传。这通常不在 Web 层面上处理,更适合在批处理任务中。
springboot minio下载大文件
### 实现Spring Boot MinIO 大文件下载
在构建基于Spring Boot的应用程序时,为了实现大文件的上传和下载功能,通常会选择MinIO作为对象存储服务。由于大多数Spring Boot应用程序只需要少量配置即可运行[^1],集成MinIO也不例外。
#### 添加依赖项
首先,在`pom.xml`中加入必要的依赖:
```xml
<dependency>
<groupId>io.minio</groupId>
<artifactId|minio|version>8.5.2</version>
</dependency>
```
#### 配置MinIO客户端连接属性
接着定义application.properties中的MinIO服务器访问参数:
```properties
minio.endpoint=http://localhost:9000
minio.accessKey=YOUR-KEY
minio.secretKey=YOUR-SECRET-KEY
minio.bucketName=my-bucket-name
```
#### 创建MinIO工具类
编写用于操作MinIO的服务层代码如下所示:
```java
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
@Service
public class MinIoService {
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Bean
public MinioClient minioClient() throws Exception {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
```
对于大型文件下载而言,推荐采用流的方式处理数据传输过程,这样可以有效减少内存占用并提高效率。下面是一个具体的例子来展示如何通过HTTP响应输出流完成这一目标:
```java
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
@RestController
public class FileController {
private final MinioClient minioClient;
private static final String BUCKET_NAME = "my-bucket-name";
public FileController(MinioClient minioClient) {
this.minioClient = minioClient;
}
@GetMapping("/download/{fileName:.+}")
public void downloadFile(@PathVariable String fileName, HttpServletResponse response) throws Exception {
InputStream stream = minioClient.getObject(
GetObjectArgs.builder().bucket(BUCKET_NAME).object(fileName).build());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, bytesRead);
}
stream.close();
}
}
```
上述方法能够确保即使面对非常庞大的文件也能平稳地将其发送给请求方。
阅读全文
相关推荐














