Minio java后端连接验证
1.接口
/** * 验证minio链接地址、用户、密码是否能够链接成功 * @param storageConfig * @return * @throws Exception */ public String verifiedLink(StorageConfig storageConfig);
2.实现类
@Override public String verifiedLink(StorageConfig storageConfig) { try { MinioClient minioClient = MinioClient.builder() .endpoint(storageConfig.getServer()) .credentials(storageConfig.getUsername(), storageConfig.getPassword()) .build(); minioClient.listBuckets(); } catch (Exception e) { return MinioException.getMessageByCode(e.getMessage()); } return MinioException.SUCCESS.getMessage(); }
3.枚举类
@Getter @AllArgsConstructor public enum MinioException { SUCCESS("200","minio链接成功"), REQUEST_SIGNATURE ("The request signature we calculated does not match the signature you provided. Check your key and signing method.","密码错误"), ACCESS_KEY("The Access Key Id you provided does not exist in our records.","用户名错误"), LANDMARK("Non-XML response from server. Response code: 400","IP端口错误"), FAILED_CONNECT("Failed to connect to", "IP错误"); private final String code; private final String message; public static String getMessageByCode(String code) { for (int i = 0; i < values().length; i++) { MinioException type = values()[i]; System.out.println(type.code.indexOf(code)); if(type.code.equals(code)||code.indexOf(type.code)>-1){ return type.message; } } return "其他错误"; }