要通過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;
@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();
}
}
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_KEY
和YOUR_SECRET_KEY
替換為您的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);
}
}
創建一個名為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進行交互。您可以根據需要擴展此示例,以支持更多操作和功能。