中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

如何通過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
宜春市| 鄯善县| 通山县| 石泉县| 固原市| 溆浦县| 遵义市| 洞口县| 弥渡县| 文成县| 平江县| 玛多县| 灵璧县| 榕江县| 舟曲县| 突泉县| 桂阳县| 诸城市| 河西区| 和林格尔县| 汾阳市| 通辽市| 庄河市| 临颍县| 建瓯市| 屏山县| 呼玛县| 柞水县| 聊城市| 布尔津县| 南部县| 高安市| 临城县| 夏河县| 石阡县| 池州市| 新和县| 鄱阳县| 华亭县| 额敏县| 灌云县|