package com.ard.tianyu.component.minio.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import com.ard.common.utils.ServletUtils; import com.ard.common.utils.spring.SpringUtils; import io.minio.*; import org.springframework.web.multipart.MultipartFile; import io.minio.http.Method; /** * Minio 文件存储工具类 * * @author ruoyi */ public class MinioUtil { /** * 判断桶是否存在 * * @param bucketName 桶名称 * @return 如果桶存在返回 true,否则返回 false * @throws IOException */ public static boolean bucketExists(String bucketName) throws IOException { MinioClient minioClient = SpringUtils.getBean(MinioClient.class); try { return minioClient.bucketExists(io.minio.BucketExistsArgs.builder().bucket(bucketName).build()); } catch (Exception e) { throw new IOException("Error checking if bucket exists", e); } } // 创建桶 public static void createBucket(String bucketName) throws IOException { MinioClient minioClient = SpringUtils.getBean(MinioClient.class); try { minioClient.makeBucket(io.minio.MakeBucketArgs.builder().bucket(bucketName).build()); setBucketPolicy(bucketName); } catch (Exception e) { throw new IOException("Error creating bucket", e); } } /** * 设置桶策略 * 刘苏义 * 2023/9/20 10:01:39 */ public static boolean setBucketPolicy(String bucket) throws Exception { MinioClient minioClient = SpringUtils.getBean(MinioClient.class); try { //设置策略 String sb = "{\"Version\":\"2012-10-17\"," + "\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":" + "{\"AWS\":[\"*\"]},\"Action\":[\"s3:ListBucket\",\"s3:ListBucketMultipartUploads\"," + "\"s3:GetBucketLocation\"],\"Resource\":[\"arn:aws:s3:::" + bucket + "\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:PutObject\",\"s3:AbortMultipartUpload\",\"s3:DeleteObject\",\"s3:GetObject\",\"s3:ListMultipartUploadParts\"],\"Resource\":[\"arn:aws:s3:::" + bucket + "/*\"]}]}"; SetBucketPolicyArgs setBucketPolicyArgs = SetBucketPolicyArgs.builder() .bucket(bucket) .config(sb) .build(); minioClient.setBucketPolicy(setBucketPolicyArgs); return true; } catch (Exception ex) { throw new Exception("设置桶策略异常:" + ex.getMessage()); } } /** * 上传文件 * * @param bucketName 桶名称 * @param fileName * @throws IOException */ public static String uploadFile(String bucketName, String fileName, MultipartFile multipartFile) throws IOException { String url = ""; MinioClient minioClient = SpringUtils.getBean(MinioClient.class); try (InputStream inputStream = multipartFile.getInputStream()) { minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build()); url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build()); url = url.substring(0, url.indexOf('?')); return ServletUtils.urlDecode(url); } catch (Exception e) { throw new IOException(e.getMessage(), e); } } /** * 上传本地文件 * * @param bucketName 桶名称 * @param fileName 存储到 Minio 的文件名 * @param localFilePath 本地文件绝对路径 * @return 访问 URL * @throws IOException */ public static String uploadLocalFile(String bucketName, String fileName, String localFilePath) throws IOException { MinioClient minioClient = SpringUtils.getBean(MinioClient.class); try (InputStream inputStream = new FileInputStream(localFilePath)) { File file = new File(localFilePath); minioClient.putObject( PutObjectArgs.builder() .bucket(bucketName) .object(fileName) .stream(inputStream, file.length(), -1) .contentType("application/octet-stream") // 可根据需求自行修改 .build() ); String url = minioClient.getPresignedObjectUrl( GetPresignedObjectUrlArgs.builder() .bucket(bucketName) .object(fileName) .method(Method.GET) .build() ); return ServletUtils.urlDecode(url.substring(0, url.indexOf('?'))); } catch (Exception e) { throw new IOException(e.getMessage(), e); } } public static String uploadInputStream(String bucketName, String fileName, InputStream inputStream, long size, String contentType) throws IOException { MinioClient minioClient = SpringUtils.getBean(MinioClient.class); if (!bucketExists(bucketName)) { createBucket(bucketName); } try { minioClient.putObject( PutObjectArgs.builder() .bucket(bucketName) .object(fileName) .stream(inputStream, size, -1) .contentType(contentType) .build() ); String url = minioClient.getPresignedObjectUrl( GetPresignedObjectUrlArgs.builder() .bucket(bucketName) .object(fileName) .method(Method.GET) .build() ); url = url.substring(0, url.indexOf('?')); return ServletUtils.urlDecode(url); } catch (Exception e) { throw new IOException(e.getMessage(), e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ignore) { } } } } /** * 判断文件是否存在 */ public static boolean exists(String bucketName, String fileName) throws IOException { MinioClient minioClient = SpringUtils.getBean(MinioClient.class); if (!bucketExists(bucketName)) { createBucket(bucketName); } try { minioClient.statObject( io.minio.StatObjectArgs.builder() .bucket(bucketName) .object(fileName) .build() ); return true; } catch (io.minio.errors.ErrorResponseException e) { if ("NoSuchKey".equals(e.errorResponse().code()) || "NoSuchObject".equals(e.errorResponse().code())) { return false; } throw new IOException(e.getMessage(), e); } catch (Exception e) { throw new IOException(e.getMessage(), e); } } /** * 删除文件 */ public static void delete(String bucketName, String fileName) throws IOException { MinioClient minioClient = SpringUtils.getBean(MinioClient.class); try { minioClient.removeObject( io.minio.RemoveObjectArgs.builder() .bucket(bucketName) .object(fileName) .build() ); } catch (Exception e) { throw new IOException(e.getMessage(), e); } } }