下面就来分享一下"SpringBoot中整合Minio文件存储的安装部署过程"的攻略吧。
一、安装部署Minio
步骤1:下载Minio
从 Minio的官方网站 下载Minio服务端的压缩包。解压后,可以看到其中包含了可执行的minio程序。
步骤2:启动Minio
执行以下命令启动单节点Minio服务:
./minio server /data
其中/data为数据存放目录,你可以自己设置。
步骤3:访问Minio
默认情况下,Minio使用http://localhost:9000 访问服务,可以在浏览器中打开该链接进行访问。如果可以看到Minio的登录界面,那么恭喜你,Minio服务端已经部署成功。
二、SpringBoot中整合Minio
步骤1:添加Maven依赖
在 pom.xml 中添加以下依赖:
<!-- Minio客户端 -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.2.1</version>
</dependency>
步骤2:配置文件添加Minio信息
在 application.yml 文件中添加以下Minio相关的配置信息:
# Minio客户端设置
minio:
endpoint: http://localhost:9000 # Minio服务端的URL
accessKey: S3_ACCESS_KEY # Minio服务端的Access Key
secretKey: S3_SECRET_KEY # Minio服务端的Secret Key
bucketName: minio-bucket # Minio服务端的存储桶名称
步骤3:编写服务代码
可以定义一个服务类,用于操作Minio存储桶,示例代码如下:
@Service
public class MinioService {
private final MinioClient minioClient;
private final String bucketName;
public MinioService(
@Value("${minio.endpoint}") String endpoint,
@Value("${minio.accessKey}") String accessKey,
@Value("${minio.secretKey}") String secretKey,
@Value("${minio.bucketName}") String bucketName) throws Exception {
this.minioClient = new MinioClient(endpoint, accessKey, secretKey);
this.bucketName = bucketName;
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
}
public void uploadFile(String fileName, byte[] content) throws Exception {
PutObjectArgs args = PutObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.stream(new ByteArrayInputStream(content), content.length, -1)
.build();
minioClient.putObject(args);
}
public byte[] downloadFile(String fileName) throws Exception {
GetObjectArgs args = GetObjectArgs.builder()
.bucket(bucketName)
.object(fileName)
.build();
InputStream is = minioClient.getObject(args);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) != -1) {
bos.write(buf, 0, len);
}
return bos.toByteArray();
}
}
步骤4:测试代码
在Controller类中实现文件上传、下载的测试接口,示例如下:
@RestController
public class FileController {
@Autowired
private MinioService minioService;
@PostMapping("/file/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) {
try {
minioService.uploadFile(file.getOriginalFilename(), file.getBytes());
} catch (Exception e) {
e.printStackTrace();
return "上传文件失败:" + e.getMessage();
}
return "上传文件成功";
}
@GetMapping(value = "/file/download")
public ResponseEntity<byte[]> downloadFile(@RequestParam("fileName") String fileName) {
byte[] content = null;
try {
content = minioService.downloadFile(fileName);
} catch (Exception e) {
e.printStackTrace();
}
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/octet-stream");
headers.add("Content-Disposition", "attachment;filename=" + fileName);
HttpStatus httpStatus = HttpStatus.OK;
ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(content, headers, httpStatus);
return responseEntity;
}
}
三、示例说明
示例1:上传图片到Minio
@GetMapping("/image/upload")
public String uploadImage(@RequestParam("file") MultipartFile file) {
try {
minioService.uploadFile(file.getOriginalFilename(), file.getBytes());
} catch (Exception e) {
e.printStackTrace();
return "上传图片失败:" + e.getMessage();
}
return "上传图片成功";
}
示例2:下载图片到本地
@GetMapping("/image/download")
public void downloadImage(@RequestParam("fileName") String fileName, HttpServletResponse response) {
byte[] content = null;
try {
content = minioService.downloadFile(fileName);
} catch (Exception e) {
e.printStackTrace();
}
if (content != null) {
OutputStream os = null;
try {
os = response.getOutputStream();
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
os.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
以上就是"SpringBoot中整合Minio文件存储的安装部署过程"的完整攻略了。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot中整合Minio文件存储的安装部署过程 - Python技术站