每天分享(20220415)
今天分享MinIO文件对象存储服务器docker单机版安装及springboot整合使用。
MinIO简介
MinIO是一款高性能的对象存储容器,适合存储大量的非结构化数据,例如:图片、视频、Word等文件。此外,它还自带非常方便的可视化界面。
MinIOdocker安装
拉取镜像docker pull minio/minio创建并运行容器
docker run -it --name minio -p 9000:9000 -p 9001:9001 -d \-v /usr/local/docker/minio/data:/data \-v /usr/local/docker/minio/config:/root/.minio \-e MINIO_ROOT_USER=admin \-e MINIO_ROOT_PASSWORD=admin123 \minio/minio server /data --console-address :9001
查看容器日志
随后,在浏览器打开9000端口,分别输入上面的账号密码(账号:admin,密码:admin123),登录即可
创建Bucket
登录成功后,进入MinIO后台页面。
springboot集成MinIO
使用maven引用minio依赖
dependency groupIdio.minio/groupId artifactIdminio/artifactId version8.3.0/version exclusions exclusion groupIdcom.squareup.okhttp3/groupId artifactIdokhttp/artifactId /exclusion /exclusions /dependency dependency groupIdcom.squareup.okhttp3/groupId artifactIdokhttp/artifactId version4.8.1/version /dependency
MinioUtil 创建操作类
建一个 MinioUtil 操作类,并实例化 MinioClient 对象
public class MinioUtil { private static MinioClient client; static { try { client = MinioClient.builder() .endpoint(new URL()) .credentials(admin, admin123) .build(); } catch (MalformedURLException e) { e.printStackTrace(); } }}
MinioUtil 实现文件上传
/** * 上传本地文件 */ public static ObjectWriteResponse upload(String bucket, String localFileName, String remoteFileName) throws Exception{ File file = new File(localFileName); FileInputStream fileInputStream = new FileInputStream(file); return client.putObject(PutObjectArgs.builder() .stream(fileInputStream, file.length(), PutObjectArgs.MIN_MULTIPART_SIZE) .object(remoteFileName) .bucket(bucket) .build()); } /** * 上传MultipartFile */ public static ObjectWriteResponse upload(String bucket, MultipartFile file, String remoteFileName) throws Exception { return client.putObject(PutObjectArgs.builder() .bucket(bucket) .stream(file.getInputStream(), file.getSize(), PutObjectArgs.MIN_MULTIPART_SIZE) .object(remoteFileName) .build()); }
MinIO实现文件下载
修改 MinioUtil,增加 2 个下载方法,分别是下载到本地和浏览器打开下载
/** * 下载文件到本地 */ public static void downLocal(String bucket, String remoteFileName, String localFileName) throws Exception { client.downloadObject(DownloadObjectArgs.builder() .bucket(bucket) .object(remoteFileName) .filename(localFileName) .build()); } /** * 下载文件写入HttpServletResponse */ public static void downResponse(String bucket, String remoteFileName, HttpServletResponse response) throws Exception { GetObjectResponse object = client.getObject(GetObjectArgs.builder() .bucket(bucket) .object(remoteFileName) .build()); response.setHeader(Content-Disposition, attachment;filename= remoteFileName.substring(remoteFileName.lastIndexOf(/) 1)); response.setContentType(application/force-download); response.setCharacterEncoding(UTF-8); IOUtils.copy(object, response.getOutputStream()); }
创建 MinioController
@RestController@RequestMapping(/minio)public class MinioController { /** * 上传本地文件 */ @GetMapping(/uploadLocalFile) public String uploadLocalFile() throws Exception{ ObjectWriteResponse resp = MinioUtil.upload(test01, E:\\测试文件.txt, getDatePath() test.txt); return 上传成功; } /** * 上传MultipartFile */ @PostMapping(/uploadMultipartFile) public String uploadMultipartFile(MultipartFile file) throws Exception { ObjectWriteResponse resp = MinioUtil.upload(test01, file, getDatePath() file.getOriginalFilename()); return 上传成功; } /** * 下载文件到本地 */ @PostMapping(/downLocal) public String downLocal() throws Exception { MinioUtil.downLocal(test01, /2022/4/15/test.txt, test.txt); return 下载成功; } /** * 下载文件写入HttpServletResponse */ @GetMapping(/downResponse) public void downResponse(HttpServletResponse response) throws Exception { MinioUtil.downResponse(test01, /2022/4/15/test.txt, response); } private String getDatePath(){ LocalDateTime now = LocalDateTime.now(); return String.format(/%s/%s/%s/, now.getYear(), now.getMonthValue(), now.getDayOfMonth()); }}
执行完毕后,在 test01 这个 bucket 底下,会创建 /2022/04/15/ 的目录,并且将文件上传到该目录下
打开 Bucket,点击 Manage 进行设置管理
点击这里的 Private,把它改成 Public