Table of contents
1. Build a stand-alone MinIO
(1) Configure docker-compose.yml
minio:
image: minio/minio
container_name: "chain-minio"
ports:
- 9000:9000 # 服务端口
- 9090:9090 # 控制台端口
environment:
- "MINIO_ACCESS_KEY=admin" # 登陆账号
- "MINIO_SECRET_KEY=12345678" # 密码
volumes: # 挂载数据卷
- "/dockerdata/minio/data:/data"
- "/dockerdata/minio/config:/root/.minio"
command: server /data --console-address ":9090" # 容器启动时执行的指令
restart: always # 容器退出时总是重启容器
(2) Execute docker-compose.yml
docker-compose up -d
(3) After execution, access the console on the host
服务器ip:9090
Create a Bucket on the console. Create an account to get Key and Secret
(4) Use java sdk
pom.xml add dependencies:
<!-- minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.2</version>
</dependency>
application.yml add configuration:
# minio 配置
minio:
url: http://192.168.182.135:9000 # 对象存储服务的URL
accessKey: admin # Access key 账户
secretKey: 12345678 # Secret key 密码
The accessKey and secretKey here can be the account and password for logging in to the console, or they can be accounts newly added in the console.
For example
# minio 配置
minio:
url: http://192.168.182.135:9000 #对象存储服务的URL
accessKey: 5JT8GZMHP4P9CP3UIVOJ # Access key账户
secretKey: ixElQtTEVnn2s+BgkAfE+0LmTDqlZMBrzH1HJ51o # Secret key 密码
Add Config
@Configuration
public class MinioConfig {
@Value("${minio.url}")
private String url;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Bean
public MinioClient getMinioClient() {
MinioClient minioClient = MinioClient
.builder()
.endpoint(url)
.credentials(accessKey, secretKey)
.build();
return minioClient;
}
}
MinioUtil
@Component
public class MinioUtil {
@Resource
private MinioClient minioClient;
/**
* 创建一个桶
*/
public void createBucket(String bucket) throws Exception {
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
}
}
/**
* 上传一个文件
*/
public void uploadFile(InputStream stream, String bucket, String objectName) throws Exception {
minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
.stream(stream, -1, 10485760).build());
}
/**
* 列出所有的桶
*/
public List<String> listBuckets() throws Exception {
List<Bucket> list = minioClient.listBuckets();
List<String> names = new ArrayList<>();
list.forEach(b -> {
names.add(b.name());
});
return names;
}
/**
* 下载一个文件
*/
public InputStream download(String bucket, String objectName) throws Exception {
InputStream stream = minioClient.getObject(
GetObjectArgs.builder().bucket(bucket).object(objectName).build());
return stream;
}
/**
* 删除一个桶
*/
public void deleteBucket(String bucket) throws Exception {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
}
/**
* 删除一个对象
*/
public void deleteObject(String bucket, String objectName) throws Exception {
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
}
}
Write a Controller test
@Api(tags = "文件操作接口")
@RestController
public class MinioController{
@Resource
MinioUtil minioUtil;
@ApiOperation("上传一个文件")
@RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
@ResponseBody
public Response<Integer> fileupload(@RequestParam MultipartFile uploadfile, @RequestParam String bucket,
@RequestParam(required=false) String objectName) throws Exception {
minioUtil.createBucket(bucket);
if (objectName != null) {
minioUtil.uploadFile(uploadfile.getInputStream(), bucket, "/"+uploadfile.getOriginalFilename());
} else {
minioUtil.uploadFile(uploadfile.getInputStream(), bucket, uploadfile.getOriginalFilename());
}
return ResponseUtils.success(1);
}
@ApiOperation("下载一个文件")
@RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
@ResponseBody
public void downloadFile(@RequestParam String bucket, @RequestParam String objectName,
HttpServletResponse response) throws Exception {
InputStream stream = minioUtil.download(bucket, objectName);
ServletOutputStream output = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
IOUtils.copy(stream, output);
}
}