Minio安装与简单使用

最近工作中接手一个老项目,其中有使用Minio作为文件上传下载的工具,现简单记录安装与项目中的使用,方便日后学习。

Minio中文官方文档地址: https://docs.min.io/cn/
Minio的源码地址:https://github.com/minio/minio

下载

# 下载  过程较慢,需耐心等一会
wget https://dl.min.io/server/minio/release/linux-amd64/minio
# 添加可执行权限
chmod +x minio
# 设置登录的access key
export MINIO_ACCESS_KEY=minio
# 设置登录的secret key
export MINIO_SECRET_KEY=minio
# 启动 minio
./minio server /data

启动后报错


Minio启动报错

提示secret key至少8个字符,于是修改一下

# 设置登录的secret key
export MINIO_SECRET_KEY=miniotest

再次启动


Minio启动

有一行WARNING,没管,浏览器中输入:http://192.168.126.51:9000/
输入前面安装时设置的access key和secret key


Minio访问

项目中使用

application.properties中配置Minio

minio.url=http://192.168.126.51:9000
minio.accessKey=minio
minio.secretKey=miniotest
minio.bucketName=image

pom.xml文件引入jar包

<dependency>
        <!-- 版本较老,目前最新版本是8.2.2 -->
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>6.0.13</version>
</dependency>

配置类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.minio.MinioClient;

@Configuration
public class MinioConfig {
    
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Value("${minio.url}")
    private String url;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;
    @Value("${minio.bucketName:default}")
    private String bucketName;

    @Bean
    public MinioClient minioClient() {
        try {
            MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
            if(!minioClient.bucketExists(bucketName)) {
                minioClient.makeBucket(bucketName);
                minioClient.setBucketPolicy(bucketName, "download");
            }
            return minioClient;
        } catch (Exception e) {
            logger.error("", e);
        }
        return null;
    }
}

service

import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.mfhcd.core.tools.StringUtil;

import io.minio.MinioClient;

@Component
public class MinioService {
    
    @Value("${minio.bucketName:default}")
    private String bucketName;
    
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private MinioClient minioClient;
    
    public void createBucket(String bucketName) {
        try {
            if(!minioClient.bucketExists(bucketName)) {
                minioClient.makeBucket(bucketName);
            }
        } catch (Exception e) {
            logger.error("", e);
        }
    }
    
    public String getObjectUrl(String objectName) throws Exception {
        return getObjectUrl(bucketName, objectName);
    }
    
    public String getObjectUrl(String bucketName, String objectName) throws Exception {
        try {
            if(StringUtil.isEmpty(objectName)) {
                return null;
            }
            return minioClient.getObjectUrl(bucketName, objectName);
        } catch (Exception e) {
            logger.error("", e);
        }
        return null;
    }
    
    public String upload(String objectName, InputStream stream) {
        return upload(bucketName, objectName, stream);
    }
    
    public String upload(String bucketName, String objectName, InputStream stream) {
        try {
            minioClient.putObject(bucketName, objectName, stream, null, null, null, null);
            return minioClient.getObjectUrl(bucketName, objectName);
        } catch (Exception e) {
            logger.error("", e);
        }
        return null;
    }
    
    public InputStream download(String objectName) {
        return download(bucketName, objectName);
    }
    
    public InputStream download(String bucketName, String objectName) {
        try {
            return minioClient.getObject(bucketName, objectName);
        } catch (Exception e) {
            logger.error("", e);
        }
        return null;
    }

    public void remove(String objectName) {
        remove(bucketName, objectName);
    }

    public void remove(String bucketName, String objectName) {
        try {
            if(StringUtil.isEmpty(objectName)) {
                return;
            }
            minioClient.removeObject(bucketName, objectName);
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容