If you’re building a Spring Boot application that handles files—whether it’s uploads, downloads, or storage—you’ll need a place to store those files safely and efficiently. That’s where MinIO comes in. It’s an open-source, high-performance object storage solution that works just like Amazon S3 but can run anywhere—even on your local machine.
In this article, we’ll walk you through how to connect your Spring Boot application to a MinIO server using the
application.properties file for configuration. This method is perfect if you prefer traditional .properties files over YAML.
Let’s break it down step by step.
Step 1: Add MinIO SDK to Your Project
First, you need to include the MinIO Java SDK in your project so you can connect and interact with the MinIO server.
If you use Maven, add the following to your pom.xml:
<dependency>
<groupid>io.minio</groupid>
<artifactid>minio</artifactid>
<version>8.5.7</version>
</dependency>
If you're using Gradle, add this to your build.gradle:
implementation 'io.minio:minio:8.5.7'
Step 3: Create a Properties Class to Load MinIO Values
Create a class to load and store the values from application.properties. Spring Boot will automatically bind the properties based on the prefix.
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
private String url;
private String accessKey;
private String secretKey;
// Getters and setters
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
}
Make sure Spring Boot knows to scan this class by enabling configuration properties. Add this to your main application class:
@SpringBootApplication
@EnableConfigurationProperties(MinioProperties.class)
public class YourApp {
public static void main(String[] args) {
SpringApplication.run(YourApp.class, args);
}
}
Step 4: Build a MinIO Client Bean
Next, use the values from MinioProperties to create a MinioClient bean that can be injected into any service.
@Configuration
public class MinioConfig {
@Bean
public MinioClient minioClient(MinioProperties properties) {
return MinioClient.builder()
.endpoint(properties.getUrl())
.credentials(properties.getAccessKey(), properties.getSecretKey())
.build();
}
}
Once this is in place, you can use the MinioClient anywhere in your app to interact with MinIO—uploading files, downloading, or managing buckets.
Step 5: Test the Connection
To make sure your connection works, create a simple test service that lists existing buckets or creates one.
@Service
public class MinioService {
private final MinioClient minioClient;
public MinioService(MinioClient minioClient) {
this.minioClient = minioClient;
}
public void checkAndCreateBucket(String bucketName) {
try {
boolean exists = minioClient.bucketExists(
BucketExistsArgs.builder().bucket(bucketName).build()
);
if (!exists) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
System.out.println("✅ Bucket created: " + bucketName);
} else {
System.out.println("ℹ️ Bucket already exists: " + bucketName);
}
} catch (Exception e) {
System.err.println("❌ Could not connect to MinIO: " + e.getMessage());
}
}
}
You can run this test on application startup using a CommandLineRunner:
@Bean
public CommandLineRunner testMinio(MinioService service) {
return args -> service.checkAndCreateBucket("example-bucket");
}
image quote pre code