minio是一个高性能、分布式的对象存储系统,如果不想再多花钱买一个阿里云对象存储,又不想直接将文件上传到项目路径下,强烈推荐使用minio,对服务器要求不高,而且传输速度很快,方便对文件的统一管理,下面的是单机部署方式。
安装运行
查找镜像
sudo docker search minio
拉取镜像
sudo docker pull minio/minio
创建目录
一个用来存放配置,一个用来存储上传文件的目录
启动前需要先创建Minio外部挂载的配置文件( /docker/minio/config),和存储上传文件的目录( /docker/minio/data),注意要给这两个路径足够的权限,否则可能会出错
mkdir -p /docker/minio/config
mkdir -p /docker/minio/data
运行服务
docker run -p 9000:9000 -p 9090:9090 \
--net=host \
--name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=minioadmin" \
-e "MINIO_SECRET_KEY=minioadmin" \
-v /home/minio/data:/data \
-v /home/minio/config:/root/.minio \
minio/minio server \
/data --console-address ":9090" -address ":9000"
9090端口指的是minio的客户端端口
MINIO_ACCESS_KEY :账号
MINIO_SECRET_KEY :密码(账号长度必须大于等于5,密码长度必须大于等于8位)
可以通过http://localhost:9000访问minio
java调用minio服务
yml配置文件
#对象存储
minio:
endPoint: http://ip:9000
accessKey: minioadmin
secretKey: minioadmin
bucketName: bucket
将配置文件中的信息实例化成bean
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinIOProperties {
private String endPoint;
private String accessKey;
private String secretKey;
private String bucketName;
}
使用之前注入的配置对象将minio的客户端实例化
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MinIOConfig {
@Autowired
private MinIOProperties minIOProperties;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(minIOProperties.getEndPoint())
.credentials(minIOProperties.getAccessKey(), minIOProperties.getSecretKey())
.build();
}
}
封装minio的常用方法成工具类
import io.minio.*;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Component
public class MinIOUtils {
@Value("${minio.bucketName}")
private String bucketName;
@Autowired
private MinioClient minioClient;
public String uploadFile(InputStream inputStream, Long size, String filename) {
return uploadFile(inputStream,size,bucketName,filename);
}
/**
* 文件上传
* @param inputStream
* @param size
* @param bucketName
* @param filename
* @return
*/
public String uploadFile(InputStream inputStream, Long size,String bucketName, String filename) {
try {
ObjectWriteResponse objectWriteResponse = minioClient.putObject(
PutObjectArgs.builder()
.bucket(bucketName).object(filename).stream(inputStream, size, -1).build());
return objectWriteResponse.object();
} catch (ErrorResponseException | XmlParserException | ServerException | NoSuchAlgorithmException | IOException | InvalidResponseException | InvalidKeyException | InternalException | InsufficientDataException e) {
e.printStackTrace();
}
return "失败";
}
/**
* 文件下载
* @param objectName
* @return
*/
public InputStream downloadInputStream(String objectName) {
InputStream stream = null;
try {
stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
e.printStackTrace();
}
return stream;
}
/**
* 删除文件
* @param objectName
* @return
*/
public boolean deleteFile(String objectName){
try {
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
e.printStackTrace();
}
return true;
}
}