0.MINIO学习文档
1.Mac安装MINIO
中文官方网址:MinIO下载和安装 | 用于创建高性能对象存储的代码和下载内容
(1) brew 安装
brew install minio/stable/minio
(2)安装完成,执行brew info minio
(3)启动minio服务
/opt/homebrew/opt/minio/bin/minio server --certs-dir\=/opt/homebrew/etc/minio/certs --address\=:9000 /opt/homebrew/var/minio
参数解释:
- --certs-dir=/opt/homebrew/etc/minio/certs 配置文件目录
- --address=:9000 使用的端口
- /opt/homebrew/var/minio 存储数据目录
Ctrl+c关闭服务器。
(4)启动成功。
- 访问 http://127.0.0.1:9000 或者 http://127.0.0.1:52301
- RootUser: minioadmin
- RootPass: minioadmin
2. 利用本地MINIO实现文件上传功能
(1)首先先建一个Buckets
(2) 然后在nacos中配置你的oss-minio(这种方法比较通用)
oss:
provider:
MINIO:
ossType: minio
accessKey: minioadmin
secretKey: minioadmin
endpoint: 127.0.0.1
port: 9000
isHttps: false
bucket-name: self-safe
accessKey和secretKey可以在你登录minio的时候就会看到:
你也可以在你代码里写死,但是这样不通用:
try (InputStream inputStream = new FileInputStream(tempFile)) {
// 上传文件到 MinIO
log.info("Uploading file to MinIO, fileName: {}", fileName);
MinioClient minioClient = MinioClient.builder()
// MinIO 服务地址
.endpoint("http://localhost:9000")
// MinIO 访问密钥
.credentials("minioadmin", "minioadmin")
.build();
minioClient.putObject(
PutObjectArgs.builder()
// 替换为你的 MinIO Bucket 名称
.bucket("self-safe")
// 上传的文件名
.object(fileName)
// 文件流和文件大小
.stream(inputStream, tempFile.length(), -1)
// 文件类型
.contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
.build()
);
(3)调用上传文件工具类ossUtils.uploadFile,文件就会被自动上传到你自己本地的minio了
UploadResult uploadResult = ossUtils.uploadFile(inputStream, exportExcelName + ".xlsx");
(4) 把数据写入excel并上传到minio总体代码
private <T extends DamExcelWrite> void exportCustomerRiskMonitoring(
String fileSid,
Function<Integer, List<T>> exportFunction,
Class<T> clazz,
List<String> sheetNames,
String exportExcelName
) {
// 获取下载状态信息
FileDownloadStatus fileStatus = fileDownloadStatusService.getDownloadStatusById(fileSid);
DateTime processTime = DateUtil.date();
DateTime expireTime = DateUtil.offsetDay(processTime, fileExpireTime);
fileStatus.setExpireTime(expireTime);
String fileName = exportExcelName + "-" + fileSid;
File tempFile;
try {
tempFile = File.createTempFile(fileName, ExcelTypeEnum.XLSX.getValue());
} catch (Exception e) {
log.error("create file error, stop export, fileName: {}", fileName);
return;
}
try (OutputStream outputStream = new FileOutputStream(tempFile)) {
log.info("Starting to write Excel file");
// 创建模板文件
ExcelWriter excelWriter = EasyExcel.write(outputStream, clazz)
.inMemory(true)
.excelType(ExcelTypeEnum.XLSX)
.build();
// 创建并配置Sheet
WriteSheet writeSheet = EasyExcel.writerSheet(sheetNames.get(0)).head(clazz).build();
// 分页写入数据
int pageIndex = 1;
while (true) {
log.info("Fetching data for pageIndex: {}", pageIndex);
List<T> pageData = exportFunction.apply(pageIndex);
if (pageData == null || pageData.isEmpty()) {
log.warn("No data found for pageIndex: {}, ending write loop.", pageIndex);
break; // 数据为空,退出循环
}
log.info("Writing data for pageIndex: {}, data size: {}", pageIndex, pageData.size());
excelWriter.write(pageData, writeSheet);
pageIndex++;
}
log.info("Finishing ExcelWriter to finalize write operation.");
excelWriter.finish();
} catch (Exception e) {
log.error("Error during Excel export, fileSid: {}", fileSid, e);
fileStatus.setGenerateStatus("2");
fileStatus.setMsg("Error during export: " + e.getMessage());
}
try (InputStream inputStream = new FileInputStream(tempFile)) {
// 上传文件
log.info("Starting upload for file: {}", fileName);
long start = System.currentTimeMillis();
// 将文件流上传
UploadResult uploadResult = ossUtils.uploadFile(inputStream, sheetNames + ".xlsx");
log.info("Upload finished, cost: {}, file: {}", System.currentTimeMillis() - start, fileName);
// 更新文件状态为成功
fileStatus.setGenerateStatus("1");
fileStatus.setMsg("success");
saveExportRecord("CustomerRiskMonitoringExport", fileSid, processTime, expireTime, uploadResult);
} catch (Exception e) {
log.error("Error during file upload, fileSid: {}", fileSid, e);
fileStatus.setGenerateStatus("2");
fileStatus.setMsg("Error during upload: " + e.getMessage());
} finally {
if (tempFile.exists()) {
tempFile.delete();
}
}
// 更新下载状态
log.info("Updating fileDownloadStatus: {}", fileStatus);
fileDownloadStatusService.updateDownloadStatus(fileStatus);
}
(5)检查文件是否被上传成功,miniou会按照年月日分好文件夹