1、pom文件
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.0.3</version>
</dependency>
2、minio配置
minio:
url: http://192.168.5.202:9000
accessKey: admin
secretKey: admin123
bucketName: test
3、minio配置
package com.pojo.file.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.minio.MinioClient;
/**
* Minio 配置信息
*
* @author ruoiy
*/
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig
{
/**
* 服务地址
*/
private String url;
/**
* 用户名
*/
private String accessKey;
/**
* 密码
*/
private String secretKey;
/**
* 存储桶名称
*/
private String bucketName;
public String getUrl()
{
return url;
}
public void setUrl(String url)
{
this.url = url;
}
public String getAccessKey()
{
return accessKey;
}
public void setAccessKey(String accessKey)
{
this.accessKey = accessKey;
}
public String getSecretKey()
{
return secretKey;
}
public void setSecretKey(String secretKey)
{
this.secretKey = secretKey;
}
public String getBucketName()
{
return bucketName;
}
public void setBucketName(String bucketName)
{
this.bucketName = bucketName;
}
@Bean
public MinioClient getMinioClient()
{
return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
}
}
4、上传和删除文件实现
package com.pojo.file.service.impl;
import com.pojo.file.domain.SysFileInfo;
import com.pojo.file.service.SysFileService;
import com.pojo.file.utils.FileUploadUtils;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.DeleteObject;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.pojo.file.config.MinioConfig;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;
import java.util.List;
/**
* Minio 文件存储
*
* @author ruoyi
*/
@Service
@Primary
public class MinioSysFileServiceImpl implements SysFileService {
@Autowired
private MinioConfig minioConfig;
@Autowired
private MinioClient client;
@PostConstruct
public void init() {
boolean isExist = false;
try {
isExist = client.bucketExists(BucketExistsArgs.builder().bucket(minioConfig.getBucketName()).build());
if (!isExist) {
// 创建一个名为asiatrip的存储桶,用于存储照片的zip文件。
client.makeBucket(MakeBucketArgs.builder().bucket(minioConfig.getBucketName()).build());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 本地文件上传接口
*
* @param file 上传的文件
* @return 访问地址
* @throws Exception
*/
@Override
public String uploadFile(MultipartFile file) throws Exception {
String fileName = FileUploadUtils.extractFilename(file);
PutObjectArgs args = PutObjectArgs.builder()
.bucket(minioConfig.getBucketName())
.object(fileName)
.stream(file.getInputStream(), file.getSize(), -1)
.contentType(file.getContentType())
.build();
client.putObject(args);
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
}
@Override
public void deleteFromFileSys(SysFileInfo sysFileInfo) {
//删除文件
// http://192.168.5.202:9000/test/2021/04/27/710fd081-fb91-4d15-abcb-f7ff70afa7f6.mp4
//从url 获取object name bucket 后面全是object name
String baseUrl = minioConfig.getUrl() + "/" + minioConfig.getBucketName();
String objectName = sysFileInfo.getFilePath().replace(baseUrl, "");
RemoveObjectArgs args = RemoveObjectArgs.builder().bucket(minioConfig.getBucketName())
.object(objectName)
.build();
try {
client.removeObject(args);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void batchDeleteFromFileSys(List<SysFileInfo> sysFileInfoList) {
String baseUrl = minioConfig.getUrl() + "/" + minioConfig.getBucketName();
List<DeleteObject> objects = new LinkedList<>();
sysFileInfoList.forEach(j -> {
String objectName = j.getFilePath().replace(baseUrl, "");
objects.add(new DeleteObject(objectName));
});
RemoveObjectsArgs args = RemoveObjectsArgs.builder().bucket(minioConfig.getBucketName())
.objects(objects)
.build();
Iterable<io.minio.Result<io.minio.messages.DeleteError>> iterable = client.removeObjects(args);
iterable.forEach(j -> {
});
}
}
FileUploadUtil
package com.pojo.file.utils;
import java.io.File;
import java.io.IOException;
import com.pojo.common.core.utils.SecurityUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;
import com.pojo.common.core.exception.file.FileNameLengthLimitExceededException;
import com.pojo.common.core.exception.file.FileSizeLimitExceededException;
import com.pojo.common.core.exception.file.InvalidExtensionException;
import com.pojo.common.core.utils.DateUtils;
import com.pojo.common.core.utils.IdUtils;
import com.pojo.common.core.utils.StringUtils;
import com.pojo.common.core.utils.file.MimeTypeUtils;
/**
* 文件上传工具类
*
* @author ruoyi
*/
public class FileUploadUtils {
/**
* 默认的文件名最大长度 100
*/
public static final int DEFAULT_FILE_NAME_LENGTH = 200;
/**
* 资源映射路径 前缀
*/
@Value("${file.prefix}")
public String localFilePrefix;
/**
* 根据文件路径上传
*
* @param baseDir 相对应用的基目录
* @param file 上传的文件
* @return 文件名称
* @throws IOException
*/
public static final String upload(String baseDir, MultipartFile file) throws IOException {
try {
return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
} catch (Exception e) {
throw new IOException(e.getMessage(), e);
}
}
/**
* 文件上传
*
* @param baseDir 相对应用的基目录
* @param file 上传的文件
* @return 返回上传成功的文件名
* @throws FileSizeLimitExceededException 如果超出最大大小
* @throws FileNameLengthLimitExceededException 文件名太长
* @throws IOException 比如读写文件出错时
* @throws InvalidExtensionException 文件校验异常
*/
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
InvalidExtensionException {
int fileNamelength = file.getOriginalFilename().length();
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
}
assertAllowed(file, allowedExtension);
String fileName = extractFilename(file);
File desc = getAbsoluteFile(baseDir, fileName);
file.transferTo(desc);
String pathFileName = getPathFileName(fileName);
return pathFileName;
}
/**
* 编码文件名
*/
public static final String extractFilename(MultipartFile file) {
String fileName = file.getOriginalFilename();
String extension = getExtension(file);
fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
return fileName;
}
private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
File desc = new File(uploadDir + File.separator + fileName);
if (!desc.exists()) {
if (!desc.getParentFile().exists()) {
desc.getParentFile().mkdirs();
}
}
return desc;
}
private static final String getPathFileName(String fileName) throws IOException {
String pathFileName = "/" + fileName;
return pathFileName;
}
/**
* 文件大小校验
*
* @param file 上传的文件
* @return
* @throws FileSizeLimitExceededException 如果超出最大大小
* @throws InvalidExtensionException
*/
public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
throws FileSizeLimitExceededException, InvalidExtensionException {
String fileName = file.getOriginalFilename();
String extension = getExtension(file);
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) {
throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
fileName);
} else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) {
throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
fileName);
} else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) {
throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
fileName);
} else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) {
throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
fileName);
} else {
throw new InvalidExtensionException(allowedExtension, extension, fileName);
}
}
}
/**
* 判断MIME类型是否是允许的MIME类型
*
* @param extension
* @param allowedExtension
* @return
*/
public static final boolean isAllowedExtension(String extension, String[] allowedExtension) {
for (String str : allowedExtension) {
if (str.equalsIgnoreCase(extension)) {
return true;
}
}
return false;
}
/**
* 获取文件名的后缀
*
* @param file 表单文件
* @return 后缀名
*/
public static final String getExtension(MultipartFile file) {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
if (StringUtils.isEmpty(extension)) {
extension = MimeTypeUtils.getExtension(file.getContentType());
}
return extension;
}
}
public List<String> catpture(String serial, int num) {
Map<String, Object> paramMap = Maps.newHashMap();
paramMap.put("accessToken", this.getVideoAccessToken());
paramMap.put("deviceSerial", serial.toUpperCase());
paramMap.put("channelNo", "1");
List<String> urlList = Lists.newArrayList();
for (int i = 0; i < num; i++) {
String result = HttpUtil.post("https://open.ys7.com/api/lapp/device/capture", paramMap);
if (result.contains("\"code\":\"200\"")) {
JSONObject jsonObject = JSONUtil.parseObj(result);
JSONObject data = jsonObject.getJSONObject("data");
String picUrl = data.getStr("picUrl");
// 从url获取文件流
byte[] files = HttpUtil.downloadBytes(picUrl);
String fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + ".jpg";
//上传文件流
PutObjectArgs args = PutObjectArgs.builder()
.bucket(minioConfig.getBucketName())
.object(fileName)
.stream(new ByteArrayInputStream(files), files.length, -1)
.build();
minioClient.putObject(args);
urlList.add(minioConfig.getFileUrl() + "/" + minioConfig.getBucketName() + "/" + fileName);
}
}
return urlList;
}
注意,批量删除文件必须要对删除操作返回的结果进行迭代
问题:一开始一直按照官网示例,进行试验,发现却没有效果。原因请注意一句话:
Iterable<Result<DeleteError>> - Lazy iterator contains object removal status.
一定要对results 进行迭代。
Minio上传本地文件
public String upload(String fileLoc) {
String fileName = LocalDateUtil.getCurrentTimeStr(LocalDateUtil.DATE_PATTERN_yyyyMMdd1) + "/" + UUID.fastUUID().toString() + ".jpg";
UploadObjectArgs uploadObjectArgs = null;
try {
uploadObjectArgs = UploadObjectArgs.builder()
.bucket(minioConfig.getBucketName())
//文件绝对路径
.filename(fileLoc)
//文件名
.object(fileName)
.build();
} catch (IOException e) {
e.printStackTrace();
}
if (Objects.isNull(uploadObjectArgs)) {
return null;
}
try {
minioClient.uploadObject(uploadObjectArgs);
} catch (Exception e) {
e.printStackTrace();
}
String url = minioConfig.getFileUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
return url;
}