centos安装minio 记录之前的教程消失了 非docker
首先需要下载 minio
wget https://dl.minio.io/server/minio/release/linux-amd64/minio
windows可以直接访问链接下载
修改权限
chmod +x minio
编写启动脚本
#!/bin/bash
export MINIO_ACCESS_KEY=指定用户
export MINIO_SECRET_KEY=指定密码
#后台启动
nohup /路径/minio/minio server --address 0.0.0.0:指定端口 /路径/minio/data > /路径/minio/minio.log 2>&1 &
如果报错 请先创建 data、log文件夹
添加bucket
需要访问 服务器或者本机 127.0.0.1:配置的端口
输入 在sh中设置的账号和密码登录
登录进去以后 在右下角
1 为上传按钮 可以直接在web页面上传
2 为创建bucket按钮
下图为点击创建后的效果
输入名称后 回车即创建成功
创建成功后在左侧列表会显示
点击3个点
最后点弹窗右上角关闭 就可以使用了
java代码对接
首先是使用maven
需要依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>4.0.0</version>
</dependency>
这个依赖是工具类中有一个类需要这个包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
工具类
import io.minio.MinioClient;
import io.minio.errors.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.xmlpull.v1.XmlPullParserException;
import sun.misc.BASE64Decoder;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import cn.hutool.core.date.DateUtil;
/**
* @Author: duke_yuzhaolu
* @Date: 2021/06/08/15:15
* @Description: minio文件上传工具类
*/
@Slf4j
@Data
@Component
public class MinioUtil {
//可以写到配置文件获取 也可以直接赋值
@Value("${config.minioUrl}")
private String minioUrl;
@Value("${config.minioName}")
private String minioName;
@Value("${config.minioPass}")
private String minioPass;
@Value("${config.bucketName}")
private String bucketName;
private MinioClient minioClient = null;
/**
* 上传文件
* @param file
* @return
*/
public String upload(MultipartFile file, String bizPath, String customBucket) {
String file_url = "";
String newBucket = bucketName;
if(StringUtils.isNotEmpty(customBucket)){
newBucket = customBucket;
}
try {
initMinio(minioUrl, minioName,minioPass);
// 检查存储桶是否已经存在
if(minioClient.bucketExists(newBucket)) {
log.info("{} 已经存在可以上传!",bucketName);
} else {
// 创建一个名为ota的存储桶
minioClient.makeBucket(newBucket);
log.info("{} 已经不存在,正在创建!",bucketName);
}
InputStream stream = file.getInputStream();
// 获取文件名
String orgName = file.getOriginalFilename();
if("".equals(orgName)){
orgName=file.getName();
}
String objectName = bizPath + "_" + DateUtil.format(new Date(Long.parseLong(String.valueOf(System.currentTimeMillis())) ), "yyyyMMddHHmmssSSS") + ".jpg";
// 使用putObject上传一个本地文件到存储桶中。
minioClient.putObject(newBucket,objectName, stream,stream.available(),"application/octet-stream");
stream.close();
file_url = minioUrl+"/"+newBucket+"/"+objectName;
log.info("车牌号:{},图片链接:{}",bizPath,file_url);
}catch (Exception e){
log.error(e.getMessage(), e);
}
return file_url;
}
/**
* 文件上传
* @param file
* @param bizPath
* @return
*/
public String upload(MultipartFile file, String bizPath) {
return upload(file,bizPath,null);
}
/**
* 文件上传
* @param fileBase64
* @param bizPath
* @return
*/
public String upload(String fileBase64, String bizPath) {
return upload(base64ToMultipart(fileBase64),bizPath,null);
}
/**
* 获取文件流
* @param bucketName
* @param objectName
* @return
*/
public InputStream getMinioFile(String bucketName,String objectName){
InputStream inputStream = null;
try {
initMinio(minioUrl, minioName, minioPass);
inputStream = minioClient.getObject(bucketName, objectName);
} catch (Exception e) {
log.info("文件获取失败" + e.getMessage());
}
return inputStream;
}
/**
* 删除文件
* @param bucketName
* @param objectName
* @throws Exception
*/
public void removeObject(String bucketName, String objectName) {
try {
initMinio(minioUrl, minioName,minioPass);
minioClient.removeObject(bucketName, objectName);
}catch (Exception e){
log.info("文件删除失败" + e.getMessage());
}
}
/**
* 获取文件外链
* @param bucketName
* @param objectName
* @param expires
* @return
*/
public String getObjectURL(String bucketName, String objectName, Integer expires) {
initMinio(minioUrl, minioName,minioPass);
try{
String url = minioClient.presignedGetObject(bucketName, objectName, expires);
return URLDecoder.decode(url,"UTF-8");
}catch (Exception e){
log.info("文件路径获取失败" + e.getMessage());
}
return null;
}
/**
* 初始化客户端
* @param minioUrl
* @param minioName
* @param minioPass
* @return
*/
private MinioClient initMinio(String minioUrl, String minioName,String minioPass) {
if (minioClient == null) {
try {
minioClient = new MinioClient(minioUrl, minioName,minioPass);
} catch (Exception e) {
e.printStackTrace();
}
}
return minioClient;
}
/**
* 上传文件到minio
* @param stream
* @param relativePath
* @return
*/
public String upload(InputStream stream,String relativePath) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, RegionConflictException, InvalidArgumentException {
initMinio(minioUrl, minioName,minioPass);
if(minioClient.bucketExists(bucketName)) {
log.info("Bucket already exists.");
} else {
// 创建一个名为ota的存储桶
minioClient.makeBucket(bucketName);
log.info("create a new bucket.");
}
minioClient.putObject(bucketName,relativePath, stream, stream.available(),"application/octet-stream");
stream.close();
return minioUrl+bucketName+"/"+relativePath;
}
public MultipartFile base64ToMultipart(String imgStr) {
try {
BASE64Decoder decoder = new BASE64Decoder();
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
//调整异常数据
b[i] += 256;
}
}
InputStream inputStream = new ByteArrayInputStream(b);
return new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
正常调用就可以了 我的工具类是传的Base64文件 如果是传的file再根据需要修改就可以了
在此感谢之前发布教程的人 但是链接莫名失效 所以在此记录一下