记录minio 文件上传
MinIO提供多个语言版本SDK的支持,下边找到java版本的文档:
地址:https://docs.min.io/docs/java-client-quickstart-guide.html
maven依赖如下:
XML
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.3</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.8.1</version>
</dependency>
需要三个参数才能连接到minio服务。
参数 说明
Endpoint 对象存储服务的URL
Access Key Access key 就像用户ID,可以唯一标识你的账户。
Secret Key Secret key 是你账户的密码。
1.上传
1.1 配置注入
package com.zhan.Config;
import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* minio 配置注入
*/
@Data
@ConfigurationProperties(prefix = "minio")
@Configuration
public class MinioConfiguration {
private String endpoint;
private String accessKey;
private String secretKey;
private String bucketName;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
1.2 上传一个文件
/**
* 上传一个文件
* @param stream
* @param bucket
* @param objectName // 文件名
* @throws Exception
*/
public void uploadFile(InputStream stream, String bucket, String objectName) throws Exception {
minioClient
.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
.stream(stream, -1, 10485760).build());
}
@Test
public void upload() {
//根据扩展名取出mimeType
ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(".mp4");
String mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE;//通用mimeType,字节流
if(extensionMatch!=null){
mimeType = extensionMatch.getMimeType();
}
try {
UploadObjectArgs testbucket = UploadObjectArgs.builder()
.bucket("testbucket")
// .object("test001.mp4")
.object("001/test001.mp4")//添加子目录
.filename("D:\\develop\\upload\\1mp4.temp")
.contentType(mimeType)//默认根据扩展名确定文件内容类型,也可以指定
.build();
minioClient.uploadObject(testbucket);
System.out.println("上传成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("上传失败");
}
}
2.删除
@Test
public void delete(){
try {
minioClient.removeObject(
RemoveObjectArgs.builder().bucket("testbucket").object("001/test001.mp4").build());
System.out.println("删除成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("删除失败");
}
}
3. ☆查询文件 (后端处理同文件判断,同一文件秒传)
@Transactional
@ApiOperation("上传一个文件")
@RequestMapping(value = "/uploadfild",method = RequestMethod.POST)
public ResponseUtils fileupload(@RequestParam("file") MultipartFile multipartFile, @RequestParam(defaultValue = "****") String bucket, @RequestParam(required = false) String objectName) throws Exception {
MediaResources mediaResources = new MediaResources();
/**
* 方法默认的保存路径为:C:\Documents and Settings\Administrator\Local Settings\Temp
*/
//立即删除文件 file.delete(); //在JVM退出时删除文件 file.deleteOnExit();
File tempFile = File.createTempFile("minio","temp");
/**
* 先获取流 后面会改变
*/
InputStream stream = multipartFile.getInputStream();
/**
* 上传的文件拷贝到临时文件
*/
multipartFile.transferTo(tempFile);
String absolutePath = tempFile.getAbsolutePath();
FileInputStream fileInputStream = new FileInputStream(new File(absolutePath));
String s_md5 = DigestUtils.md5Hex(fileInputStream);
//删除
tempFile.delete();
/**
* 数据放入
*/
mediaResources.setFileUniqueValue(s_md5);
mediaResources.setBucket(bucket);
mediaResources.setAccessAddress("/"+bucket+"/"+multipartFile.getOriginalFilename());
mediaResources.setUploadedBy("zhan_yuan");
mediaResources.setUploadTime(new Date());
mediaResources.setState("以审核");
String[] s = multipartFile.getOriginalFilename().split("\\.");
ContentInfo ex = ContentInfoUtil.findExtensionMatch("."+s[1]);
mediaResources.setFileType(ex.getMimeType());
mediaResources.setFileName(multipartFile.getOriginalFilename());
/**
* 新增
*/
mediaResourcesService.save(mediaResources);
minioUtil.createBucket(bucket);
if(objectName != null) minioUtil.uploadFile(stream,bucket,objectName+"/"+multipartFile.getOriginalFilename());
else minioUtil.uploadFile(stream,bucket,multipartFile.getOriginalFilename());
return ResponseUtils.success("/"+bucket+"/"+multipartFile.getOriginalFilename());
}