|
@@ -0,0 +1,98 @@
|
|
|
+package api.common.util;
|
|
|
+
|
|
|
+import io.minio.*;
|
|
|
+import io.minio.errors.ErrorResponseException;
|
|
|
+import io.minio.errors.InsufficientDataException;
|
|
|
+import io.minio.errors.InvalidResponseException;
|
|
|
+import io.minio.errors.XmlParserException;
|
|
|
+import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.security.InvalidKeyException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+
|
|
|
+public class MinioUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断 bucket 是否存在
|
|
|
+ */
|
|
|
+ public static boolean isBucketExist(MinioClient minioClient, String bucketName) throws InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
|
|
|
+ return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建 bucket
|
|
|
+ */
|
|
|
+ public static void createBucket(MinioClient minioClient, String bucketName) throws InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
|
|
|
+ if (!isBucketExist(minioClient, bucketName)) {
|
|
|
+ minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过文件路径上传文件上传文件
|
|
|
+ */
|
|
|
+ public static void uploadByFilePath(
|
|
|
+ MinioClient minioClient,
|
|
|
+ String sourceFilePath,
|
|
|
+ String bucketName,
|
|
|
+ String objectName
|
|
|
+ ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
|
|
|
+ minioClient.uploadObject(UploadObjectArgs.builder()
|
|
|
+ .filename(sourceFilePath)
|
|
|
+ .bucket(bucketName)
|
|
|
+ .object(objectName)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过文件路径上传文件上传文件
|
|
|
+ *
|
|
|
+ * @param partSize 分片最小 5MB
|
|
|
+ */
|
|
|
+ public static void uploadByStream(
|
|
|
+ MinioClient minioClient,
|
|
|
+ InputStream inputStream,
|
|
|
+ long objectSize,
|
|
|
+ long partSize,
|
|
|
+ String bucketName,
|
|
|
+ String objectName
|
|
|
+ ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
|
|
|
+ minioClient.putObject(PutObjectArgs.builder()
|
|
|
+ .stream(inputStream, objectSize, partSize)
|
|
|
+ .bucket(bucketName)
|
|
|
+ .object(objectName)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载文件
|
|
|
+ */
|
|
|
+ public static void downloadToFile(
|
|
|
+ MinioClient minioClient,
|
|
|
+ String bucketName,
|
|
|
+ String objectName,
|
|
|
+ String targetFilePath
|
|
|
+ ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
|
|
|
+ minioClient.downloadObject(DownloadObjectArgs.builder()
|
|
|
+ .bucket(bucketName)
|
|
|
+ .object(objectName)
|
|
|
+ .filename(targetFilePath)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载文件
|
|
|
+ */
|
|
|
+ public static InputStream downloadToStream(
|
|
|
+ MinioClient minioClient,
|
|
|
+ String bucketName,
|
|
|
+ String objectName
|
|
|
+ ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
|
|
|
+ return minioClient.getObject(GetObjectArgs.builder()
|
|
|
+ .bucket(bucketName)
|
|
|
+ .object(objectName)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+}
|