文件存储MinIO读写工具类

内容读工具类MinioReadUtils

package com.assoft.fmc.common.utils;

import com.assoft.fmc.common.form.ItemData;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletResponse;


import com.assoft.fmc.common.constant.MinioConstants;
import org.springframework.util.StringUtils;

public class MinioReadUtils {
	private static MinioClient minioClient;
	public static List docList;
	static {
		initMinioClientInstance();
	}

	private static synchronized void initMinioClientInstance() {
		if (minioClient == null) {
			minioClient = MinioClient.builder()
					.endpoint(MinioConstants.FLOW_MINIO_IP, MinioConstants.FLOW_MINIO_PORT, false) // https or not
					.credentials(MinioConstants.FLOW_ACCESS_KEY_WRITE, MinioConstants.FLOW_SECRET_KEY_WRITE).build();
		}
	}

	/**
	 * 从bucket获取指定对象的输入流,后续可使用输入流读取对象 getObject与minio server连接默认保持5分钟, 每隔15s由minio
	 * server向客户端发送keep-alive check,5分钟后由客户端主动发起关闭连接
	 */
	public static InputStream getObject(String bucketName, String objectName) throws Exception {
		GetObjectArgs args = GetObjectArgs.builder().bucket(bucketName).object(objectName).build();
		return minioClient.getObject(args);
	}
	/**
	 * 判断文件是否存在,不能判断文件夹
	 * @param bucketName
	 * @param objectName
	 * @return
	 * @throws Exception
	 */
	public static boolean checkFileExist(String bucketName, String objectName) {
		try {
			StatObjectArgs args = StatObjectArgs.builder().bucket(bucketName).object(objectName).build();
			 minioClient.statObject(args);
			 return true;
		} catch (Exception e) {
			 return false;
		}
		
	}
	/**
	 * 获取对象的临时访问url,有效期5分钟
	 */
	public static String getObjectURL(String bucketName, String objectName) throws Exception {
		GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(objectName)
				.expiry(5, TimeUnit.MINUTES).method(Method.GET).build();
		return minioClient.getPresignedObjectUrl(args);
	}

	
	/**
	 * 从minio下载文件,直接通过response传输
	 */
	public static void downloadFile(String bucketName, String objectName, HttpServletResponse response) throws Exception {
		String[] objList=objectName.split("/");
		String	fileName=objList[objList.length-1];
		try (InputStream is = getObject(bucketName, objectName);
				BufferedInputStream bis = new BufferedInputStream(is);
				OutputStream os = response.getOutputStream()) {
			// try {
			/*
			 * InputStream is = getObject(bucketName, filePath); BufferedInputStream bis =
			 * new BufferedInputStream(is); OutputStream os = response.getOutputStream();
			 */
			response.setContentType("application/force-download;charset=utf-8");// 设置强制下载而不是直接打开
			response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));// 设置文件名

