Minio相关知识点和遇到的问题

最近公司有文件存储相关的业务,准备使用minio来存储。

记录一下minio相关的知识点和遇到的问题

知识点:

一、Springboot整合minio

  1. 引入依赖
     <!-- minio依赖 -->
            <dependency>
                <groupId>io.minio</groupId>
                <artifactId>minio</artifactId>
                <version>8.4.5</version>
            </dependency>
  2. 在application.yml中添加相关配置
    minio:
      # 服务默认的端口号为9000,可视化页面的端口号为9001,不要写错了
      url: http://101.35.89.27:9000
      accessKey: minio
      secretKey: Carbonease@2022
  3. 编写配置文件类,并创建“MinioClient”对象,将对象交给Spring管理
    @Configuration
    @ConfigurationProperties(prefix = "minio")
    public class MinioConfig {
    
        /**
         * 服务地址
         */
        private String url;
    
        /**
         * 用户名
         */
        private String accessKey;
    
        /**
         * 密码
         */
        private String secretKey;
    
        /**
         * 桶名称
         */
        private String bucketName;
    
    
        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 String getBucketName() {
            return bucketName;
        }
    
        public void setBucketName(String bucketName) {
            this.bucketName = bucketName;
        }
    
        @Bean
        public MinioClient getMinioClient(){
            return MinioClient.builder().endpoint(url).credentials(accessKey,secretKey).build();
        }
    }
  4. 在代码中注入“MinioClient”即可

minio配置参数

minio策略配置参数详解_任错错的博客-CSDN博客_minioclient.setbucketpolicy

Amazon S3 中的策略和权限 - Amazon Simple Storage Service

遇到的问题:

一、访问Client报错

场景和现象:

调取Minio的接口时报了下面的错误


io.minio.errors.InvalidResponseException: Non-XML response from server. Response code: 403, Content-Type: text/xml; charset=utf-8, body: <?xml version="1.0" encoding="UTF-8"?>
<Error>
  <Code>AccessDenied</Code>
  <Message>S3 API Request made to Console port. S3 Requests should be sent to API port.</Message>
  <RequestId>0</RequestId>
</Error>

原因:

原因是配置minio的端口号错误。9001为可视化页面的地址,默认的地址应为9000

minio:
  url: http://localhsot:9001
  accessKey: minio
  secretKey: minio

二、通过minio服务地址,直接访问文件失败

 原因:

桶的策略为private,不允许直接通过地址访问,需要后台去获取文件流,然后返回给前端

@Override
    public void browseFiles(HttpServletResponse response, String bucketName, String[] fileNames) throws Exception{
        for (String fileName : fileNames) {
            ServletOutputStream outputStream = null;
            try {
                StatObjectResponse file = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(fileName).build());
                byte[] buf = new byte[1024];
                int length = 0;
                response.reset();
                response.setContentType(file.contentType());
                response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                response.setCharacterEncoding("UTF-8");
                GetObjectResponse getObjectResponse = minioClient.getObject(GetObjectArgs.builder().bucket("drlee").object(fileName).build());
                outputStream = response.getOutputStream();
                while ((length = getObjectResponse.read(buf)) > 0) {
                    outputStream.write(buf, 0, length);
                }
                outputStream.close();
            }catch(Exception e){
                e.printStackTrace();
            }finally {
                if(outputStream != null){
                    outputStream.close();
                }
            }
        }
    }

response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(fileName, "UTF-8"));

inline为直接显示、attachment为下载

三、上传文件时报文件大小过大

minio本身没有限制文件的大小,这个问题是spring报的,默认情况下上传的文件大小不允许超过1M,只需要添加两行配置即可

spring:  
    servlet:
        multipart:
            #文件大小
          max-file-size: 10MB
            #请求大小
          max-request-size: 15MB

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值