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

   2024-09-30 3820
核心提示:要通过Spring Boot API与MinIO进行交互,您需要遵循以下步骤:添加依赖项在您的pom.xml文件中,添加以下依赖项以使用MinIO Java

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

添加依赖项

在您的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>
创建配置类

创建一个名为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;@Configurationpublic 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();    }}
在application.properties文件中添加MinIO连接信息

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

minio.endpoint=play.minio.iominio.accessKey=YOUR_ACCESS_KEYminio.secretKey=YOUR_SECRET_KEY

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

创建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;@Servicepublic 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);    }}
创建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进行交互。您可以根据需要扩展此示例,以支持更多操作和功能。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号