minio官网
MinIO是根据Apache许可证v2.0发布的高性能对象存储。它与Amazon S3云存储服务兼容。使用MinIO构建用于机器学习,分析和应用程序数据工作负载的高性能基础架构。
它最适合存储非结构化数据,如照片,视频,日志文件,备份和容器/ VM映像。对象的大小可以从几KB到最大5TB
Minio服务器足够轻,可以与应用程序堆栈捆绑在一起,类似于NodeJS,Redis和MySQL。
个人而言安装简单,快速测试
docker pull minio/minio
设置自定的数据目录,可以根据自己的习惯设置
mkdir /data/docker-config/docker-minio/data
docker run --name minio -v /data/docker-conf/docker-minio/data:/data -p 7900:9000 --privileged=true -d minio/minio server /data
server:
port: 8001
os:
minio:
url: http://localhost:7900
secretKey: minioadmin
accessKey: minioadmin
multipart:
maxRequestSize: 100MB
maxFileSize: 10MB
org.springframework.boot
spring-boot-starter-web
io.minio
minio
7.0.2
/**
* 文件上传
*/
@Service
public class MinioFileUpload {
/*URL*/
@Value("${os.minio.url}")
private String endpoint;
/*访问密钥*/
@Value("${os.minio.accessKey}")
private String accessKey;
/*秘钥*/
@Value("${os.minio.secretKey}")
private String secretKey;
/**
* 获取文件url
* @throws IOException
* @throws InvalidKeyException
* @throws InvalidResponseException
* @throws InsufficientDataException
* @throws NoSuchAlgorithmException
* @throws InternalException
* @throws XmlParserException
* @throws InvalidBucketNameException
* @throws ErrorResponseException
* @throws RegionConflictException
*/
public void select() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException, RegionConflictException, InvalidPortException, InvalidEndpointException {
//实例化客户端
MinioClient minioClient = new MinioClient(endpoint,accessKey,secretKey);
String image = minioClient.getObjectUrl("image", "e0ebc697-9798-4c5c-be3b-272bb175dcb4.jpg");
System.out.println("-----:"+image);
}
/**
* 上传文件流
* @param bucketName
* @param objectName 避免对象名称重复
* @param multipartFile
* @throws IOException
* @throws InvalidKeyException
* @throws InvalidResponseException
* @throws InsufficientDataException
* @throws NoSuchAlgorithmException
* @throws InternalException
* @throws XmlParserException
* @throws InvalidBucketNameException
* @throws ErrorResponseException
* @throws RegionConflictException
*/
public int uploadByStream(String bucketName, String objectName, MultipartFile multipartFile) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException, RegionConflictException {
//基础参数是否为空
if (null == bucketName
|| "".equals(bucketName)
|| null == objectName
|| "".equals(objectName)
|| null == multipartFile
|| multipartFile.isEmpty()){
return 0;
}
//获取文件流
InputStream inputStream = multipartFile.getInputStream();
try {
//实例化客户端
MinioClient minioClient = new MinioClient(endpoint,accessKey,secretKey);
//检查桶是否存在
boolean isExist = minioClient.bucketExists(bucketName);
if (isExist){
System.err.println("桶已经存在");
}else {
//新建桶
minioClient.makeBucket(bucketName);
}
PutObjectOptions putObjectOptions = new PutObjectOptions(inputStream.available(),-1);
//上传数据类型
putObjectOptions.setContentType(multipartFile.getContentType());
//上传文件
minioClient.putObject(bucketName, objectName, inputStream, putObjectOptions);
inputStream.close();
System.err.println("上传成功");
return 1;
} catch (InvalidEndpointException e) {
e.printStackTrace();
} catch (InvalidPortException e) {
e.printStackTrace();
}
return 0;
}
}
/**
* 文件管理
*/
@RestController
@RequestMapping("/os/minio/")
public class MinioFileManageController {
@Autowired
private MinioFileUpload minioFileUpload;
@PostMapping("upload")
public int upload(@RequestParam("file") MultipartFile multipartFile){
UUID uuid = UUID.randomUUID();
try {
return minioFileUpload.uploadByStream("image",uuid.toString()+".jpg",multipartFile);
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidResponseException e) {
e.printStackTrace();
} catch (InsufficientDataException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InternalException e) {
e.printStackTrace();
} catch (XmlParserException e) {
e.printStackTrace();
} catch (InvalidBucketNameException e) {
e.printStackTrace();
} catch (ErrorResponseException e) {
e.printStackTrace();
} catch (RegionConflictException e) {
e.printStackTrace();
}
return 0;
}
@PostMapping("test")
public int test(){
try {
minioFileUpload.select();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidResponseException e) {
e.printStackTrace();
} catch (InsufficientDataException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InternalException e) {
e.printStackTrace();
} catch (XmlParserException e) {
e.printStackTrace();
} catch (InvalidBucketNameException e) {
e.printStackTrace();
} catch (ErrorResponseException e) {
e.printStackTrace();
} catch (RegionConflictException e) {
e.printStackTrace();
} catch (InvalidPortException e) {
e.printStackTrace();
} catch (InvalidEndpointException e) {
e.printStackTrace();
}
return 1;
}
}