总体步骤
第一步:引入依赖
第二步:配置参数类
第三步:添加MinIoUtil
第四步:接口中引用文章来源:https://www.toymoban.com/news/detail-514352.html
上代码文章来源地址https://www.toymoban.com/news/detail-514352.html
<!--存储文件依赖-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.1.0</version><br> </dependency>
/**
* minio相关配置 也可以配置在yml文件
*/
@Component
public class ParamConfig {
public static String endpoint = "http://127.0.0.1:9000";
public static String accessKey = "minioadmin";
public static String secretKey = "minioadmin";
public static String bucketName = "uavtest";
}
import cn.hutool.core.date.DateUtil;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.messages.Bucket;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
/**
* Minio工具类
*/
@Slf4j
@Component
public class MinIoUtil {
public static MinioClient minioClient;
public static ParamConfig paramConfig;
/**
* 初始化minio配置
*/
@PostConstruct
public void init() {
try {
log.info("Minio Initialize........................");
minioClient = MinioClient.builder().endpoint(paramConfig.endpoint).credentials(paramConfig.accessKey, paramConfig.secretKey).build();
createBucket(paramConfig.bucketName);
log.info("Minio Initialize........................successful");
} catch (Exception e) {
e.printStackTrace();
log.error("初始化minio配置异常: 【{}】", e.fillInStackTrace());
}
}
/**
* 判断bucket是否存在
*/
@SneakyThrows(Exception.class)
public static boolean bucketExists(String bucketName) {
return minioClient.bucketExists(bucketName);
}
/**
* 创建bucket
*/
@SneakyThrows(Exception.class)
public static void createBucket(String bucketName) {
boolean isExist = minioClient.bucketExists(bucketName);
if (!isExist) {
minioClient.makeBucket(bucketName);
}
}
/**
* 获取全部bucket
*/
@SneakyThrows(Exception.class)
public static List<Bucket> getAllBuckets() {
return minioClient.listBuckets();
}
/**
* 文件上传
*
* @param bucketName: 桶名
* @param fileName: 文件名
* @param filePath: 文件路径
*/
@SneakyThrows(Exception.class)
public static void upload(String bucketName, String fileName, String filePath) {
minioClient.putObject(bucketName, fileName, filePath, null);
}
/**
* 上传文件
* 返回可以直接预览文件的URL
*/
public static String uploadFile(MultipartFile file) {
try {
//如果存储桶不存在则创建
if (!bucketExists(ParamConfig.bucketName)) {
createBucket(ParamConfig.bucketName);
}
PutObjectOptions putObjectOptions = new PutObjectOptions(file.getInputStream().available(), -1);
putObjectOptions.setContentType(file.getContentType());
String originalFilename = file.getOriginalFilename();
//得到文件流
InputStream inputStream = file.getInputStream();
//保证文件不重名(并且没有特殊字符)
String fileName = ParamConfig.bucketName + DateUtil.format(new Date(), "_yyyyMMddHHmmss") + originalFilename;
minioClient.putObject(ParamConfig.bucketName, fileName, inputStream, putObjectOptions);
return getPreviewFileUrl(ParamConfig.bucketName, fileName);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 文件上传
* 返回下载文件url地址 和下面upload方法仅传参不同
* bucketName 也可以直接从ParamConfig对象中获取
*/
@SneakyThrows(Exception.class)
public static String upload(String bucketName, String fileName, InputStream stream) {
minioClient.putObject(bucketName, fileName, stream, new PutObjectOptions(stream.available(), -1));
return getPreviewFileUrl(bucketName, fileName);
}
/**
* 文件上传
* 返回下载文件url地址 和上面upload方法仅传参不同
*/
@SneakyThrows(Exception.class)
public static String upload(String bucketName, MultipartFile file) {
final InputStream is = file.getInputStream();
final String fileName = file.getOriginalFilename();
minioClient.putObject(bucketName, fileName, is, new PutObjectOptions(is.available(), -1));
is.close();
return getPreviewFileUrl(bucketName, fileName);
}
/**
* 删除文件
*
* @param bucketName: 桶名
* @param fileName: 文件名
*/
@SneakyThrows(Exception.class)
public static void deleteFile(String bucketName, String fileName) {
minioClient.removeObject(bucketName, fileName);
}
/**
* 下载文件
*/
// @SneakyThrows(Exception.class)
// public static void download(String bucketName, String fileName, HttpServletResponse response) {
// // 获取对象的元数据
// final ObjectStat stat = minioClient.statObject(bucketName, fileName);
// response.setContentType(stat.contentType());
// response.setCharacterEncoding("UTF-8");
// response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
// InputStream is = minioClient.getObject(bucketName, fileName);
// IOUtils.copy(is, response.getOutputStream());
// is.close();
// }
/**
* 获取minio文件的下载或者预览地址
* 取决于调用本方法的方法中的PutObjectOptions对象有没有设置contentType
*
* @param bucketName: 桶名
* @param fileName: 文件名
*/
@SneakyThrows(Exception.class)
public static String getPreviewFileUrl(String bucketName, String fileName) {
return minioClient.presignedGetObject(bucketName, fileName);
}
}
@ResponseBody
@PostMapping("/tFlightFileUpload")
@ApiOperation(value = "文件上传管理-添加minio")
public Response add(@RequestParam("file") MultipartFile file) throws IOException {
String url = MinIoUtil.uploadFile(file);
return Response.success(url);
}```
注意:通过minioClient.presignedGetObject(bucketName, fileName) 获取的链接是下载还是预览的区别是上传文件时是否有指定 ContentType属性
添加了 putObjectOptions.setContentType(file.getContentType());
获取的链接就是预览链接,Minio支持图片及pdf格式文件预览
转自
https://www.cnblogs.com/wffzk/p/15961335.html
到了这里,关于MinIo实现上传下载预览操作指南的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!