package com.star.minio.minio;
/**
* @Author StarC
* @Date 2022/2/8 11:32
* @Description:
*/
import io.minio.*;
import io.minio.errors.ErrorResponseException;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
@Slf4j
public class MinIOUtils {
private static MinioClient minioClient;
private static String endpoint;
private static String accessKey;
private static String secretKey;
private static Integer port;
private static Integer imgSize;
private static Integer fileSize;
private static final String SEPARATOR = "/";
static {
MinioProperties minioProperties= BaseHolder.getBean("minioProperties");
MinIOUtils.endpoint = minioProperties.getEndpoint();
MinIOUtils.accessKey = minioProperties.getAccessKey();
MinIOUtils.secretKey = minioProperties.getSecretKey();
MinIOUtils.port=minioProperties.getPort();
createMinioClient();
}
/**
* 创建基于Java端的MinioClient
*/
public static void createMinioClient() {
try {
if (null == minioClient) {
log.info("开始创建 MinioClient...");
minioClient = MinioClient.builder().endpoint(endpoint, port, false)
.credentials(accessKey, secretKey)
.build();
log.info("创建完毕 MinioClient...");
}
} catch (Exception e) {
log.error("[Minio工具类]>>>> MinIO服务器异常:", e);
}
}
/**
* 获取上传文件前缀路径
* @return
*/
public static String getBasisUrl(String bucketName) {
return endpoint + SEPARATOR + bucketName + SEPARATOR;
}
/****************************** Operate Bucket Start ******************************/
/**
* 启动SpringBoot容器的时候初始化Bucket
* 如果没有Bucket则创建
* @throws Exception
*/
private static void createBucket(String bucketName) throws Exception {
if (!bucketExists(bucketName)) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
}
/**
* 判断Bucket是否存在,true:存在,false:不存在
* @return
* @throws Exception
*/
public static boolean bucketExists(String bucketName) throws Exception {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
}
/**
* 获得Bucket的策略
* @param bucketName
* @return
* @throws Exception
*/
public static String getBucketPolicy(String bucketName) throws Exception {
return minioClient
.getBucketPolicy(
GetBucketPolicyArgs
.builder()
.bucket(bucketName)
.build()
);
}
/**
* 获得所有Bucket列表
* @return
* @throws Exception
*/
public static List<Bucket> getAllBuckets() throws Exception {
return minioClient.listBuckets();
}
/**
* 根据bucketName获取其相关信息
* @param bucketName
* @return
* @throws Exception
*/
public static Optional<Bucket> getBucket(String bucketName) throws Exception {
return getAllBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
}
/**
* 根据bucketName删除Bucket,true:删除成功; false:删除失败,文件或已不存在
* @param bucketName
* @throws Exception
*/
public static void removeBucket(String bucketName) throws Exception {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
}
/****************************** Operate Bucket End ******************************/
/****************************** Operate Files Start ******************************/
/**
* 判断文件是否存在
* @param bucketName 存储桶
* @param objectName 文件名
* @return
*/
public static boolean isObjectExist(String bucketName, String objectName) {
boolean exist = true;
try {
minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (Exception e) {
log.error("[Minio工具类]>>>> 判断文件是否存在, 异常:", e);
exist = false;
}
return exist;
}
/**
* 判断文件夹是否存在
* @param bucketName 存储桶
* @param objectName 文件夹名称
* @return
*/
public static boolean isFolderExist(String bucketName, String objectName) {
boolean exist = false;
try {
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).prefix(objectName).recursive(false).build());
for (Result<Item> result : results) {
Item item = result.get();
if (item.isDir() && objectName.equals(item.objectName())) {
exist = true;
}
}
} catch (Exception e) {
log.error("[Minio工具类]>>>> 判断文件夹是否存在,异常:", e);
exist = false;
}
return exist;
}
/**
* 根据文件前置查询文件
* @param bucketName 存储桶
* @param prefix 前缀
* @param recursive 是否使用递归查询
* @return MinioItem 列表
* @throws Exception
*/
public static List<Item> getAllObjectsByPrefix(String bucketName,
String prefix,
boolean recursive) throws Exception {
List<Item> list = new ArrayList<>();
Iterable<Result<Item>> objectsIterator = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build());
if (objectsIterator != null) {
for (Result<Item> o : objectsIterator) {
Item item = o.get();
list.add(item);
}
}
return list;
}
/**
* 获取文件流
* @param bucketName 存储桶
* @param objectName 文件名
* @return 二进制流
*/
public static InputStream getObject(String bucketName, String objectName) throws Exception {
return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
}
/**
* 断点下载
* @param bucketName 存储桶
* @param objectName 文件名称
* @param offset 起始字节的位置
* @param length 要读取的长度
* @return 二进制流
*/
public InputStream getObject(String bucketName, String objectName, long offset, long length)throws Exception {
return minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.offset
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践minio+springboot代码实践
资源推荐
资源详情
资源评论
收起资源包目录
minio+springboot代码实践 (174个子文件)
0193e5423488528144e323ece08f765f91a73b 271B
1958f0b02e20f32cdd8cd717d5a6abeea73587 74B
1baa1148866c27b669da668ebe6b8f53a4ff6d 639B
1d8e79a284a8c8c9c0e36dad3eb3bee3408724 49B
2c4588e9fdde23c7ffc6393d5f5f2dc2f1a328 131B
3c522527f5ef451f7c4c75c36825577f7d43f4 4KB
3f53f723516e4c0c9f8fb6b9d3d0882f1b6428 46B
44d9cb967bff99ba5d6a97398d2c2cf0ca14bc 64B
4906a9b77afb3b0b40f52c9002f0cb04bae2d8 117B
5a3fcc6130213d0160dcc5d189b0d508562716 611B
6149fe2923bb79f222dfb66bf779fc5c87ea07 47B
6320a496f3201c67f85a866af38fb0f8911168 46B
6d0d3012a47f022bd6ffce6a3e6a5b7ecaedb9 60B
746ac60fb9624d09d72d2235f9b6bf6ef25b08 163B
8ab018eaf11d9b3a4a90e7818ace373dfbb380 3KB
8fb2282df5b8f7263470a5a2dc0e196f35f35f 4KB
904df64999d4835eb7461a6b08bec53b7549b5 45B
945344cf010d3ced2c859c1487f7360fb27ba5 95B
996331a4842f93bcefcdc8a35ca5c835ed94de 733B
9d63196841f9bb9fbb26e00fb5b44ff82ded06 131B
9e00a2a96fa9d7c5dbc9859664a78d980158c2 249B
b741de18d348398b7f908204d7213bdaf82f4b 71B
b96590545cab1386ddc8de963bb9989851fc51 79B
ca4e4744f71dde9e2b3ad33b36c9e28f1fb105 47B
cb93e7050f42ff587aefbc40cc038ad4fa3271 128B
cfe2ff51b4414e39a59e9975d30bc45753fe11 95B
MinIOUtils.class 16KB
MinioProperties.class 3KB
MinioController.class 2KB
BaseHolder.class 1KB
MinioApplication.class 732B
MinioApplicationTests.class 530B
mvnw.cmd 7KB
mvnw.cmd 7KB
COMMIT_EDITMSG 6B
config 300B
d0291937a98aa77f58ab7c7f1b61e7044d56aa 45B
dd12f17644411d6e840bd5a10c6ecda0175f18 51KB
description 73B
e7c8dfa2958acec799a366fe48993fa1aacf1c 47B
exclude 240B
f65c97dac822871f35381c5bcd6ee608ce09f5 201B
fdff20b291c46ec76f07603367fec4d9fcb167 184B
.gitignore 395B
.gitignore 395B
HEAD 148B
HEAD 23B
minio.iml 9KB
minio.iml 9KB
index 2KB
maven-wrapper.jar 57KB
MinIOUtils.java 16KB
MinIOUtils.java 16KB
MinioController.java 1KB
MinioController.java 1KB
BaseHolder.java 1KB
BaseHolder.java 1KB
MinioProperties.java 473B
MinioProperties.java 473B
MinioApplication.java 317B
MinioApplication.java 317B
MinioApplicationTests.java 214B
MinioApplicationTests.java 214B
master 148B
master 139B
master 41B
master 41B
HELP.md 891B
HELP.md 891B
mvnw 10KB
mvnw 10KB
index.pb 0B
maven-wrapper.properties 233B
minio.rar 16KB
pre-rebase.sample 5KB
fsmonitor-watchman.sample 5KB
update.sample 4KB
push-to-checkout.sample 3KB
pre-commit.sample 2KB
prepare-commit-msg.sample 1KB
pre-push.sample 1KB
commit-msg.sample 896B
pre-receive.sample 544B
applypatch-msg.sample 478B
pre-applypatch.sample 424B
pre-merge-commit.sample 416B
post-update.sample 189B
workspace.xml 10KB
pom.xml 2KB
pom.xml 2KB
Project_Default.xml 1KB
Project.xml 985B
Maven__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml 940B
compiler.xml 873B
Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_13_1.xml 841B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_6_3.xml 818B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 805B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_13_1.xml 800B
Maven__org_springframework_boot_spring_boot_starter_logging_2_6_3.xml 797B
Maven__org_springframework_boot_spring_boot_starter_tomcat_2_6_3.xml 790B
共 174 条
- 1
- 2
资源评论
yl66668888
- 粉丝: 193
- 资源: 63
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功