MinIO部署及示例

news/2024/10/15 14:13:12

docker部署

docker run \-p 9000:9000 \-p 9001:9001 \-d \--name minio \-v /Users/ivan/code/black/dockerData/minio:/data \-e "MINIO_ROOT_USER=ROOT" \-e "MINIO_ROOT_PASSWORD=MINIO123" \quay.io/minio/minio server /data --console-address ":9001"

管理后台 localhost:9001 用户名ROOT 密码MINIO123
上传以后的文件,通过localhost:9000/xxx来访问

sprinboo3集成

依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>io.minio</groupId><artifactId>minio</artifactId></dependency>
</dependencies>

Springboot3封装starter

@Data
@Configuration
@EnableConfigurationProperties({MinIOConfigProperties.class})
//当引入FileStorageService接口时
@ConditionalOnClass(FileStorageService.class)
public class MinIOConfig {@Autowiredprivate MinIOConfigProperties minIOConfigProperties;@Beanpublic MinioClient buildMinioClient(){return MinioClient.builder().credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey()).endpoint(minIOConfigProperties.getEndpoint()).build();}
}
package com.shenzhen.dai.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;import java.io.Serializable;@Data
@Configuration
@ConfigurationProperties(prefix = "minio")  // 文件上传 配置前缀file.oss
public class MinIOConfigProperties implements Serializable {private String accessKey;private String secretKey;private String bucket;private String endpoint;private String readPath;
}
package com.shenzhen.dai.service;import java.io.InputStream;/*** @author itheima*/
public interface FileStorageService {/*** 上传自定义格式文件** @param filePath    文件路径* @param contentType 文件类型* @param filename    文件名* @param inputStream 文件流* @return 文件全路径*/String uploadFile(String filePath, String filename, String contentType, InputStream inputStream);/*** 上传图片文件** @param prefix      文件前缀* @param filename    文件名* @param inputStream 文件流* @return 文件全路径*/String uploadImgFile(String prefix, String filename, InputStream inputStream);/*** 上传html文件** @param prefix      文件前缀* @param filename    文件名* @param inputStream 文件流* @return 文件全路径*/String uploadHtmlFile(String prefix, String filename, InputStream inputStream);/*** 删除文件** @param pathUrl 文件全路径*/void delete(String pathUrl);/*** 下载文件** @param pathUrl 文件全路径* @return*/byte[] downLoadFile(String pathUrl);}
package com.shenzhen.dai.service.impl;import com.shenzhen.dai.config.MinIOConfig;
import com.shenzhen.dai.config.MinIOConfigProperties;
import com.shenzhen.dai.service.FileStorageService;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;@Slf4j
@EnableConfigurationProperties(MinIOConfigProperties.class)
@Import(MinIOConfig.class)
@Service
public class MinIOFileStorageService implements FileStorageService {@Autowiredprivate MinioClient minioClient;@Autowiredprivate MinIOConfigProperties minIOConfigProperties;private final static String separator = "/";/*** @param dirPath* @param filename yyyy/mm/dd/file.jpg* @return*/public String builderFilePath(String dirPath, String filename) {StringBuilder stringBuilder = new StringBuilder(50);if (!StringUtils.isEmpty(dirPath)) {stringBuilder.append(dirPath).append(separator);}SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");String todayStr = sdf.format(new Date());stringBuilder.append(todayStr).append(separator);stringBuilder.append(filename);return stringBuilder.toString();}@Overridepublic String uploadFile(String filePath, String filename, String contentType, InputStream inputStream) {try {PutObjectArgs putObjectArgs = PutObjectArgs.builder().object(filePath).contentType(contentType).bucket(minIOConfigProperties.getBucket()).stream(inputStream, inputStream.available(), -1).build();minioClient.putObject(putObjectArgs);StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());urlPath.append(separator).append(minIOConfigProperties.getReadPath());urlPath.append(separator).append(filePath);return urlPath.toString();} catch (Exception ex) {log.error("minio put file error.", ex);throw new RuntimeException(filename + "上传失败");}}/*** 上传图片文件** @param prefix      文件前缀* @param filename    文件名* @param inputStream 文件流* @return 文件全路径*/@Overridepublic String uploadImgFile(String prefix, String filename, InputStream inputStream) {String filePath = builderFilePath(prefix, filename);try {PutObjectArgs putObjectArgs = PutObjectArgs.builder().object(filePath).contentType("image/jpg").bucket(minIOConfigProperties.getBucket()).stream(inputStream, inputStream.available(), -1).build();minioClient.putObject(putObjectArgs);StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());urlPath.append(separator + minIOConfigProperties.getBucket());urlPath.append(separator);urlPath.append(filePath);return urlPath.toString();} catch (Exception ex) {log.error("minio put file error.", ex);throw new RuntimeException("上传文件失败");}}/*** 上传html文件** @param prefix      文件前缀* @param filename    文件名* @param inputStream 文件流* @return 文件全路径*/@Overridepublic String uploadHtmlFile(String prefix, String filename, InputStream inputStream) {String filePath = builderFilePath(prefix, filename);try {PutObjectArgs putObjectArgs = PutObjectArgs.builder().object(filePath).contentType("text/html").bucket(minIOConfigProperties.getBucket()).stream(inputStream, inputStream.available(), -1).build();minioClient.putObject(putObjectArgs);StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());urlPath.append(separator + minIOConfigProperties.getBucket());urlPath.append(separator);urlPath.append(filePath);return urlPath.toString();} catch (Exception ex) {log.error("minio put file error.", ex);ex.printStackTrace();throw new RuntimeException("上传文件失败");}}/*** 删除文件** @param pathUrl 文件全路径*/@Overridepublic void delete(String pathUrl) {String key = pathUrl.replace(minIOConfigProperties.getEndpoint() + "/", "");int index = key.indexOf(separator);String bucket = key.substring(0, index);String filePath = key.substring(index + 1);// 删除ObjectsRemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucket).object(filePath).build();try {minioClient.removeObject(removeObjectArgs);} catch (Exception e) {log.error("minio remove file error.  pathUrl:{}", pathUrl);e.printStackTrace();}}/*** 下载文件** @param pathUrl 文件全路径* @return 文件流*/@Overridepublic byte[] downLoadFile(String pathUrl) {String key = pathUrl.replace(minIOConfigProperties.getEndpoint() + "/", "");int index = key.indexOf(separator);String bucket = key.substring(0, index);String filePath = key.substring(index + 1);InputStream inputStream = null;try {inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(minIOConfigProperties.getBucket()).object(filePath).build());} catch (Exception e) {log.error("minio down file error.  pathUrl:{}", pathUrl);e.printStackTrace();}ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();byte[] buff = new byte[100];int rc = 0;while (true) {try {if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;} catch (IOException e) {e.printStackTrace();}byteArrayOutputStream.write(buff, 0, rc);}return byteArrayOutputStream.toByteArray();}
}

resources下创建META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.shenzhen.dai.service.impl.MinIOFileStorageService

其他项目使用

引入依赖

<dependency><groupId>com.shenzhen.dai</groupId><artifactId>black-news-starter-file</artifactId><version>1.0-SNAPSHOT</version>
</dependency>

增加配置

minio:accessKey: Kjcacew7Jq5x5vk0UfEYsecretKey: UHrHzJADSIqZcj4eATNFMIkl2yA9grXsuXm3j8DAbucket: blackendpoint: http://localhost:9000readPath: http://localhost:9000

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/71829.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

FingersGestures 简介

FingersGestures 简介 FingersGestures为官方推荐手势插件,主要用于替代EasyTouch,EasyTouch很多年没有更新了,并且已经在官方资产商店下架,FingersGestures插件在官方商店具有很高的热度和持续的版本迭代,以及提供了二十多个实例进行参考,后续考虑项目中都使用此插件。 …

TextMeshPro

简介支持图文混排和矢量字体图文混排使用需要开启RichText 需要挂载SpriteAsset(不挂载的情况下,使用默认的spriteAsset) 使用下标来显示没有设置spriteAsset的情况设置了spriteAsset的情况

TowardsDataScience-博客中文翻译-2020-五十五-

TowardsDataScience 博客中文翻译 2020(五十五)原文:TowardsDataScience Blog 协议:CC BY-NC-SA 4.0GPT-3,OpenAI 的革命原文:https://towardsdatascience.com/gpt-3-openais-revolution-f549bf3d4b25?source=collection_archive---------43-----------------------来源…

约80%开发效率提升,原生鸿蒙政务、文旅行业样板间专区上线

10月8日,华为官方正式宣布,其最新操作系统HarmonyOS NEXT于当日10:08正式开启公测。 为有效助力开发者加速行业应用开发,华为开发者联盟生态市场(简称生态市场)近日上线了原生鸿蒙政务行业、文旅行业“样板间”专区。政务和文旅行业作为数字化转型的重要领域,对数智应用的…

TowardsDataScience-博客中文翻译-2020-四十五-

TowardsDataScience 博客中文翻译 2020(四十五)原文:TowardsDataScience Blog 协议:CC BY-NC-SA 4.0基于 Avro 模式的类固醇弹性研究原文:https://towardsdatascience.com/elasticsearch-on-steroids-with-avro-schemas-3bfc483e3b30?source=collection_archive---------…

一站实现高效开发,鸿蒙生态伙伴模板组件专区全新上线

10月8日,华为官方正式宣布,其最新操作系统HarmonyOS NEXT于当日10:08正式开启公测。 鸿蒙生态的发展离不开丰富的鸿蒙原生应用,为了有效提升开发者开发效率,降低开发成本,华为开发者联盟生态市场近日上线了“鸿蒙生态伙伴模板&组件专区”,汇聚官方及三方生态伙伴针对政务…

TowardsDataScience-博客中文翻译-2020-三-

TowardsDataScience 博客中文翻译 2020(三)原文:TowardsDataScience Blog 协议:CC BY-NC-SA 4.0做出更好决策的 3 个要素原文:https://towardsdatascience.com/3-essentials-to-make-better-decisions-9b2cdc60365e?source=collection_archive---------47---------------…

git怎么查看每次的提交记录控制台输出日志乱码

今天来和各位小伙伴分享一下怎么查看git的每次提交(commit)的记录: 1.打开idea ——2.点击做下加的git ——3.点击log ——4.选择自己的分支 ——5.查看每次提交的代码 二、控制台log4j输出的日志乱码: 解决办法: 1、将图中几处地方变为urf-8 2、