Spring Boot

如何通过Spring Boot API与MinIO进行交互

小樊
85
2024-09-11 19:09:48
栏目: 编程语言

要通过Spring Boot API与MinIO进行交互,您需要遵循以下步骤:

  1. 添加依赖项

在您的pom.xml文件中,添加以下依赖项以使用MinIO Java SDK和Spring Boot:

    <!-- Spring Boot Starter Web -->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- MinIO Java SDK -->
   <dependency>
       <groupId>io.minio</groupId>
       <artifactId>minio</artifactId>
       <version>8.3.0</version>
    </dependency>
</dependencies>
  1. 创建配置类

创建一个名为MinioConfig.java的配置类,其中包含MinIO客户端和连接信息:

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();
    }
}
  1. application.properties文件中添加MinIO连接信息

src/main/resources目录下的application.properties文件中,添加以下内容:

minio.endpoint=play.minio.io
minio.accessKey=YOUR_ACCESS_KEY
minio.secretKey=YOUR_SECRET_KEY

请确保将YOUR_ACCESS_KEYYOUR_SECRET_KEY替换为您的MinIO实例的实际凭据。

  1. 创建MinIO服务类

创建一个名为MinioService.java的服务类,该类将处理与MinIO交互的所有操作:

import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Service
public class MinioService {

    @Autowired
    private MinioClient minioClient;

    public void uploadFile(String bucketName, String objectName, MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .stream(inputStream, file.getSize(), -1)
                .contentType(file.getContentType())
                .build();
        minioClient.putObject(putObjectArgs);
    }

    public InputStream downloadFile(String bucketName, String objectName) throws Exception {
        GetObjectArgs getObjectArgs = GetObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build();
        return minioClient.getObject(getObjectArgs);
    }

    public void deleteFile(String bucketName, String objectName) throws Exception {
        RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build();
        minioClient.removeObject(removeObjectArgs);
    }
}
  1. 创建REST控制器

创建一个名为MinioController.java的控制器类,该类将处理与MinIO交互的所有HTTP请求:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@RestController
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private MinioService minioService;

    @PostMapping("/{bucketName}/{objectName}")
    public ResponseEntity<String> uploadFile(@PathVariable String bucketName, @PathVariable String objectName, @RequestParam("file") MultipartFile file) {
        try {
            minioService.uploadFile(bucketName, objectName, file);
            return new ResponseEntity<>("File uploaded successfully", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/{bucketName}/{objectName}")
    public ResponseEntity<InputStream> downloadFile(@PathVariable String bucketName, @PathVariable String objectName) {
        try {
            InputStream inputStream = minioService.downloadFile(bucketName, objectName);
            return new ResponseEntity<>(inputStream, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @DeleteMapping("/{bucketName}/{objectName}")
    public ResponseEntity<String> deleteFile(@PathVariable String bucketName, @PathVariable String objectName) {
        try {
            minioService.deleteFile(bucketName, objectName);
            return new ResponseEntity<>("File deleted successfully", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

现在,您已经成功地创建了一个Spring Boot应用程序,可以通过API与MinIO进行交互。您可以根据需要扩展此示例,以支持更多操作和功能。

0
看了该问题的人还看了