中文文档:
https://docs.min.io/cn/
docker部署
docker run -p 9000:9000 \
--name minio_test \
-v /home/soft/minio:/data \
-e "MINIO_ROOT_USER=AKIAIOSFODNN7EXAMPLE" \
-e "MINIO_ROOT_PASSWORD=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" \
minio/minio:edge server /data
访问:http://192.168.11.223:9000/
账号密码为docker启动的MINIO_ROOT_USER、MINIO_ROOT_PASSWORD参数
主界面
Java Client SDK提供的API实现文件上传
1.pom.xml添加
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.0.3</version>
</dependency>
2.编写demo
@Test
public void minioTest() {
File file = new File("C:\\Users\\Administrator\\Downloads\\echarts.png");
try (InputStream in = new FileInputStream(file)) {
MinioClient minioClient =
MinioClient.builder()
//MinIO服务的URL
.endpoint("http://192.168.11.223:9000")
//账号-密码
.credentials("AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")
.build();
String bucketName = "my-bucket01";
// 检查存储桶是否存在
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (found) {
System.out.println(bucketName + " 存在!");
} else {
System.out.println(bucketName + " 不存在, 执行创建存储桶!");
// 创建存储桶
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket(bucketName)
.build());
}
//这里用下hutool的工具类-根据文件扩展名获得MimeType
String mime = FileUtil.getMimeType(file.getName());
System.out.println("mime: " + mime);
if (mime == null) {
//二进制流,不知道下载文件类型
mime = "application/octet-stream";
}
// 使用putObject上传一个文件到存储桶中。
ObjectWriteResponse objectWriteResponse = minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(file.getName()).stream(
in, file.length(), -1)
//文件类型,可不传,传了之后,浏览器访问分享文件时候,response header会返回"Content-Type: image/png",方便预览
.contentType(mime)
.build());
System.out.println(objectWriteResponse.etag());
System.out.println("文件上传ok!");
} catch (Exception e) {
System.out.println("异常: " + e);
}
}
效果: