docker安装minio
package com.example.radar.service.impl;
import com.example.config.MinioConfig;
import com.example.radar.service.MinioService;
import io.minio.MinioClient;
import io.minio.policy.PolicyType;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* @author ychao
* @date 2021/6/19 20:00
*/
@Service("/minioService")
public class MinioServiceImpl implements MinioService {
@Override
public Map minioUpload(MultipartFile file, String dirPath) {
Map<String, String> minioMap = new HashMap<>();
String filename = file.getOriginalFilename();
try {
//创建一个MinIO的Java客户端
MinioClient minioClient = new MinioClient(MinioConfig.ENDPOINT, MinioConfig.ACCESS_KEY, MinioConfig.SECRET_KEY);
boolean isExist = minioClient.bucketExists(MinioConfig.BUCKET_NAME);
//没有桶则创建
if (!isExist) {
//创建存储桶并设置只读权限
minioClient.makeBucket(MinioConfig.BUCKET_NAME);
minioClient.setBucketPolicy(MinioConfig.BUCKET_NAME, "*.*", PolicyType.READ_ONLY);
}
// 设置存储对象名称
String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
String objectName = dirPath + "/" + uuid + "_" + filename;
// 使用putObject上传一个文件到存储桶中
minioClient.putObject(MinioConfig.BUCKET_NAME, objectName, file.getInputStream(), file.getContentType());
minioMap.put("fileName", filename);
minioMap.put("fileUrl", MinioConfig.ENDPOINT + "/" + MinioConfig.BUCKET_NAME + "/" + objectName);
} catch (Exception e) {
e.printStackTrace();
}
return minioMap;
}
@Override
public void minioDownload(String fileUrl,String fileName,HttpServletResponse response) {
OutputStream outputStream = null;
InputStream inputStream = null;
if (StringUtils.isBlank(fileUrl)) {
response.setHeader("Content-type", "text/html;charset=UTF-8");
System.out.println("fileUrl为null文件下载失败");
}
try {
// 拿到文件路径
String url = fileUrl.split("9000/")[1];
// 获取文件对象
inputStream = getMinioObjectInputStream(MinioConfig.BUCKET_NAME, url.substring(url.indexOf("/") + 1));
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
} catch (Exception ex) {
response.setHeader("Content-type", "text/html;charset=UTF-8");
System.out.println("文件下载失败");
} finally {
try {
if (null != outputStream) {
outputStream.close();
}
if (null != inputStream) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* create by: ychao
* description: 获取minio文件中的输入流
* create time: 2021/6/19 22:51
*/
public InputStream getMinioObjectInputStream(String bucketName, String objectName) {
try {
MinioClient minioClient = new MinioClient(MinioConfig.ENDPOINT, MinioConfig.ACCESS_KEY, MinioConfig.SECRET_KEY);
return minioClient.getObject(bucketName, objectName);
} catch (Exception e) {
System.out.println("minio文件输入流获取失败");
}
return null;
}
}