MinIO
官网:https://docs.minio.io/cn/minio-client-complete-guide.html、
官网支持docker部署
服务端
在官网直接下minio
拖到Linux里,给用户授权
sudo chmod +x minio
然后启动
./minio server /data
下面说默认密码不安全啥的,无视
初始账号,密码为minioadmin
直接在对应ip和9000端口查看。
客户端
同上下载授权
./mc config host add minio-server http://192.168.44.128:9000 username password --api s3v4
Add Success…
一片绿就是成功。
服务端页面填个桶(这里的桶bucket类似于MySQL中的database的概念),客户端查看
./mc ls minio-server
Java
整合到我的项目中:
- 添加依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>3.0.10</version>
</dependency>
- yml中添加对应需要的属性
minio:
endpoint: http://192.168.44.128
port: 9000
username: minioadmin
password: minioadmin
bucket: blogger
- Config配置自动注入
@Component
public class MinioConfig {
@Value("${minio.endpoint}")
String endpoint;
@Value("${minio.port}")
String port;
@Value("${minio.username}")
String username;
@Value("${minio.password}")
String password;
@Value("${minio.bucket}")
String bucket;
public String getEndpoint() {
return endpoint;
}
public String getPort() {
return port;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getBucket() {
return bucket;
}
// @Override
// public String toString() {
// return "MinioConfig{" +
// "endpoint='" + endpoint + '\'' +
// ", port='" + port + '\'' +
// ", username='" + username + '\'' +
// ", password='" + password + '\'' +
// ", bucket='" + bucket + '\'' +
// '}';
// }
}
- Util类进行封装
@Component
public class FileUtil {
@Autowired
MinioConfig minioConfig;
MinioClient minioClient;
private void init() {
try {
this.minioClient = new MinioClient(minioConfig.getEndpoint() + ":" + minioConfig.getPort(),
minioConfig.getUsername(), minioConfig.getPassword());
if (!minioClient.bucketExists(minioConfig.getBucket())) {
minioClient.makeBucket(minioConfig.getBucket());
}
} catch (Exception e) {
System.err.println("Minio Client Init Filed");
e.printStackTrace();
}
}
public void upload(String dir, String fileName, InputStream inputStream) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidArgumentException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException {
init();
minioClient.putObject(minioConfig.getBucket(),
dir + fileName,
inputStream,
null);
}
public InputStream getInputStream(String dir, String fileName) {
init();
try {
return minioClient.getObject(minioConfig.getBucket(), dir + fileName);
} catch (Exception e) {
return null;
}
}
// @Override
// public String toString() {
// return "FileUtil{" +
// "minioConfig=" + minioConfig +
// '}';
// }
}