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

相关推荐
laocooon5238578863 小时前
一台入网的电脑有6要素, 机器名,mac,ip,俺码,网关,dns,分别有什么作用
网络协议·tcp/ip·macos
德亦周13 小时前
如何在Mac电脑上的VScode去配置C/C++环境
c++·vscode·macos
tonngw19 小时前
【Mac 从 0 到 1 保姆级配置教程 12】- 安装配置万能的编辑器 VSCode 以及常用插件
git·vscode·后端·macos·开源·编辑器·github
亚林瓜子21 小时前
虚拟Python 环境构建器virtualenv安装(macOS版)
python·macos·virtualenv·pipx
瓜子三百克21 小时前
采用sherpa-onnx 实现 ios语音唤起的调研
macos·ios·cocoa
北漂强1 天前
Mac---安装Navicat 16
macos
ElenaYu1 天前
mac安装cast
python·macos·cast
爱笑的林羽2 天前
Mac M系列 安装 jadx-gui
前端·macos
筱宇***2 天前
Mac的web服务器
mysql·nginx·macos·php
Lucky me.2 天前
关于mac配置hdc(鸿蒙)
macos·华为·harmonyos