前言
开发中,一般都会单独的分离出一个文件服务器,存储对象存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等。大部分我们还在用FastDFS,我要吐槽几句它部署麻烦,也没有管理界面,所以我推荐另一个对象存储服务Minio,肯定让你眼前一亮,有种相见恨晚的感觉。
推荐理由
docker部署
将MiniIO的数据和配置文件夹挂在到宿主机上
- docker run -p 9090:9000 --name minio \
- -e MINIO_ACCESS_KEY=admin -e MINIO_SECRET_KEY=123123123 \
- -v /mydata/minio/data:/data \ -v /mydata/minio/config:/root/.minio \ -d minio/minio server /data;# 如果不创建用户名密码,默认用户名密码: minioadmin:minioadmin
访问
springboot 使用minio
1 引入maven
io.minio minio 7.0.2
2 配置 application.properties
- minio.url= http://192.168.3.189:9090
- minio.accessKey= adminminio.secretKey= 123123123
- minio.secure=false
- minio.bucketName=testminio.configDir=/home/data/
3 注入属性
- @Component
- @ConfigurationProperties(prefix = "minio")
- public class MinioConfig { // "endPoint是一个URL,域名,IPv4或者IPv6地址"
- private String url; //("accessKey类似于用户ID,用于唯一标识你的账户")
- private String accessKey; //("secretKey是你账户的密码")
- private String secretKey; //("如果是true,则用的是https而不是http,默认值是true")
- private Boolean secure; //("默认存储桶")
- private String bucketName; //("配置目录")
- private String configDir; @Bean public MinioClient getMinioClient() throws InvalidEndpointException, InvalidPortException { MinioClient minioClient = new MinioClient(url, accessKey, secretKey,secure); return minioClient;
- } public String getBucketName() { return bucketName;
- } public String getConfigDir() { return configDir;
- } public String getUrl() { return url;
- } public void setUrl(String url) { this.url = url; } public String getAccessKey() { return accessKey;
- } public void setAccessKey(String accessKey) { this.accessKey = accessKey; } public String getSecretKey() { return secretKey;
- } public void setSecretKey(String secretKey) { this.secretKey = secretKey; } public Boolean getSecure() { return secure;
- } public void setSecure(Boolean secure) { this.secure = secure; } public void setBucketName(String bucketName) { this.bucketName = bucketName; } public void setConfigDir(String configDir) { this.configDir = configDir; }}
4 创建工具类
- @Component
- public class MinioUtil {
- @Autowired
- private MinioClient minioClient;
- /**
- * 上传文件
- */
- public void uploadFile(InputStream inputStream, String objectName) {
- String buckName = "test";
- try {
- if(!minioClient.bucketExists(buckName)) {
- minioClient.makeBucket(buckName); } minioClient.putObject(buckName, objectName, inputStream, inputStream.available(), "image/jpeg");
- } catch (Exception e) {
- e.printStackTrace(); } } public void downloadFile(String bucketName, String fileName, String originalName, HttpServletResponse response) {
- try {
- InputStream file = minioClient.getObject(bucketName, fileName); String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
- response.setHeader("Content-Disposition", "attachment;filename=" + filename);
- ServletOutputStream servletOutputStream = response.getOutputStream(); int len; byte[] buffer = new byte[1024];
- while ((len = file.read(buffer)) > 0) {
- servletOutputStream.write(buffer, 0, len);
- } servletOutputStream.flush(); file.close(); servletOutputStream.close(); } catch (ErrorResponseException e) {
- e.printStackTrace(); } catch (Exception e) {
- e.printStackTrace(); } }}
5 测试
- @RestController
- public class GreetingsController {
- @Autowired
- MinioUtil minioUtil; @RequestMapping(value = "/{name}", method = RequestMethod.GET)
- @ResponseStatus(HttpStatus.OK)
- public String greetingText(@PathVariable String name,HttpServletResponse response) throws FileNotFoundException {
- minioUtil.uploadFile(new FileInputStream(new File("C:\\Users\\ctyc\\Desktop\\1.jpg")), "test1.jpg");
- minioUtil.downloadFile("test", "test1.jpg", "t1.jpg", response);
- return "Hello " + name + "!";
- }}
本文题目:开发好物推荐7之对象存储服务Minio
转载源于:http://www.shufengxianlan.com/qtweb/news9/463609.html
成都网站建设公司_创新互联,为您提供标签优化、面包屑导航、搜索引擎优化、外贸建站、python、网站排名
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联