Minio 是一个基于Go语言的对象存储服务。它实现了大部分亚马逊S3云存储服务接口,可以看做是是S3的开源版本,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。区别于分布式存储系统,minio的特色在于简单、轻量级,对开发者友好,认为存储应该是一个开发问题而不是一个运维问题。
下载
chomd +x minio
vi run.sh
#!/bin/bash
#web管理界面登录用户
export MINIO_ROOT_USER=minio
#web管理界面登录密码
export MINIO_ROOT_PASSWORD=minio
#生成共享链接时,需要配置,否则是本地地址127.0.0.1,有地址的可以修改为自己地址
export MINIO_SERVER_URL=http://IP:9002
# nohup启动服务 指定文件存放路径 /root/data 还有设置日志文件路径 /root/minio/log
nohup ./minio server --address :9002 --console-address :9001 /root/data/minio > /root/logs/minio.log 2>&1 &
chmod u+x run.sh
.
bash run.sh
tail -f /root/logs/minio.log
io.minio
minio
7.1.0
minio:
endpoint: http://IP:9002
accessKey: bjdZxvMDxAzYETgYn0aY 配置账号
secretKey: uk7srkLHsYkwzvTYVzDBtwzlXz5fxsoMmNpbb3SN 配置密码
bucketName: test 桶名称-默认
package com.project.google.util;
/**
* @Description: TODO
* @Author xgp
* @Date 2023/8/7 8:05
* @PackageName:com.project.google.util
* @ClassName: MinioTemplate
* @Version 1.0
*/
import io.minio.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Minio 基础操作类
*
* @author: zhanghuaiyu
* @since 2021-01-22 16:27
*/
@Configuration
public class MinioTemplate implements InitializingBean {
private MinioClient minioClient;
@Value("${minio.endpoint}")
private String url;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Override
public void afterPropertiesSet() {
Assert.hasText(url, "Minio url 为空");
Assert.hasText(accessKey, "Minio accessKey为空");
Assert.hasText(secretKey, "Minio secretKey为空");
this.minioClient = new MinioClient(url, accessKey, secretKey);
}
/**
* 创建bucket
* setBucketPolicy 设置权限才可以预览
*
* @param bucketName bucket名称
*/
@SneakyThrows
public Boolean createBucket(String bucketName) {
if (!bucketExists(bucketName)) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
StringBuilder builder = new StringBuilder();
builder.append("{\n");
builder.append(" \"Statement\": [\n");
builder.append(" {\n");
builder.append(" \"Action\": [\n");
builder.append(" \"s3:GetBucketLocation\",\n");
builder.append(" \"s3:ListBucket\"\n");
builder.append(" ],\n");
builder.append(" \"Effect\": \"Allow\",\n");
builder.append(" \"Principal\": \"*\",\n");
builder.append(" \"Resource\": \"arn:aws:s3:::bucketname\"\n");
builder.append(" },\n");
builder.append(" {\n");
builder.append(" \"Action\": \"s3:GetObject\",\n");
builder.append(" \"Effect\": \"Allow\",\n");
builder.append(" \"Principal\": \"*\",\n");
builder.append(" \"Resource\": \"arn:aws:s3:::my-bucketname/*.*\"\n");
builder.append(" }\n");
builder.append(" ],\n");
builder.append(" \"Version\": \"2012-10-17\"\n");
builder.append("}\n");
minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(builder.toString().replace("bucketname", bucketName)).build());
return true;
} else {
return false;
}
}
/**
* 获取全部bucket
*
* https://docs.minio.io/cn/java-client-api-reference.html#listBuckets
*/
@SneakyThrows
public List getAllBuckets() {
return minioClient.listBuckets();
}
/**
* 根据bucketName获取信息
*
* @param bucketName bucket名称
*/
@SneakyThrows
public Optional getBucket(String bucketName) {
return minioClient.listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
}
/**
* 根据bucketName删除信息
*
* @param bucketName bucket名称
*/
@SneakyThrows
public void removeBucket(String bucketName) {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
}
/**
* 根据文件前置查询文件
*
* @param bucketName bucket名称
* @param prefix 前缀
* @param recursive 是否递归查询
* @return MinioItem 列表
*/
@SneakyThrows
public List getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
List- list = new ArrayList<>();
Iterable
> objectsIterator = minioClient.listObjects(bucketName, prefix, recursive);
if (objectsIterator != null) {
Iterator> iterator = objectsIterator.iterator();
if (iterator != null) {
while (iterator.hasNext()) {
Result- result = iterator.next();
Item item = result.get();
list.add(item);
}
}
}
return list;
}
/**
* 获取文件外链
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @param expires 过期时间 <=7
* @return url
*/
@SneakyThrows
public String getObjectUrl(String bucketName, String objectName, Integer expires) {
return minioClient.presignedGetObject(bucketName, objectName, expires);
}
/**
* 获取文件路径
*
* @param bucketName
* @param fileName
* @return
*/
@SneakyThrows
public String getObjectUrl(String bucketName, String fileName) {
return minioClient.getObjectUrl(bucketName, fileName);
}
/**
* 获取文件
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @return 二进制流
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName) {
return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
}
/**
* 获取文件
*
* @param bucketName
* @param objectName
* @return
*/
@SneakyThrows
public ObjectStat statObject(String bucketName, String objectName) {
return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
}
/**
* 上传文件
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public String putObject(String bucketName, String objectName, MultipartFile file) throws Exception {
if (!this.bucketExists(bucketName)) {
this.createBucket(bucketName);
}
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(file.getInputStream(), file.getSize(), PutObjectArgs.MIN_MULTIPART_SIZE).contentType(file.getContentType()).build());
return bucketName;
}
/**
* 上传文件
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @param stream 文件流
* @param size 大小
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream, long size) throws Exception {
minioClient.putObject(bucketName, objectName, stream, new PutObjectOptions(stream.available(), -1));
}
/**
* 获取文件信息, 如果抛出异常则说明文件不存在
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
*/
public ObjectStat getObjectInfo(String bucketName, String objectName) throws Exception {
return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
}
/**
* 删除文件
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeObject
*/
public void removeObject(String bucketName, String objectName) throws Exception {
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
}
/**
* 批量删除文件夹内所有文件
*
* @param bucketName bucket名称
* @param objectName 文件名称
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeObject
*/
public void removeObjects(String bucketName, String objectName) throws Exception {
List
- list = getAllObjectsByPrefix(bucketName, objectName, false);
for (Item item : list) {
removeObject(bucketName, item.objectName());
}
}
@SneakyThrows
public boolean bucketExists(String bucketName) {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
}
/**
* 文件下载
*
* @param response
* @param bucket
* @param objectName
* @param outName
* @throws Exception
*/
public void download(HttpServletResponse response, String bucket, String objectName, String outName) throws Exception {
ObjectStat stat = this.statObject(bucket, objectName);
response.setContentType(stat.contentType());
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(outName, "UTF-8"));
response.setHeader("FileName", URLEncoder.encode(outName, "UTF-8"));
InputStream in = this.getObject(bucket, objectName);
IOUtils.copy(in, response.getOutputStream());
in.close();
}
/**
* 合并分片文件到指定目录
*
* @param bucket
* @param fileName
* @param sources
* @return
* @throws Exception
*/
public ObjectWriteResponse composeObject(String bucket, String fileName, List
sources) throws Exception {
ObjectWriteResponse response = minioClient.composeObject(ComposeObjectArgs.builder()
.bucket(bucket)
.object(fileName)
.sources(sources)
.build());
return response;
}
}
package com.project.google.controller;
import afu.org.checkerframework.checker.oigj.qual.O;
import com.project.google.util.MinioTemplate;
import io.minio.messages.Bucket;
import org.apache.commons.io.IOUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* @Description: TODO
* @Author xgp
* @Date 2023/8/7 8:35
* @PackageName:com.project.google.controller
* @ClassName: TbMinioController
* @Version 1.0
*/
@RestController
public class TbMinioController {
@Autowired
private MinioTemplate minioTemplate;
//创建新的桶
@GetMapping("createBucket")
public Object createBucket(String bucketName){
return minioTemplate.createBucket(bucketName);
}
//获取对应桶信息
@GetMapping("getList")
public Object getList(String bucketName){
Bucket bucket = minioTemplate.getBucket(bucketName).get();
Map map = new HashMap<>();
map.put("name",bucket.name());
map.put("createDate",bucket.creationDate());
return map;
}
//获取所有桶信息
@GetMapping("getAll")
public Object getAll(){
List