MinIO上传和下载文件及文件完整性校验.
package com.xuecheng.media;
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import io.minio.*;
import io.minio.errors.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
public class MinioTest {
String url = "http://localhost:9000";
String accessKey = "accessKey";
String secretKey = "secretKey";
String filePath = "/Users/lcm/Movies/测试视频/1.mp4";
String bucketName = "testbucket";
String objectName = "1.mp4";
MinioClient minioClient = MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
@Test
public void testUpload() throws Exception {
ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(".mp4");
String mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
if (extensionMatch != null) {
mimeType = extensionMatch.getMimeType();
}
try {
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("testbucket").build());
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket("testbucket").build());
} else {
System.out.println("文件桶已存在!!!");
}
uploadFile(minioClient, filePath, bucketName, objectName, mimeType);
boolean flag = checkFileIntegrity(minioClient, filePath, bucketName, objectName);
System.out.println(flag ? "上传文件成功了!!!" : "上传文件失败了!!!");
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
System.out.println("HTTP trace: " + e.httpTrace());
}
}
@Test
public void testDelete() throws Exception {
RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket("testbucket").object("1.MP4").build();
minioClient.removeObject(removeObjectArgs);
}
@Test
public void testGetObject() throws Exception {
GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket("testbucket").object("1.MP4").build();
FilterInputStream inputStream = minioClient.getObject(getObjectArgs);
String localFileName = "/Users/lcm/Downloads/1.mp4";
FileOutputStream outputStream = new FileOutputStream(new File(localFileName));
IOUtils.copy(inputStream, outputStream);
boolean flag = checkFileIntegrity(minioClient, localFileName, bucketName, objectName);
if (flag) {
System.out.println("下载成功了!!!");
} else {
System.out.println("下载失败了!!!");
}
}
public void uploadFile(MinioClient minioClient, String filePath, String bucketName, String objectName, String contentType) throws Exception {
String uploadLocalFileMD5 = calculateMD5(filePath);
System.out.println("上传前本地文件MD5: uploadLocalFileMD5=" + uploadLocalFileMD5);
Map<String, String> md5Map = new HashMap<>();
md5Map.put("md5", uploadLocalFileMD5);
File file = new File(filePath);
minioClient.putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.stream(new FileInputStream(file), file.length(), -1)
.userMetadata(md5Map)
.contentType(contentType)
.build()
);
System.out.println("File uploaded successfully: " + objectName);
}
public String calculateMD5(String filePath) throws Exception {
FileInputStream fileInputStream = new FileInputStream(filePath);
return DigestUtils.md5Hex(fileInputStream);
}
public boolean checkFileIntegrity(MinioClient minioClient, String filePath, String bucketName, String objectName) throws Exception {
String localFileMD5 = calculateMD5(filePath);
System.out.println("Local file MD5: " + localFileMD5);
StatObjectResponse stat = minioClient.statObject(
StatObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
String minioFileMD5 = stat.userMetadata().get("md5");
System.out.println("MinIO file MD5: " + minioFileMD5);
return localFileMD5.equals(minioFileMD5);
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· 【全网最全教程】使用最强DeepSeekR1+联网的火山引擎,没有生成长度限制,DeepSeek本体