docker-compose安装minio,并设置挂载目录
java">version: '3.8'services:minio:image: minio/miniocontainer_name: minioenvironment:MINIO_ROOT_USER: rootMINIO_ROOT_PASSWORD: 123456789restarts: alwaysprivileged: trueports:- "9000:9000"- "9001:9001"volumes:- ./minio-data:/datacommand: server /data --console-address ":9001"
其中请自行修改用户名、密码、挂载目录和对应的端口与映射(密码至少需要9位,否则启动报错)
其中9001是后台服务的端口
springboot集成minio
pom.xml
<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.2.0</version>
</dependency>
application.yml
minio:endpoint: http://xxx:9000 #Minio服务所在地址bucket: test #存储桶名称accessKey: root #访问的keysecretKey: 1234 #访问的秘钥
springboot读取配置文件并注册bean
配置属性的实体类
java">@Data
@ConfigurationProperties(prefix = "minio")
@Component
public class MinioProp {private String endpoint;private String accessKey;private String secretKey;private String bucket;
}
配置类
java">@Configuration
public class MinioConfig {@Autowiredprivate MinioProp minioProp;@Beanpublic MinioClient minioClient(){return MinioClient.builder().endpoint(minioProp.getEndpoint()).credentials(minioProp.getAccessKey(), minioProp.getSecretKey()).build();}
}
springboot创建桶并设置桶仅可读
以下代码仅做示例
java">@RestController
@Slf4j
public class TestController {@Autowiredprivate MinioClient minioClient;@Autowiredprivate MinioProp minioProp;@GetMapping("/init")public R<String> init() {try {boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProp.getBucket()).build());if (!exists) {minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProp.getBucket()).build());}} catch (Exception e) {return R.fail(ResultStatusEnum.FAIL);}return R.ok("init success");}@GetMapping("/set-public")public R<String> setPublic() {String policyJson = "{\n" +" \"Version\": \"2012-10-17\",\n" +" \"Statement\": [\n" +" {\n" +" \"Effect\": \"Allow\",\n" +" \"Principal\": {\n" +" \"AWS\": [\n" +" \"*\"\n" +" ]\n" +" },\n" +" \"Action\": [\n" +" \"s3:GetObject\"\n" +" ],\n" +" \"Resource\": [\n" +" \"arn:aws:s3:::" + minioProp.getBucket() + "/*\"\n" +" ]\n" +" }\n" +" ]\n" +"}";try {minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(minioProp.getBucket()).config(policyJson).build());} catch (Exception e) {return R.fail(ResultStatusEnum.FAIL);}return R.ok("set public success");}
}
上传文件
java">@Autowired
private MinioClient minioClient;
@Autowired
private MinioProp minioProp;
@PostMapping
public R<String> uploadImage(@RequestParam("file") MultipartFile file) {try {String originalFilename = file.getOriginalFilename();String type = FileUtil.extName(originalFilename);String path = "testPath";String fileName = IdUtil.fastSimpleUUID() + StrUtil.DOT + type;minioClient.putObject(PutObjectArgs.builder().bucket(minioProp.getBucket()).object(path+ '/' + fileName).contentType(file.getContentType()).stream(file.getInputStream(), file.getInputStream().available(), -1).build());log.info("文件上传成功:{}", originalFilename);return R.ok(minioProp.getEndpoint() +"/" + minioProp.getBucket() +"/" + path+"/" + fileName);} catch (Exception e) {log.error("上传文件失败", e);return R.fail(ResultStatusEnum.FAIL);}
}
此时通过上面方法返回的地址即可访问该文件
通过9001端口进入到后台页面,可以发现该文件已上传