MinIO 简介
MinIO 是一个开源的对象存储服务器,可以通过简单的 HTTP/REST 接口来存储和检索任意数量的数据。它是云原生的,具有高可用性和可扩展性,并且支持多租户。MinIO 可以在本地或在云中运行,支持 S3 API,因此可以很容易地与许多现有的工具和应用程序集成。
Spring Boot 整合 MinIO
在 Spring Boot 应用程序中,可以使用 MinIO Java 客户端来连接和操作 MinIO 服务器。以下是一个使用 MinIO Java 客户端与 MinIO 服务器进行连接的示例:
首先,我们需要在 pom.xml 文件中添加 MinIO 客户端的依赖项:
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.6</version>
</dependency>
接下来,我们需要在 Spring Boot 应用程序中创建一个配置类,用于配置连接到 MinIO 服务器所需的信息。可以使用以下示例配置类:
@Configuration
public class MinioConfig {
@Value("${minio.access-key}")
private String accessKey;
@Value("${minio.secret-key}")
private String secretKey;
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.region}")
private String region;
@Bean
public MinioClient minioClient() throws InvalidEndpointException, InvalidPortException {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.region(region)
.build();
}
}
在上面的代码中,我们使用了 @Value 注解来从应用程序的配置文件中获取连接到 MinIO 服务器所需的信息。然后,我们使用 MinioClient.builder() 方法创建一个 MinIO 客户端,并使用获取的信息进行配置。
最后,我们可以在 Spring Boot 应用程序的任何组件中注入 MinioClient 对象,并使用它来连接和操作 MinIO 服务器。以下是一个使用 MinioClient 对象上传文件到 MinIO 服务器的示例:
@Service
public class MinioService {
@Autowired
private MinioClient minioClient;
public void uploadFile(String bucketName, String objectName, String fileName) throws Exception {
// Check if the bucket exists, and create it if it doesn't exist
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
// Upload the file to the bucket
minioClient.uploadObject(UploadObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.filename(fileName)
.build());
}
}
在上面的代码中,我们首先检查指定的桶是否存在,如果不存在,则创建它。然后,我们使用 MinioClient 对象将指定的文件上传到指定的桶中。
minio工具类代码示例
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
@Component
public class MinioUtils {
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Value("${minio.bucketName}")
private String bucketName;
private MinioClient minioClient;
public MinioUtils() throws InvalidEndpointException, InvalidPortException {
this.minioClient = MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
// 检查桶是否存在
public boolean checkBucketExist(String bucketName) throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, InvalidKeyException, IOException, NoResponseException, XmlPullParserException, ErrorResponseException, InternalException {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
}
// 创建桶
public void createBucket(String bucketName) throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, InvalidKeyException, IOException, NoResponseException, XmlPullParserException, ErrorResponseException, InternalException {
boolean isExist = checkBucketExist(bucketName);
if (!isExist) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
}
// 列出所有桶
public List<Bucket> listBuckets() throws InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, IOException, NoResponseException, XmlPullParserException, ErrorResponseException, InternalException {
return minioClient.listBuckets();
}
// 上传文件
public void uploadFile(InputStream stream, String objectName) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidResponseException, ErrorResponseException, XmlPullParserException, InternalException {
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.stream(stream, stream.available(), -1)
.build());
}
// 删除文件
public void deleteFile(String objectName) throws InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, IOException, NoResponseException, XmlPullParserException, ErrorResponseException, InternalException {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
}
// 下载文件
public InputStream downloadFile(String objectName) throws InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, IOException, NoResponseException, XmlPullParserException, ErrorResponseException, InternalException {
return minioClient.getObject(GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
}
}
在上面的代码中,我们创建了一个 MinioUtils 工具类,其中包含了一些常用的 MinIO 操作方法。其中包括检查桶是否存在、创建桶、列出所有桶、上传文件、删除文件和下载文件等操作。
在这个示例中,我们使用 Spring 的 @Value 注解来注入连接到 Min