亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

如何通過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
灵武市| 讷河市| 辽源市| 策勒县| 木里| 丽江市| 绥德县| 义马市| 芮城县| 蒲江县| 乾安县| 甘泉县| 克东县| 安塞县| 保定市| 长乐市| 绥阳县| 临泽县| 巴林左旗| 泸溪县| 巴南区| 中卫市| 宣化县| 瑞昌市| 太湖县| 安徽省| 额尔古纳市| 怀仁县| 高邑县| 红桥区| 台南市| 嘉峪关市| 彩票| 贵州省| 兰溪市| 石泉县| 菏泽市| 出国| 沙坪坝区| 海安县| 龙里县|