使用docker进行环境部署和启动
docker pull minio/minio docker run -d -p 9000:9000 -p 9001:9001 \ -e "MINIO_ROOT_USER=minio" \ -e "MINIO_ROOT_PASSWORD=minio123" \ -v /opt/minio/data:/data \ -v /opt/minio/config:/root/.minio \ minio/minio server --console-address ":9001" /data
部署自带管理界面 访问 http://192.168.85.143:9000
Access Key为minio Secret_key为minio123 进入系统后可以看到主界面
创建一个桶
添加名称
上传完成后可以通过地址+桶名+文件名访问 
项目集成MinIO
添加依赖
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>7.1.2</version> </dependency>
配置读取类
@Data @ConfigurationProperties(prefix = "minio") public class MinIOProperties { private String endpoint;//minio地址 private String accessKey;//用户名 private String secretKey;//密码 private String bucketName;//桶名 }
添加配置
spring: servlet: multipart: max-file-size: 20MB max-request-size: 21MB minio: endpoint: http://192.168.85.143:9000 #minio地址 accessKey: minio #用户名 secretKey: minio123 #密码 bucketName: nini #桶名
配置类
@Configuration @EnableConfigurationProperties(MinIOProperties.class) public class MinIOConfig { @Bean public MinioClient minioClient(MinIOProperties minIOProperties){ MinioClient minioClient = new MinioClient(minIOProperties.getEndpoint(), minIOProperties.getAccessKey(), minIOProperties.getSecretKey()); return minioClient; } }
模板类
@Component public class MinIOTemplate { @Autowired private MinioClient minioClient; @Autowired private MinIOProperties properties; /** * 上传文件 * @param file * @return */ public String upload(MultipartFile file) { // 原文件名 String originalFilename = file.getOriginalFilename(); // 获取文件的后缀 String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")); // 构造新的文件名,名字不重复 String objectName = UUID.randomUUID().toString() + suffix; // 上传文件 try { PutObjectArgs putObjectArgs = PutObjectArgs.builder() .contentType(file.getContentType()) .stream(file.getInputStream(), file.getSize(), -1) .bucket(properties.getBucketName()) .object(objectName) .build(); minioClient.putObject(putObjectArgs); } catch (Exception e) { throw new RuntimeException("上传文件失败: " + e.getMessage()); } return properties.getEndpoint() + "/" + properties.getBucketName() + "/" + objectName; } /** * 上传文件 * @param name * @param inputStream * @param contentType * @return */ public String upload(String name, InputStream inputStream, String contentType){ // 上传文件 try { PutObjectArgs putObjectArgs = PutObjectArgs.builder() .contentType(contentType) .stream(inputStream, inputStream.available(), -1) .bucket(properties.getBucketName()) .object(name) .build(); minioClient.putObject(putObjectArgs); } catch (Exception e) { throw new RuntimeException("上传文件失败: " + e.getMessage()); } return properties.getEndpoint() + "/" + properties.getBucketName() + "/" + name; } /** * 删除文件 * @param url */ public void delete(String url){ String objectName = url.replace(properties.getEndpoint()+"/","") .replace(properties.getBucketName()+"/",""); RemoveObjectArgs args = RemoveObjectArgs.builder() .bucket(properties.getBucketName()) .object(objectName) .build(); try { minioClient.removeObject(args); } catch (Exception e) { throw new RuntimeException("删除文件失败: " + e.getMessage()); } } /** * 下载文件 * @param url * @return */ public InputStream download(String url){ String objectName = url.replace(properties.getEndpoint()+"/","") .replace(properties.getBucketName()+"/",""); GetObjectArgs args = GetObjectArgs.builder() .bucket(properties.getBucketName()) .object(objectName) .build(); InputStream inputStream = null; try { inputStream = minioClient.getObject(args); } catch (Exception e) { throw new RuntimeException("下载文件失败: " + e.getMessage()); } return inputStream; } }
上传文件失败报错
如果报以下错误:The difference between the request time and the server's time is too large.
原因是:linux服务器时区的问题(因为虚拟机挂起后,时间也就停了)
解决方案:进行时间同步
1、查看系统时间、硬件时间
date // 查看系统时间 hwclock // 查看硬件时间
2、 安装ntpdate工具
yum -y install ntp ntpdate
3、 设置系统时间与网络时间同步
ntpdate cn.pool.ntp.org
4、将系统时间写入硬件时间
hwclock --systohc