MINIO 对象存储服务

  • MINIO 官网下载地址

注:需要下载 MINIO SERVER(服务端) 和 MINIO CLIENT(客户端)两个文件

方式一:管理员权限打开cmd窗口,进入minio.exe所在目录,依次执行以下命令:

#设置用户名
setx MINIO_ROOT_USER blublu

#设置密码
setx MINIO_ROOT_PASSWORD 123456789

#启动
E:\minio\bin\minio.exe server E:\minio\data --console-address ":9001" --address ":9000" > E:\minio\logs\minio.log

方式二:bin目录下新建一个minio.bat文件,执行该文件启动,文件内容示例:

@echo off
REM 声明采用UTF-8编码
chcp 65001
echo.
echo [信息] 运行MinIO文服务器。
echo.
# 设置窗口标题
title Minio文件服务

# 设置用户名
setx MINIO_ROOT_USER blublu
# 设置密码为mypassword
setx MINIO_ROOT_PASSWORD 123456789
 
cd %~dp0
# 切换到minio.exe文件所在目录
cd E:\minio\bin
# 启动minio服务
minio.exe server E:\minio\data --console-address ":9001" --address ":9000" > E:\minio\logs\minio.log
pause
  • 查看MINIO版本
minio --version

在这里插入图片描述

<dependency>
	<groupId>io.minio</groupId>
	<artifactId>minio</artifactId>
	<version>3.0.10</version>
</dependency>
# MINIO配置
minio:
  endpoint: http://127.0.0.1:9000
  accesskey: blublu
  secretKey: 123456789
package com.blu.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProp {
    /**
     * 连接url
     */
    private String endpoint;
    /**
     * 用户名
     */
    private String accesskey;
    /**
     * 密码
     */
    private String secretKey;
}
package com.blu.config;

import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(MinioProp.class)
public class MinioConfig {
 
    @Autowired
    private MinioProp minioProp;
 
    /**
     * 获取 MinioClient
     *
     * @return
     * @throws InvalidPortException
     * @throws InvalidEndpointException
     */
    @Bean
    public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
        return new MinioClient(minioProp.getEndpoint(), minioProp.getAccesskey(), minioProp.getSecretKey());
    }
}
package com.blu.utils.minio;

import com.alibaba.fastjson.JSONObject;
import com.blu.config.MinioProp;
import io.minio.MinioClient;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
@Slf4j
@Component
public class MinioUtils {
 
    @Autowired
    private MinioClient client;
    
    @Autowired
    private MinioProp minioProp;
 
    /**
     * 创建bucket
     *
     * @param bucketName bucket名称
     */
    @SneakyThrows
    public void createBucket(String bucketName) {
        if (!client.bucketExists(bucketName)) {
            client.makeBucket(bucketName);
        }
    }
 
    /**
     * 上传文件
     *
     * @param file       文件
     * @param bucketName 存储桶
     * @return
     */
    public JSONObject uploadFile(MultipartFile file, String bucketName) throws Exception {
        JSONObject res = new JSONObject();
        res.put("code", 0);
        // 判断上传文件是否为空
        if (null == file || 0 == file.getSize()) {
            res.put("msg", "上传文件不能为空");
            return res;
        }
        try {
            createBucket(bucketName);
            // 文件名
            String originalFilename = file.getOriginalFilename();
            // 新的文件名 = 存储桶名称_时间戳.后缀名
            String fileName = bucketName + "_" + System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));
            // 开始上传
            client.putObject(bucketName, fileName, file.getInputStream(), file.getContentType());
            res.put("code", 1);
            res.put("msg", minioProp.getEndpoint() + "/" + bucketName + "/" + fileName);
            return res;
        }  catch (Exception e) {
            log.error("上传文件失败:{}", e.getMessage());
        }
        res.put("msg", "上传失败");
        return res;
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值