package com.belle.yitiansystem.util;import cn.hutool.core.io.FileUtil;import com.belle.yitiansystem.systemmgmt.util.JsonUtil;import com.yougou.tools.common.utils.SpringContextHolder;import io.minio.*;import org.apache.log4j.Logger;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.util.Map;import java.util.Properties;/** * miniO存储工具类 * * @author 刘卫 * @site www.yougou.com * @company 优购科技 * @Date 创建时间:2021-10-25 17:04 */public class MiniOUtil { protected final static Logger logger = Logger.getLogger(MiniOUtil.class); /** * 桶名称 */ private final static String BUCKET_NAME = "ds-order"; /** * 初始化 MinioClient * * @return */ private static MinioClient getMinioClient() { try { Properties configProperties = SpringContextHolder.getBean("sysconfigproperties"); Map urlMap = JsonUtil.getMapFromJson(JsonUtil.getJSONString(configProperties)); String minioUrl = urlMap.get("minioUrl") + ""; String minioName = urlMap.get("minioName") + ""; String minioPass = urlMap.get("minioPass") + "";/* String minioUrl = "http://10.234.6.98:9000"; String minioName = "miniotest"; String minioPass = "miniotest";*/ MinioClient minioClient = MinioClient.builder().endpoint(minioUrl) .credentials(minioName, minioPass) .build(); return minioClient; } catch (Exception e) { logger.error("初始化【miniO】异常:" + e.getMessage(), e); } return null; } /** * 上传文件 * * @param fis * @param fileName */ public static void uploadInputStream(InputStream fis, String fileName) { try { MinioClient minioClient = getMinioClient(); //已知文件上传 PutObjectArgs objectArgs = PutObjectArgs.builder().object(fileName) .bucket(BUCKET_NAME) .stream(fis, fis.available(), -1).build(); minioClient.putObject(objectArgs); } catch (Exception e) { logger.error("上传到【miniO】异常:" + e.getMessage(), e); } finally { if (null != fis) { try { fis.close(); } catch (IOException e) { logger.error("上传到【miniO】异常->关闭流失败:" + e.getMessage(), e); } } } } /** * 获取对象 * * @param fileName * @return */ public static InputStream getObject(String fileName) { MinioClient minioClient = getMinioClient(); try { GetObjectArgs objectArgs = GetObjectArgs.builder() .bucket(BUCKET_NAME) .object(fileName) .build(); InputStream inputStream = minioClient.getObject(objectArgs); return inputStream; } catch (Exception e) { logger.error("【获取miniO对象】异常:" + e.getMessage(), e); } return null; } /** * 下载文件 到指定目录 * * @param sourceFileName * @param targetFileName */ public static void downloadFile(String sourceFileName, String targetFileName) { MinioClient minioClient = getMinioClient(); /*GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(minioBucketName) .object(uploadFileName).build();*/ DownloadObjectArgs objectArgs = DownloadObjectArgs.builder() .bucket(BUCKET_NAME) .object(sourceFileName) .filename(targetFileName) .build(); try { minioClient.downloadObject(objectArgs); } catch (Exception e) { logger.error("【下载miniO对象】异常:" + e.getMessage(), e); } } /** * 判断桶是否存在 * * @param bucketName */ public static void existsBucket(String bucketName) { MinioClient minioClient = getMinioClient(); try { // 判断桶是否存在 BucketExistsArgs argsExist = BucketExistsArgs.builder() .bucket(bucketName) .build(); boolean isExist = minioClient.bucketExists(argsExist); if (!isExist) { logger.error("桶不存在"); } else { logger.error("桶已存在"); } } catch (Exception e) { logger.error("【桶不存在,抛异常】异常:" + e.getMessage(), e); } } /** * 创建桶 * * @param bucketName */ public static void createBucket(String bucketName) { MinioClient minioClient = getMinioClient(); try { MakeBucketArgs args = MakeBucketArgs.builder() .bucket(BUCKET_NAME) .build(); minioClient.makeBucket(args); logger.info("创建桶成功"); } catch (Exception e) { logger.error("【创建桶,抛异常】异常:" + e.getMessage(), e); } } /** * 删除桶 * * @param bucketName */ public static void removeBucket(String bucketName) { MinioClient minioClient = getMinioClient(); try { RemoveBucketArgs args = RemoveBucketArgs.builder() .bucket(bucketName) .build(); minioClient.removeBucket(args); logger.info("删除桶成功"); } catch (Exception e) { logger.error("【删除桶,抛异常】异常:" + e.getMessage(), e); } } public static void main(String[] args) throws Exception { // 创建桶// createBucket(minioUrl, minioName, minioPass, "ds-order"); // 上传文件测试/* File file = new File("C:\\Users\\david\\Desktop\\test456.xlsx"); FileInputStream fis = new FileInputStream(file); String uploadFileName = "test456.xlsx"; uploadInputStream(fis, uploadFileName);*/ // 获取文件测试 InputStream inputStream = getObject("\\20211027\\活动名单_1635318202985.xlsx"); File file2 = new File("C:\\Users\\david\\Desktop\\新文件test456.xlsx"); FileUtil.writeFromStream(inputStream, file2); }}