MinioUtils.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.futu.goose.utils;
  2. import io.minio.*;
  3. import io.minio.errors.*;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import java.io.IOException;
  8. import java.security.InvalidKeyException;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.util.UUID;
  11. @Component
  12. public class MinioUtils {
  13. @Value("${minio.endpoint}")
  14. private String endpoint;
  15. @Value("${minio.accessKey}")
  16. private String accessKey;
  17. @Value("${minio.accessSecret}")
  18. private String accessSecret;
  19. @Value("${minio.bucketName}")
  20. private String bucketName;
  21. private MinioClient minioClient;
  22. public MinioUtils() {
  23. }
  24. private MinioClient getMinioClient() {
  25. if (minioClient == null) {
  26. minioClient = MinioClient.builder()
  27. .endpoint(endpoint)
  28. .credentials(accessKey, accessSecret)
  29. .build();
  30. }
  31. return minioClient;
  32. }
  33. /**
  34. * 生成随机文件名
  35. * @param originalFileName 原始文件名
  36. * @return 随机文件名
  37. */
  38. private String generateRandomFileName(String originalFileName) {
  39. // 获取文件扩展名
  40. String extension = "";
  41. int dotIndex = originalFileName.lastIndexOf('.');
  42. if (dotIndex > 0) {
  43. extension = originalFileName.substring(dotIndex);
  44. }
  45. // 生成随机 UUID 作为文件名
  46. String uuid = UUID.randomUUID().toString();
  47. return uuid + extension;
  48. }
  49. /**
  50. * 上传图片到 MinIO
  51. * @param file 上传的图片文件
  52. * @return 图片的访问 URL
  53. * @throws IOException 输入输出异常
  54. * @throws ServerException 服务器异常
  55. * @throws InsufficientDataException 数据不足异常
  56. * @throws ErrorResponseException 错误响应异常
  57. * @throws NoSuchAlgorithmException 无此算法异常
  58. * @throws InvalidKeyException 无效密钥异常
  59. * @throws InvalidResponseException 无效响应异常
  60. * @throws XmlParserException XML 解析异常
  61. * @throws InternalException 内部异常
  62. */
  63. public String uploadImage(MultipartFile file) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, InvalidBucketNameException, RegionConflictException {
  64. MinioClient client = getMinioClient();
  65. // 检查存储桶是否存在
  66. if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
  67. client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
  68. }
  69. // 生成随机文件名
  70. String objectName = generateRandomFileName(file.getOriginalFilename());
  71. // 上传文件
  72. client.putObject(PutObjectArgs.builder()
  73. .bucket(bucketName)
  74. .object(objectName)
  75. .stream(file.getInputStream(), file.getSize(), -1)
  76. .contentType(file.getContentType())
  77. .build());
  78. // 返回文件的访问 URL
  79. return endpoint + "/" + bucketName + "/" + objectName;
  80. }
  81. }