To download a file from MinIO using Spring Boot, 스프링부트 Minio 사용하기

To download a file from MinIO using Spring Boot, you can utilize the MinIO Java SDK. Here's an example of how you can achieve this:

  1. Add the MinIO dependency to your pom.xml file:

    <dependency>
     <groupId>io.minio</groupId>
     <artifactId>minio</artifactId>
     <version>7.1.0</version>
    </dependency>
    
  2. Create a configuration file (e.g., MinioConfig.java) to establish a connection with your MinIO server: ```java import io.minio.MinioClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;

@Configuration public class MinioConfig {

@Value("${minio.endpoint}")
private String endpoint;

@Value("${minio.accessKey}")
private String accessKey;

@Value("${minio.secretKey}")
private String secretKey;

@Bean
public MinioClient minioClient() {
    return MinioClient.builder()
            .endpoint(endpoint)
            .credentials(accessKey, secretKey)
            .build();
}

}


3. Create a controller class (e.g., `FileController.java`) to handle file download requests:
```java
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.io.InputStream;

@Controller
public class FileController {

    @Autowired
    private MinioClient minioClient;

    @GetMapping("/download/{bucketName}/{objectName}")
    public ResponseEntity<InputStreamResource> downloadFile(
            @PathVariable("bucketName") String bucketName,
            @PathVariable("objectName") String objectName) {
        try {
            InputStream fileStream = minioClient.getObject(bucketName, objectName);
            InputStreamResource resource = new InputStreamResource(fileStream);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", objectName);

            return ResponseEntity.ok()
                    .headers(headers)
                    .body(resource);
        } catch (MinioException | Exception e) {
            e.printStackTrace();
            return ResponseEntity.notFound().build();
        }
    }
}

In this example, the downloadFile method is mapped to the /download/{bucketName}/{objectName} endpoint, where bucketName represents the MinIO bucket name, and objectName represents the name of the file you want to download from MinIO.

  1. Configure the MinIO connection details in your application.properties or application.yml file:
    minio.endpoint=http://your-minio-server:9000
    minio.accessKey=your-access-key
    minio.secretKey=your-secret-key
    

Make sure to replace the placeholder values with your MinIO server's actual details.

With this setup, when you send a GET request to /download/{bucketName}/{objectName}, the file will be downloaded with the specified objectName from the MinIO bucket specified by bucketName.

Remember to handle error scenarios and customize the code according to your specific requirements.

댓글

이 블로그의 인기 게시물

Using the MinIO API via curl

Kafka consumer in a Spring Boot application using a scheduled task