SpringBoot 3.3.1 + Minio 实现极速上传和预览模式
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Service
public class MinioService {
@Autowired
private MinioClient minioClient;
@Value("${minio.bucket-name}")
private String bucketName;
public void uploadFile(String fileName, byte[] data) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
boolean isExist = minioClient.bucketExists(bucketName);
if (!isExist) {
minioClient.makeBucket(bucketName);
}
minioClient.putObject(bucketName, fileName, data, null);
}
public void downloadFile(String fileName, String downloadFileName) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
minioClient.downloadObject(bucketName, fileName, downloadFileName);
}
public String getPresignedObjectUrl(String fileName) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
return minioClient.getPresignedObjectUrl(bucketName, fileName);
}
}
这段代码提供了一个简单的MinIO服务类,用于处理文件的上传、下载和获取预签名URL。它展示了如何使用Spring Boot和MinIO客户端库来与MinIO服务器交互。
评论已关闭