If your Spring Boot application needs to upload, store, or retrieve files, connecting it to an object storage service like MinIO is a great choice. MinIO is fast, lightweight, and works just like Amazon S3. but you can run it locally, on your own server, or in the cloud.
In this article, you’ll learn how to connect your Spring Boot app to a MinIO server using values stored in the
application.yml file.
This is a clean and flexible way to manage configuration, especially when you’re working in different environments like development and production.
Let’s break it down step-by-step.
Step 1: Add the MinIO SDK to Your Project
To let your Spring Boot app talk to MinIO, you’ll need the MinIO Java SDK.
If you're using Maven, add this 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'
Once added, let your build tool download the dependencies.
Step 3: Bind YAML Values to a Configuration Class
Create a class that reads the values from
application.yml and makes them available in your application.
@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 you annotate the class with
@Component and
@ConfigurationProperties. Also, don’t forget to enable configuration property support by adding this annotation in your main application class:
@SpringBootApplication
@EnableConfigurationProperties(MinioProperties.class)
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
Step 4: Create a MinIO Client Bean
Now, you’ll use the values from your MinioProperties class to build a MinioClient bean. This bean can be injected anywhere in your app where you need to interact with MinIO.
@Configuration
public class MinioConfig {
@Bean
public MinioClient minioClient(MinioProperties properties) {
return MinioClient.builder()
.endpoint(properties.getUrl())
.credentials(properties.getAccessKey(), properties.getSecretKey())
.build();
}
}
This MinioClient is now ready to use in services for uploading, downloading, or listing files.
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