Mac安装MINIO服务器实现本地上传和下载服务

0.MINIO学习文档

Minio客户端mc使用 | Elibaron学习笔记

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)启动成功。

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会按照年月日分好文件夹

相关推荐
神奇的小猴程序员4 小时前
苹果Mac电脑玩我的世界没有版本?用PCL-R即可!
macos
灵韵aura5 小时前
macOS 的指令缩写
macos
yqj2341 天前
ESP32-S3中小智项目优化内容
ide·macos·xcode
Mininglamp_27182 天前
Cider量化配置与性能调优:在M系列Mac上压榨Mano-P的每一点推理性能
macos
xuefuhe2 天前
MacOS EAGAIN无法打开terminal
macos
传奇开心果编程3 天前
【SwiftUI入门练中学】第7课 动画与手势:让界面更灵动
学习·macos·ui·ios·swiftui·swift
the3clipse3 天前
MacOS、Linux 与 Windows 视频编解码对比:AV1、HEVC、ProRes 与硬件加速优劣势
linux·windows·macos·hevc·av1·视频解码·硬件解码
传奇开心果编程4 天前
【SwiftUI娓娓道来】第3课:让界面活起来——交互与动画指南
学习·macos·ui·ios·swiftui·swift
朋友圈自动点赞工具4 天前
Gmail 委托邮箱在手机上关不掉
macos·智能手机·cocoa·硬件评测
小黄人软件4 天前
AndroidStudio老项目 macOS运行BlueTooth蓝牙串口助手(Android+Studio源码).rar
android·macos