以下是一个简化的Spring Boot微服务接入MinIO实现文件服务的代码示例:
@Configuration
public class MinioConfig {
@Value("${minio.url}")
private String url;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Bean
public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, XmlParserException {
return new MinioClient(url, accessKey, secretKey);
}
}
@Service
public class MinioService {
@Autowired
private MinioClient minioClient;
public boolean bucketExists(String bucketName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, XmlParserException {
boolean isExist = minioClient.bucketExists(bucketName);
return isExist;
}
public void createBucket(String bucketName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, XmlParserException {
minioClient.makeBucket(bucketName);
}
public void uploadFile(MultipartFile file, String bucketName, String objectName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, XmlParserException {
minioClient.putObject(bucketName, objectName, file.getInputStream(), file.getContentType());
}
public Stream<Path> loadFile(String bucketName, String objectName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, XmlParserException {
InputStream inputStream = minioClient.getObject(bucketName, objectName);
return IOUtils.toBuffered(inputStream).lines().onClose(() -> {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
@RestController
public class MinioController {
@Autowired
private MinioService minioService;
@PostMapping("/upload")
public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("bucket") String bucket, @RequestParam("object") String object) {
try {
minioService.uploadFile(file, bucket, o