docker run -p 9000:9000 -p 9001:9001 -v /mydata/minio/data:/data minio/minio server /data --console-address ":9001java导包
最好是这个版本,其他版本尝试过都出bug了
配置文件io.minio minio8.2.1
spring: # 上传文件大小设置 servlet: multipart: enabled: true max-file-size: 50MB minio: endpoint: xxx:9000 accesskey: xxx secretkey: xxx bucketName: xxx*** 作
1、编写一个属性文件
@Data @Component @ConfigurationProperties(prefix = "minio") // 从配置文件的前缀拿 public class MinioProperties { private String endpoint; private String accessKey; private String secretKey; }
2、编写一个minioClient
@Configuration public class MinioConfig { @Resource private MinioProperties minioProperties; @Bean public MinioClient minioClient() { System.out.println(minioProperties.getAccessKey()); System.out.println(minioProperties.getSecretKey()); MinioClient minioClient = MinioClient.builder() .endpoint(minioProperties.getEndpoint()) .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey()) .build(); return minioClient; } }
3、上传文件Api
public class MinioServiceImpl implements MinioService { @Value("${minio.bucketName}") private String bucketName; @Value("${minio.endpoint}") private String endPoint; @Resource private MinioClient minioClient; @Override public List本地浏览设置uploadFile(MultipartFile[] file) throws ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException { if (file == null || file.length == 0) { throw new APIException(ResultCode.PARAM_IS_BLANK); } List fileUrlList = new ArrayList<>(file.length); String url = ""; for (MultipartFile multipartFile : file) { // 1.获取文件名 String originalFilename = multipartFile.getOriginalFilename(); // 2.截取后缀名 String imgSuffix = originalFilename.substring(originalFilename.lastIndexOf(".")); // 3.生成唯一名 String newFileName = UUID.randomUUID().toString() + imgSuffix; // 4.日期目录 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); String dataPath = dateFormat.format(new Date()); // 5.合成路径 String finalFileName = dataPath + "/" + newFileName; // 别忘了bucketName url = endPoint + "/" + bucketName + "/" + finalFileName; try { // 文件上传 InputStream in = multipartFile.getInputStream(); minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(finalFileName).stream( in, multipartFile.getSize(), -1) .contentType(multipartFile.getContentType()) .build()); in.close(); fileUrlList.add(url); } catch (IOException e) { throw new APIException(ResultCode.COMMON_FAIL); } } return fileUrlList; } }
通过上面这串url就可以直接访问图片了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)