1.(1)linux安装minio
wget https://dl.minio.io/server/minio/release/linux-amd64/minio
chmod +x minio
mv minio /usr/local/bin
mkdir -p /usr/local/minio/data
minio server /usr/local/minio/data //运行
前台运行
(2)docker安装minio (推荐)
docker pull minio/minio
docker run -it -p 9000:9000 -d minio/minio server /data
//设置 ACCESS_KEY 和 SECRET_KEY
echo "admin" | docker secret create access_key -
echo "admin123456" | docker secret create secret_key -
//自定义用户和秘钥安装
docker run -p 9000:9000 --name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=admin" \
-e "MINIO_SECRET_KEY=admin123456" \
-v /home/data:/data \
-v /home/config:/root/.minio \
minio/minio server /data
2.访问minio
http://218.244.138.54:9000/
minio
3.使用java上传图片
3.1 导入maven依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.1.0</version>
</dependency>
3.2 图片上传工具类
package org.jeecg.common.util;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/**
* @author: lzm
* @date: 2020/12/17 10:13 上午
*/
public class MinioUtils {
private static String url = "http://218.244.138.54:9000";
private static String access = "access";
private static String secret = "secret";
private static String bucket = "bucket";
/**
* 上传
* @return
*/
public static String uploadFileMinio(MultipartFile file,String receiptId){
try {
//创建MinioClient对象
MinioClient minioClient =
MinioClient.builder()
.endpoint(url)
.credentials(access, secret)
.build();
File toFile = MultipartFileToFile.multipartFileToFile(file);
InputStream in = new FileInputStream(toFile);
String fileName = file.getName();
//文件上传到minio上的Name把文件后缀带上,不然下载出现格式问题
fileName = receiptId+"."+fileName;
//创建头部信息
Map<String, String> headers = new HashMap<>(10);
//添加自定义内容类型
headers.put("Content-Type", "application/octet-stream");
//添加存储类
headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
//添加自定义/用户元数据
Map<String, String> userMetadata = new HashMap<>(10);
userMetadata.put("My-Project", "Project One");
//上传
minioClient.putObject(
PutObjectArgs.builder().bucket(bucket).object(fileName).stream(
in, in.available(), -1)
.headers(headers)
.userMetadata(userMetadata)
.build());
in.close();
return url+"/"+bucket+"/"+fileName;
}catch (Exception e){
e.printStackTrace();
return e.getMessage();
}
}
/**
* 删除
* @param fileName
* @return
* @throws Exception
*/
public static String removeMinio(String fileName){
try {
//创建MinioClient对象
MinioClient minioClient = MinioClient.builder()
.endpoint(url)
.credentials(access, secret)
.build();
minioClient.removeObject( RemoveObjectArgs.builder().bucket(bucket).object(fileName).build());
return "success";
}catch (Exception e){
e.printStackTrace();
return e.getMessage();
}
}
/**
* 下载
* @return
*/
public void downloadMinio(String fileName){
try {
//创建MinioClient对象
MinioClient minioClient =
MinioClient.builder()
.endpoint(url)
.credentials(access, secret)
.build();
InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(fileName).build());
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = stream.read(buf, 0, buf.length)) >= 0) {
System.out.println(new String(buf, 0, bytesRead, StandardCharsets.UTF_8));
}
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.3 效果图
上传成功4.获取图片地址(设置为短链接)
修改指定bucket的policy添加policy
网友评论