先看下前端上传效果
日期工具包使用的是hutool的
import cn.hutool.core.date.DateUtil;
接口可以上传视频和图片暂无做限制
pom.xml
<!--分布式存储-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.0.3</version>
</dependency>
application.yml
minio:
endpoint: http://xx:9999 #Minio服务所在地址
bucketName: xx #存储桶名称
accessKey: xx #访问的key
secretKey: xx #访问的秘钥
url:
http: http://xx.com:9999
@Value(value = "${minio.endpoint}")
private String endpoint;
@Value(value = "${minio.accessKey}")
private String accessKey;
@Value(value = "${minio.secretKey}")
private String secretKey;
@Value("${minio.bucketName}")
private String bucketName;
@Value("${url.http}")
private String utlHttp;
@Autowired
private MinioUtils minioUtils;
@ApiImplicitParam(name = "userId", value = "用户的id", required = true, dataType = "String", paramType = "query")
@PostMapping("/upLoadImage")
public JSONResult upLoadImage(String userId, @RequestParam("file") MultipartFile image) throws Exception {
if (StringUtils.isBlank(userId)) {
return JSONResult.errorMsg("用户id不能为空");
}
if (image.isEmpty()) {
return JSONResult.errorMsg("不能上传空文件哦");
}
//图片保存路径
//String fileUploadPath ="/"+userId+"/image";
String uploadFile=null;
if (image != null && !image.isEmpty()) {
String imageName = image.getOriginalFilename();
if (StringUtils.isNotBlank(imageName)) {
String date = DateUtil.formatDate(new Date());
String filename = DateUtil.currentSeconds() + image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
String imgName = userId + "/" + "img" + "/" + date + "/" + filename;
minIoClientUpload(image.getInputStream(), imgName);
uploadFile = "/" + bucketName + "/" + imgName;
/**这块是属于视频截取头帧,该代码忽略
String videoUrl = endpoint + "/" + bucketName + "/" + imgName;
//获取视频的第一帧图片输出流
InputStream first = MinioUtils.randomGrabberFFmpegImage(videoUrl);
//获取文件名
String fileName = videoUrl.substring(videoUrl.lastIndexOf("/"), videoUrl.lastIndexOf(".")).concat(".jpg");
//将流转化为multipartFile
MultipartFile multipartFile = new MockMultipartFile("file", fileName, "image/jpg", first);
String pictureName = minioUtils.upload(multipartFile);
String pictureUrl = endpoint + "/" + bucketName + "/" + pictureName;
**/
}
} else {
return JSONResult.errorMsg("上传功能出错");
}
User user = new User();
//图片上传最终路径
//图片最终 保存路径
user.setUserAvatar(utlHttp+uploadFile);
//这块使用mybatis-plus业务根据自己需要存储图片
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.eq("user_id", userId);
userService.update(user,updateWrapper);
return JSONResult.ok(uploadFile);
}
/**
* 上传
* @param imgInputStream
* @param objectName
* @throws Exception
*/
public void minIoClientUpload(InputStream imgInputStream, String objectName) throws Exception {
//创建头部信息
Map<String, String> headers = new HashMap<>(10);
//添加自定义内容类型
headers.put("Content-Type", "application/octet-stream");
//添加存储类
headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
//添加自定义/用户元数据
Map<String, String> userMetadata = new HashMap<>(10);
userMetadata.put("My-Project", "Project One");
MinioClient minioClient =
MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
minioClient.putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.stream(imgInputStream, imgInputStream.available(), -1)
.userMetadata(userMetadata)
.build());
imgInputStream.close();
}
使用工具请求接口
上传成功获取地址
登录Minio控制台查看是否有上传成功
标签:endpoint,String,minio,image,springboot,bucketName,上传,分布式 From: https://www.cnblogs.com/rzkwz/p/16851635.html可以看到上传成功了