			byte[] buffer = new byte[1024 * 1024 * 1024]; // buffer 1M
			int offset = bis.read(buffer);
			while (offset != -1) {
				os.write(buffer, 0, offset);
				offset = bis.read(buffer);
			}
			os.flush();
		} catch (Exception e) {
			throw new RuntimeException("下载文件失败", e);
		}
	}

	public static List getAllBuckets() {
		try {
			List buckets = minioClient.listBuckets();
			return buckets;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

//	遍历找到桶下所有路径
	public static void listDocs(String bucketName,String prefix){
		Iterable> listObjects=new ArrayList<>();
		if(StringUtils.hasLength(prefix)){
			listObjects = minioClient.listObjects(ListObjectsArgs.builder()
					.bucket(bucketName)
					.prefix(prefix)
					.build());
		}else {
			listObjects = minioClient.listObjects(ListObjectsArgs.builder()
					.bucket(bucketName)
					.build());
		}
		try{
		for (Result result : listObjects) {
			Item item = result.get();
			if(item.isDir()){
				docList.add(item.objectName());
				listDocs(bucketName,item.objectName());
			}
		}
		}catch (Exception e){

		}
	}

	public static Iterable> listObjects(String bucketName,String prefix,String title,boolean recursive){
		try {
			Iterable> listObjects=new ArrayList<>();
			if(StringUtils.hasLength(title)){
				listObjects = minioClient.listObjects(ListObjectsArgs.builder()
						.bucket(bucketName)
						.prefix(prefix)
						.startAfter(title)
						.build());
			}
			else if(StringUtils.hasLength(prefix)){
				listObjects = minioClient.listObjects(ListObjectsArgs.builder()
						.bucket(bucketName)
						.prefix(prefix)
						.recursive(recursive) // 递归
						.build());
			}else {
				listObjects = minioClient.listObjects(ListObjectsArgs.builder()
						.bucket(bucketName)
						.recursive(recursive) // 递归
						.build());
			}
			return listObjects;
		} catch (Exception e) {
			System.out.println("Error occurred: " + e);
			return null;
		}
	}

}

内容写工具类MinioWriteUtils

package com.assoft.fmc.common.utils;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.SnowballObject;
import io.minio.UploadObjectArgs;
import io.minio.UploadSnowballObjectsArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.assoft.fmc.common.constant.MinioConstants;

public class MinioWriteUtils {
	private static MinioClient minioClient;
	static {
		initMinioClientInstance();
	}
	private static synchronized void initMinioClientInstance() {
		if (minioClient == null) {
			minioClient = MinioClient.builder().endpoint("http://"+MinioConstants.FLOW_MINIO_IP+":"+MinioConstants.FLOW_MINIO_PORT)
					.credentials(MinioConstants.FLOW_ACCESS_KEY_WRITE, MinioConstants.FLOW_SECRET_KEY_WRITE).build();
		}
	}

	private static void createBucket(String bucketName) throws InvalidKeyException, ErrorResponseException,
			InsufficientDataException, InternalException, InvalidResponseException, NoSuchAlgorithmException,
			ServerException, XmlParserException, IllegalArgumentException, IOException {
		BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(bucketName).build();
		if (!minioClient.bucketExists(bucketExistsArgs)) {
			MakeBucketArgs makeArgs = MakeBucketArgs.builder().bucket(bucketName).build();
			minioClient.makeBucket(makeArgs);
		}
	}
	/**
	 * 删除对象
	 */
	public static void removeObject(String bucketName, String objectName) throws Exception {
		RemoveObjectArgs args = RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build();
		minioClient.removeObject(args);
	}

	/**
	 * 从给定输入流中传输对象并放入bucket
	 */
	public static ObjectWriteResponse putObject(String bucketName, String objectName, InputStream stream, long objectSize,
			String contentType) throws Exception {
		if (StringUtils.isBlank(bucketName)) {
			throw new RuntimeException("bucketName不能为空!");
		}
		createBucket(bucketName);
		long partSize = -1; // objectSize已知,partSize设为-1意为自动设置
		PutObjectArgs putArgs = PutObjectArgs.builder().bucket(bucketName).object(objectName)
				.stream(stream, objectSize, partSize).contentType(contentType).build();
		ObjectWriteResponse response = minioClient.putObject(putArgs);
		return response;
	}
	   /**
     * 上传MultipartFile
     * @param bucketName 文件存放的bucket
     * @param filePath 文件在bucket里的全目录
     * */
    public ObjectWriteResponse uploadFile(String bucketName, String filePath, MultipartFile file) throws Exception{
    	return putObject(bucketName, filePath, file.getInputStream(), file.getSize(), file.getContentType());
    }
    public static ObjectWriteResponse uploadFile(String bucketName, String objectName, String fileName) throws InvalidKeyException, ErrorResponseException, InsufficientDataException, InternalException, InvalidResponseException, NoSuchAlgorithmException, ServerException, XmlParserException, IOException {
    	createBucket(bucketName);
    	UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).build();
    	ObjectWriteResponse response = minioClient.uploadObject(uploadObjectArgs);
    	
    	return response;
    }
    public static boolean uploadFiles(String bucketName, String basePath, List fileNames) throws InvalidKeyException, ErrorResponseException, InsufficientDataException, InternalException, InvalidResponseException, NoSuchAlgorithmException, ServerException, XmlParserException, IllegalArgumentException, IOException  {
    	createBucket(bucketName);
    	List objects = new ArrayList();
    	for (String fileName : fileNames) {
//    		File fiel  = new File(basePath+fileName);
//    		
//    		objects.add(new SnowballObject(fileName, new f, bytes.length, ZonedDateTime.now()));
    		byte[] bytes = fileToBytes(basePath+fileName);
    	    objects.add(new SnowballObject(fileName, new ByteArrayInputStream(bytes), bytes.length, ZonedDateTime.now()));
		}
    	try {
            minioClient.uploadSnowballObjects(
                     UploadSnowballObjectsArgs.builder().bucket(bucketName).objects(objects).build());
             return true;
         } catch (Exception e) {
             e.printStackTrace();
             return false;
         }
    }
    /**
     * Description 文件转字节数组
     * Create By Mr.Fang
     *
     * @param path 文件路径
     * @return byte[] 字节数组
     * @time 2022/7/10 10:55
     **/
    public static byte[] fileToBytes(String path) {
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            bos = new ByteArrayOutputStream();
            fis = new FileInputStream(path);
            int temp;
            byte[] bt = new byte[1024 * 10];
            while ((temp = fis.read(bt)) != -1) {
                bos.write(bt, 0, temp);
            }
            bos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (Objects.nonNull(fis)) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bos.toByteArray();
    }
   /* List objects = new ArrayList();
    objects.add(
        new SnowballObject(
            "my-object-one",
            new ByteArrayInputStream("hello".getBytes(StandardCharsets.UTF_8)),
            5,
            null));
    objects.add(
        new SnowballObject(
            "my-object-two",
            new ByteArrayInputStream("java".getBytes(StandardCharsets.UTF_8)),
            4,
            null));
    minioClient.uploadSnowballObjects(
        UploadSnowballObjectsArgs.builder().bucket("my-bucketname").objects(objects).build());*/
}

你可能感兴趣的:(java,servlet,spring)