访问minio地址的图片_在SpringBoot应用中使用Minio作为云存储服务
时间: 2024-02-04 18:03:09 浏览: 101
好的,我理解了你的问题。如果你想在 SpringBoot 应用中使用 Minio 作为云存储服务,可以按照以下步骤进行操作:
1. 添加 Minio 客户端依赖
在 `pom.xml` 文件中添加 Minio 客户端依赖:
```xml
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
```
2. 配置 Minio 客户端
在 `application.properties` 文件中添加以下配置:
```properties
minio.url=http://localhost:9000
minio.access-key=minio
minio.secret-key=minio123
minio.bucket-name=my-bucket
```
其中,`minio.url` 是 Minio 服务的访问地址,`minio.access-key` 和 `minio.secret-key` 是访问 Minio 服务的凭证,`minio.bucket-name` 是存储对象的桶名称。
3. 实现上传文件接口
```java
@RestController
public class FileController {
@Autowired
private MinioClient minioClient;
@PostMapping("/files")
public String uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
String fileName = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
long size = file.getSize();
String contentType = file.getContentType();
minioClient.putObject(PutObjectArgs.builder()
.bucket("my-bucket")
.object(fileName)
.stream(inputStream, size, -1)
.contentType(contentType)
.build());
return "File uploaded successfully: " + fileName;
}
}
```
4. 实现展示图片接口
```java
@RestController
public class ImageController {
@Autowired
private MinioClient minioClient;
@GetMapping("/images/{fileName}")
public ResponseEntity<byte[]> getImage(@PathVariable String fileName) throws Exception {
InputStream inputStream = minioClient.getObject(GetObjectArgs.builder()
.bucket("my-bucket")
.object(fileName)
.build());
byte[] bytes = IOUtils.toByteArray(inputStream);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
headers.setContentLength(bytes.length);
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
}
}
```
在这个示例中,我们定义了一个 `FileController` 和一个 `ImageController`,分别用于上传文件和展示图片。其中,`ImageController` 中的 `getImage` 方法根据文件名从 Minio 服务中获取图片,并返回 `ResponseEntity<byte[]>` 对象,该对象包含图片的二进制数据和响应头信息,用于在浏览器中展示图片。
以上就是在 SpringBoot 应用中使用 Minio 作为云存储服务的步骤。希望能对你有所帮助。
阅读全文
相关推荐
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)