package com.soyuan.mh.srs.util;import com.google.common.base.Stopwatch;import com.google.common.util.concurrent.ThreadFactoryBuilder;import com.soyuan.mh.srs.config.child.MinIoConfig;import io.minio.*;import io.minio.errors.*;import java.io.*;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.time.Duration;import java.util.ArrayList;import java.util.List;import java.util.concurrent.*;import java.util.function.Consumer;/** * @author tangwx@soyuan.com.cn * @date 2021/4/2 上午11:46 */public class MinIoUtil { /** * 用于上传视频 */ private static final ExecutorService es = Executors.newFixedThreadPool(5, new ThreadFactoryBuilder().setNameFormat("mh.srs.upload-minio-%d").build()); private MinioClient client; private String bucketName; public MinIoUtil(MinIoConfig config) { this(config.getEndPoint(), config.getAccessKey(), config.getSecretKey(),config.getBucketName()); } public MinIoUtil(String endPoint, String accessKey, String secretKey,String bucketName) { this.bucketName=bucketName; this.client = MinioClient.builder() .endpoint(endPoint) .credentials(accessKey, secretKey) .build(); } /** * 初始化创建桶和子文件夹 */ public void init() { createBucket(); //makeDir("image/"); //makeDir("video/"); } private boolean createBucket() { try { boolean isExist = client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); if (isExist) { return false; }else{ client.makeBucket(MakeBucketArgs.builder() .bucket(bucketName) .objectLock(false) .build()); return true; } } catch (ErrorResponseException e) { e.printStackTrace(); } catch (InsufficientDataException e) { e.printStackTrace(); } catch (InternalException e) { e.printStackTrace(); } catch (InvalidBucketNameException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } catch (XmlParserException e) { e.printStackTrace(); } catch (RegionConflictException e) { e.printStackTrace(); } return false; } public boolean deleteBucket(String bucketName){ try { client.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); return true; } catch (ErrorResponseException e) { e.printStackTrace(); } catch (InsufficientDataException e) { e.printStackTrace(); } catch (InternalException e) { e.printStackTrace(); } catch (InvalidBucketNameException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } catch (XmlParserException e) { e.printStackTrace(); } return false; } public boolean download(String bucketName, String objectName, String output) { final InputStream is = getObject(bucketName, objectName); if (is != null) { try { OutputStream os = new FileOutputStream(new File(output)); byte[] arr = new byte[1024]; int len; while ((len=is.read(arr))!=-1){ os.write(arr,0,len); } os.close(); is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } return false; } public InputStream getObject(String bucketName,String objectName) { try { return client.getObject(GetObjectArgs.builder() .bucket(bucketName) .object(objectName) .build()); } catch (ErrorResponseException e) { e.printStackTrace(); } catch (InsufficientDataException e) { e.printStackTrace(); } catch (InternalException e) { e.printStackTrace(); } catch (InvalidBucketNameException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } catch (XmlParserException e) { e.printStackTrace(); } return null; } /** * 上传jpg图片 * @param imageName 图片名称 * @param inputPath 图片路径 * @return */ public boolean putImage(String imageName, String inputPath) { try { InputStream ins = new FileInputStream(inputPath); return putObject(bucketName, imageName, ContentType.JPEG, ins); } catch (FileNotFoundException e) { e.printStackTrace(); } return false; } /** * 异步上传MP4 * @param videoName 视频名称 * @param inputPath 视频路径 * @param consumer 回调接口 * @return */ public void putVideoAsy(final String videoName, final String inputPath, Consumer<Boolean> consumer){ es.submit(() -> { try { InputStream ins = new FileInputStream(inputPath); boolean res = putObject(bucketName, videoName, ContentType.MP4, ins); consumer.accept(res); } catch (FileNotFoundException e) { e.printStackTrace(); } }); } /** * 创建目录 * @param dir */ public void makeDir(String dir){ try { client.putObject(PutObjectArgs.builder() .bucket(bucketName) .object(dir) .stream(new ByteArrayInputStream(new byte[] {}), 0, -1) .build()); } catch (ErrorResponseException e) { e.printStackTrace(); } catch (InsufficientDataException e) { e.printStackTrace(); } catch (InternalException e) { e.printStackTrace(); } catch (InvalidBucketNameException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } catch (XmlParserException e) { e.printStackTrace(); } } private boolean putObject(String bucket,String objName,ContentType contentType,InputStream ins) { try { client.putObject(PutObjectArgs.builder() .bucket(bucket) .object(objName) .stream(ins, ins.available(), -1) .contentType(contentType.type) .build()); return true; } catch (ErrorResponseException e) { e.printStackTrace(); } catch (InsufficientDataException e) { e.printStackTrace(); } catch (InternalException e) { e.printStackTrace(); } catch (InvalidBucketNameException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } catch (XmlParserException e) { e.printStackTrace(); } return false; } public enum ContentType { DEFAULT("application/octet-stream"), JPEG("image/jpeg"), PNG("image/png"), MP4("video/mp4"), CSV("text/csv"); private String type; ContentType(String type) { this.type = type; } }}