目录
- 1、搭建单机MinIO
1、搭建单机MinIO
(1)配置 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)执行docker-compose.yml
docker-compose up -d
(3)执行过后,在主机访问 控制台
服务器ip:9090
在控制台创建 Bucket。创建账户获取 Key 和 Secret
(4)运用 java sdk
pom.xml 添加依赖:
<!-- minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.2</version>
</dependency>
application.yml 添加配置:
# minio 配置
minio:
url: http://192.168.182.135:9000 # 对象存储服务的URL
accessKey: admin # Access key 账户
secretKey: 12345678 # Secret key 密码
这里的 accessKey 和 secretKey 可以是登录控制台的账户和密码,也可以是在控制台新增的账户
例如
# minio 配置
minio:
url: http://192.168.182.135:9000 #对象存储服务的URL
accessKey: 5JT8GZMHP4P9CP3UIVOJ # Access key账户
secretKey: ixElQtTEVnn2s+BgkAfE+0LmTDqlZMBrzH1HJ51o # Secret key 密码
添加 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());
}
}
写个 Controller 测试
@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);
}
}