minion相关
							parent
							
								
									6b0a8cf8cc
								
							
						
					
					
						commit
						9b3d6927d1
					
				| @ -0,0 +1,51 @@ | |||||||
|  | package com.glxp.api.config; | ||||||
|  | 
 | ||||||
|  | import io.minio.MinioClient; | ||||||
|  | import io.minio.errors.InvalidEndpointException; | ||||||
|  | import io.minio.errors.InvalidPortException; | ||||||
|  | import lombok.Data; | ||||||
|  | import lombok.RequiredArgsConstructor; | ||||||
|  | import org.springframework.boot.context.properties.ConfigurationProperties; | ||||||
|  | import org.springframework.context.annotation.Bean; | ||||||
|  | import org.springframework.context.annotation.Configuration; | ||||||
|  | 
 | ||||||
|  | @Configuration | ||||||
|  | @RequiredArgsConstructor | ||||||
|  | public class MinioConfig { | ||||||
|  | 
 | ||||||
|  |     @Data | ||||||
|  |     @ConfigurationProperties("minio") | ||||||
|  |     @Configuration | ||||||
|  |     public class MinioProperties { | ||||||
|  |         /** | ||||||
|  |          * 服务地址 | ||||||
|  |          */ | ||||||
|  |         private String endpoint; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |         /** | ||||||
|  |          * 用户名 | ||||||
|  |          */ | ||||||
|  |         private String accessKey; | ||||||
|  | 
 | ||||||
|  |         /** | ||||||
|  |          * 密码 | ||||||
|  |          */ | ||||||
|  |         private String secretKey; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 初始化客户端 | ||||||
|  |      * | ||||||
|  |      * @return 客户端 | ||||||
|  |      */ | ||||||
|  |     @Bean | ||||||
|  |     public MinioClient minioClient(MinioProperties minioProperties) { | ||||||
|  |         try { | ||||||
|  |             return new MinioClient(minioProperties.getEndpoint(), minioProperties.getAccessKey(), minioProperties.getSecretKey()); | ||||||
|  |         } catch (InvalidEndpointException | InvalidPortException e) { | ||||||
|  |             throw new RuntimeException(e); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,156 @@ | |||||||
|  | package com.glxp.api.util; | ||||||
|  | 
 | ||||||
|  | import cn.hutool.core.io.FileUtil; | ||||||
|  | import io.minio.MinioClient; | ||||||
|  | import io.minio.errors.*; | ||||||
|  | import io.minio.http.Method; | ||||||
|  | import io.minio.policy.PolicyType; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  | import org.springframework.web.multipart.MultipartFile; | ||||||
|  | import org.xmlpull.v1.XmlPullParserException; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.security.InvalidKeyException; | ||||||
|  | import java.security.NoSuchAlgorithmException; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * minio工具类 | ||||||
|  |  */ | ||||||
|  | @Slf4j | ||||||
|  | @Component | ||||||
|  | public class MinioUtil { | ||||||
|  | 
 | ||||||
|  |     private static MinioClient minioClient; | ||||||
|  | 
 | ||||||
|  |     @Autowired | ||||||
|  |     public void setMinioClient(MinioClient minioClient) { | ||||||
|  |         MinioUtil.minioClient = minioClient; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 如不存在则创建存储bucket | ||||||
|  |      * | ||||||
|  |      * @return Boolean | ||||||
|  |      */ | ||||||
|  |     private static void makeBucket(String bucketName) { | ||||||
|  |         try { | ||||||
|  | 
 | ||||||
|  |             boolean exists = minioClient.bucketExists(bucketName); | ||||||
|  |             if (!exists) { | ||||||
|  |                 minioClient.makeBucket(bucketName); | ||||||
|  |                 minioClient.setBucketPolicy(bucketName, "*", PolicyType.READ_ONLY); | ||||||
|  |             } | ||||||
|  |         } catch (Exception e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |             throw new RuntimeException(e); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 上传文件 | ||||||
|  |      * | ||||||
|  |      * @param fileFullName 文件全地址 | ||||||
|  |      * @param file         文件 | ||||||
|  |      */ | ||||||
|  |     public static void uploadFile(String fileFullName, MultipartFile file) { | ||||||
|  |         try { | ||||||
|  |             //从fileName中获取bucketName和objectName
 | ||||||
|  |             String reg = "/(\\S*?)(/\\S*)"; | ||||||
|  |             String bucketName = fileFullName.replaceFirst(reg, "$1"); | ||||||
|  |             String objectName = fileFullName.replaceFirst(reg, "$2"); | ||||||
|  |             makeBucket(bucketName); | ||||||
|  |             minioClient.putObject(bucketName, objectName, file.getInputStream(), FileUtil.getMimeType(fileFullName)); | ||||||
|  |         } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | | ||||||
|  |                  InvalidKeyException | ErrorResponseException | InternalException | InvalidArgumentException | | ||||||
|  |                  XmlPullParserException | NoResponseException e) { | ||||||
|  |             throw new RuntimeException(e); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 上传文件  内置关闭流 | ||||||
|  |      * | ||||||
|  |      * @param fileFullName    文件全地址 | ||||||
|  |      * @param file            文件 | ||||||
|  |      * @param fileContentType 文件contentType | ||||||
|  |      * @throws Exception | ||||||
|  |      */ | ||||||
|  |     public static void uploadFile(String fileFullName, InputStream file, String fileContentType) throws InvalidArgumentException, XmlPullParserException, NoResponseException { | ||||||
|  |         try { | ||||||
|  |             //从fileName中获取bucketName和objectName
 | ||||||
|  |             String reg = "/(\\S*?)(/\\S*)"; | ||||||
|  |             String bucketName = fileFullName.replaceFirst(reg, "$1"); | ||||||
|  |             String objectName = fileFullName.replaceFirst(reg, "$2"); | ||||||
|  |             makeBucket(bucketName); | ||||||
|  |             minioClient.putObject(bucketName, objectName, file, fileContentType); | ||||||
|  |         } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | | ||||||
|  |                  InvalidKeyException | ErrorResponseException | | ||||||
|  |                  InternalException e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |             throw new RuntimeException(e); | ||||||
|  |         } finally { | ||||||
|  |             try { | ||||||
|  |                 file.close(); | ||||||
|  |             } catch (IOException e) { | ||||||
|  |                 log.error(e.getMessage(), e); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 获取文件地址带有效期 | ||||||
|  |      * | ||||||
|  |      * @param fileFullName 文件全名称带路径 | ||||||
|  |      * @param expires      过期时间 | ||||||
|  |      * @return 完整访问地址 | ||||||
|  |      */ | ||||||
|  |     public static String getPresignedObjectUrl(String fileFullName, Integer expires) { | ||||||
|  |         String reg = "/(\\S*?)(/\\S*)"; | ||||||
|  |         String bucketName = fileFullName.replaceFirst(reg, "$1"); | ||||||
|  |         String objectName = fileFullName.replaceFirst(reg, "$2"); | ||||||
|  |         try { | ||||||
|  |             return minioClient.getPresignedObjectUrl(Method.GET, bucketName, objectName, expires, null); | ||||||
|  |         } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | | ||||||
|  |                  InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException | | ||||||
|  |                  InternalException | InvalidExpiresRangeException e) { | ||||||
|  |             throw new RuntimeException(e); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 获取文件地址带有效期 | ||||||
|  |      * | ||||||
|  |      * @param fileFullName 文件全名称带路径 | ||||||
|  |      * @return inputStream | ||||||
|  |      */ | ||||||
|  |     public static InputStream getFileInputStream(String fileFullName) { | ||||||
|  |         String reg = "/(\\S*?)(/\\S*)"; | ||||||
|  |         String bucketName = fileFullName.replaceFirst(reg, "$1"); | ||||||
|  |         String objectName = fileFullName.replaceFirst(reg, "$2"); | ||||||
|  |         try { | ||||||
|  |             return minioClient.getObject(bucketName, objectName); | ||||||
|  |         } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | | ||||||
|  |                  InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException | | ||||||
|  |                  InternalException | InvalidArgumentException e) { | ||||||
|  |             throw new RuntimeException(e); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void deleteFile(String fileFullName) { | ||||||
|  |         String reg = "/(\\S*?)(/\\S*)"; | ||||||
|  |         String bucketName = fileFullName.replaceFirst(reg, "$1"); | ||||||
|  |         String objectName = fileFullName.replaceFirst(reg, "$2"); | ||||||
|  |         try { | ||||||
|  |             minioClient.removeObject(bucketName, objectName); | ||||||
|  |         } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | | ||||||
|  |                  InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException | | ||||||
|  |                  InternalException e) { | ||||||
|  | //            throw new RuntimeException(e);
 | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in New Issue