赞
踩
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
虽然现在我们可以直接使用类似OSS、COS等存储服务,但是当我们需要将服务进行本地化部署时,大多情况都需要本地自己搭建存储服务。这里介绍个人之前使用过的Minio,推荐直接官网学习,本文只做简单介绍与使用。
提示:以下是本篇文章正文内容,下面案例可供参考
MinIO 是一个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。
wget https://dl.minio.io/server/minio/release/linux-amd64/minio
yum -y install wget
chmod 777 -R minio
./minio server /home/file
1、创建工具类MinIoUtil :
import io.minio.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class MinIoUtil { private static Logger logger = LoggerFactory.getLogger(MinIoUtil.class); private static volatile MinIoUtil instance; private static final Object lock = new Object(); //http://172.17.90.17:9000 private String endPoint; //minioadmin private String accessKey; //minioadmin private String secretKey; //biosan private String bucket; protected MinioClient client; public static MinIoUtil getInstance(){ if(instance == null){ synchronized (lock){ if(instance == null){ instance = new MinIoUtil(); } } } return instance; } private MinIoUtil(){ BaseValueService service = BeanFactory.getBean("baseValueService", BaseValueService.class); this.endPoint = service.getValue("MinIo", "endPoint"); this.accessKey = service.getValue("MinIo", "accessKey"); this.secretKey = service.getValue("MinIo", "secretKey"); this.bucket = service.getValue("MinIo", "bucket"); this.client = new MinioClient(this.endPoint,accessKey,secretKey); } public void putFile(String path, String fileName, byte[] buffer){ if (!path.endsWith("/")) path = path + "/"; putFile(path + fileName, buffer); } public void putFile(String fileFullName, byte[] buffer) { fileFullName = fileFullName.replaceAll("\\\\", "/"); ByteArrayInputStream in = new ByteArrayInputStream(buffer); try{ boolean isExit = client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build()); if(isExit){ logger.info("Bucket is exit"); }else { client.makeBucket(MakeBucketArgs.builder().bucket(bucket) .build()); } client.putObject(PutObjectArgs.builder().bucket(bucket).object(fileFullName).stream(in,in.available(),-1).build()); }catch (Exception e){ logger.info("【file upload occur exception】 e = {}",e); } } public byte[] getFile(String path, String fileName) { if (!path.endsWith("/")) path = path + "/"; return getFile(path + fileName); } public byte[] getFile(String fileFullName){ ByteArrayOutputStream out = new ByteArrayOutputStream(); while (true) { byte[] buf = new byte[8192 * 10]; try { InputStream inputStream = client.getObject(GetObjectArgs.builder().bucket(bucket).object(fileFullName).build()); int read = inputStream.read(buf); if (read < 0) break; out.write(buf, 0, read); } catch (Exception e) { logger.error(e.getMessage(), e); BaseException.throwException("读取文件失败"); break; } } return out.toByteArray(); } }
2、文件上传
MinIoUtil.getInstance().putFile(path, file.getNewFileName(), file.getBuf());
3、获取文件
File f = new File(fileName);
byte[] image = MinIoUtil.getInstance().getFile(f.getParent(), f.getName());
Minio的介绍就是上面这些内容,需要深入学习的可以直接到官网进行学习,链接文章开始已给出。很多情况下,我们可能不仅要搭建本地存储服务,而且还需要同步线上数据,这里提供大家一种方法rclone。当然rclone不止适用于Minio,方法适用于所有遵循S3协议的存储服务,我个人是将COS数据同步到本地Minio服务,链接:rclone同步S3存储服务间数据。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。