• minio使用案例(Springboot)


    一.minio文件服务搭建

     非docker环境部署(Linux部署)

    1.官网下载安装包:MinIO | Code and downloads to create high performance object storage

     上传安装包文件到目录(这个可以自由选择)

    /home/minio/

    并创建data文件夹与在data下创建minio.log文件

     

    2. 添加操作权限

    chmod +x minio

    用户名跟密码非必需执行的代码,如果不需要可以直接执行第3步.

    # 自定义设置用户名(默认minioadmin)

    export MINIO_ACCESS_KEY=admin


    # 自定义设置密码(默认minioadmin)
     

    export MINIO_SECRET_KEY=123456


    3.设置后台服务启动(9005是后台登录页面,9006是webapi接口端口,用于服务调用)

    nohup ./minio server  --console-address :9005 --address :9006 /home/minio/data  > /home/minio/data/minio.log 2>&1 &


    4. 防火墙开通端口(非必需,nginx跟防火墙端口任意选一个)
    firewall-cmd --add-port=9005/tcp --permanent
    firewall-cmd --add-port=9006/tcp --permanent
    firewall-cmd --reload

    或者nginx代理

    二、Springboot项目配置

    1.yml配置

    1. minio:
    2. url: http://bd.baidu123.xyz
    3. accessKey: 1234567890
    4. secretKey: 1234567890
    5. bucketName: myjlis

    注释:

    url:就是api接口url地址的nginx代理地址,你也可以直接填ip+9006

    accessKey与secretKey的值取自:后台管理平台(ip+9005后台平台地址)

     bucketName创建的桶名

    maven依赖

    1. <dependency>
    2. <groupId>io.minio</groupId>
    3. <artifactId>minio</artifactId>
    4. <version>7.0.2</version>
    5. </dependency>

    1. import lombok.Data;
    2. import org.springframework.boot.context.properties.ConfigurationProperties;
    3. /**
    4. * @author :jerry
    5. * @date :Created in 2022/9/6 10:32
    6. * @description:bean配置类
    7. * @version: V1.1
    8. */
    9. @Data
    10. @ConfigurationProperties(prefix = "minio")
    11. public class MinioPro {
    12. /**
    13. * 端点
    14. */
    15. private String url;
    16. /**
    17. * 用户名
    18. */
    19. private String accesskey;
    20. /**
    21. * 密码
    22. */
    23. private String secretKey;
    24. /**
    25. * 桶名称
    26. */
    27. private String bucketName;
    28. }

    1. import com.myj.common.MinioPro;
    2. import io.minio.MinioClient;
    3. import io.minio.errors.InvalidEndpointException;
    4. import io.minio.errors.InvalidPortException;
    5. import org.springframework.boot.context.properties.EnableConfigurationProperties;
    6. import org.springframework.context.annotation.Bean;
    7. import org.springframework.context.annotation.Configuration;
    8. import javax.annotation.Resource;
    9. /**
    10. * @author :jerry
    11. * @date :Created in 2022/9/6 10:31
    12. * @description:minio
    13. * @version: V1.1
    14. */
    15. @Configuration
    16. @EnableConfigurationProperties(MinioPro.class)
    17. public class MinioConfiguration {
    18. @Resource
    19. private MinioPro minioPro;
    20. /**
    21. * 初始化 MinIO 客户端
    22. */
    23. @Bean
    24. public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
    25. return new MinioClient(minioPro.getUrl(), minioPro.getAccesskey(), minioPro.getSecretKey());
    26. }
    27. }
    1. import java.io.IOException;
    2. /**
    3. * @author :jerry
    4. * @date :Created in 2022/9/6 08:55
    5. * @description:
    6. * @version: V1.1
    7. */
    8. public interface MinioService {
    9. /**上传文件*/
    10. String upload(byte[] base64Str);
    11. /**删除文件*/
    12. String delete(String fileName);
    13. /**获取文件url预览*/
    14. String getFileUrl(String objectName);
    15. /**获取文件流*/
    16. byte[] uploadFile(String objectName) throws IOException;
    17. }

    1. import com.myj.common.MinioPro;
    2. import com.myj.service.MinioService;
    3. import io.minio.MinioClient;
    4. import io.minio.PutObjectOptions;
    5. import io.minio.errors.*;
    6. import lombok.extern.slf4j.Slf4j;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.stereotype.Service;
    9. import javax.annotation.Resource;
    10. import java.io.*;
    11. import java.security.InvalidKeyException;
    12. import java.security.NoSuchAlgorithmException;
    13. import java.util.UUID;
    14. /**
    15. * @author :jerry
    16. * @date :Created in 2022/9/6 09:06
    17. * @description
    18. * @version: V1.1
    19. */
    20. @Slf4j
    21. @Service
    22. public class MinioServiceImpl implements MinioService {
    23. @Resource
    24. private MinioPro minioPro;
    25. @Autowired
    26. private MinioClient minioClient;
    27. /**base64编码的图片上传*/
    28. @Override
    29. public String upload(byte[] base64Str) {
    30. //oss是前端传来的base64对象,第一行代码将获取对象中的base64字符串
    31. try {
    32. InputStream byteArrayInputStream = new ByteArrayInputStream(base64Str);
    33. boolean bucketExists = minioClient.bucketExists(minioPro.getBucketName());
    34. if (!bucketExists) {
    35. minioClient.makeBucket(minioPro.getBucketName());
    36. }
    37. // 生成文件名称
    38. String nameSuffix = UUID.randomUUID().toString() + ".jpg";
    39. // 上传配置
    40. PutObjectOptions options = new PutObjectOptions(byteArrayInputStream.available(), PutObjectOptions.MIN_MULTIPART_SIZE);
    41. // options.setContentType("image/jpeg");
    42. options.setContentType("image/jpg");
    43. minioClient.putObject(minioPro.getBucketName(), nameSuffix, byteArrayInputStream, options);
    44. return nameSuffix;
    45. } catch (Exception e) {
    46. log.error("上传文件失败", e.getMessage());
    47. return "上传文件失败";
    48. }
    49. }
    50. // @Override
    51. // public void show(String fileName, HttpServletResponse response) {
    52. // InputStream in = null;
    53. // OutputStream out = null;
    54. // try {
    55. // MinioClient minioClient = new MinioClient(minioPro.getUrl(), minioPro.getAccesskey(), minioPro.getSecretKey());
    56. // in = minioClient.getObject(minioPro.getBucketName(), fileName);
    57. // int length = 0;
    58. // byte buf[] = new byte[1024];
    59. // out = response.getOutputStream();
    60. // response.reset();
    61. // //Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显 示附加的文件。
    62. // // Content-disposition其实可以控制用户请求所得的内容存为一个文件的时候提供一个默认的文件名,
    63. // // 文件直接在浏览器上显示或者在访问时弹出文件下载对话框。
    64. // response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
    65. // response.setCharacterEncoding("utf-8");
    66. // while ((length = in.read(buf)) > 0) {
    67. // out.write(buf, 0, length);
    68. // }
    69. // } catch (Exception ex) {
    70. // ex.printStackTrace();
    71. // } finally {
    72. // if (in != null) {
    73. // try {
    74. // in.close();
    75. // } catch (Exception e) {
    76. // throw new RuntimeException(e);
    77. // }
    78. // }
    79. // if (out != null) {
    80. // try {
    81. // out.close();
    82. // } catch (IOException e) {
    83. // e.printStackTrace();
    84. // }
    85. // }
    86. // }
    87. // }
    88. /**
    89. * 删除文件
    90. */
    91. @Override
    92. public String delete(String fileName) {
    93. try {
    94. minioClient.removeObject(minioPro.getBucketName(), fileName);
    95. } catch (Exception e) {
    96. throw new RuntimeException(e);
    97. }
    98. return "删除成功";
    99. }
    100. /**
    101. * 获取文件url预览
    102. */
    103. @Override
    104. public String getFileUrl(String objectName) {
    105. try {
    106. return minioClient.getObjectUrl(minioPro.getBucketName(), objectName);
    107. } catch (ErrorResponseException e) {
    108. throw new RuntimeException(e);
    109. } catch (InsufficientDataException e) {
    110. throw new RuntimeException(e);
    111. } catch (InternalException e) {
    112. throw new RuntimeException(e);
    113. } catch (InvalidBucketNameException e) {
    114. throw new RuntimeException(e);
    115. } catch (InvalidKeyException e) {
    116. throw new RuntimeException(e);
    117. } catch (InvalidResponseException e) {
    118. throw new RuntimeException(e);
    119. } catch (IOException e) {
    120. throw new RuntimeException(e);
    121. } catch (NoSuchAlgorithmException e) {
    122. throw new RuntimeException(e);
    123. } catch (XmlParserException e) {
    124. throw new RuntimeException(e);
    125. }
    126. }
    127. /**
    128. * 获取文件流
    129. */
    130. @Override
    131. public byte[] uploadFile(String objectName) throws IOException {
    132. InputStream uploadFile = null;
    133. try {
    134. uploadFile = minioClient.getObject(minioPro.getBucketName(), objectName);
    135. } catch (ErrorResponseException e) {
    136. throw new RuntimeException(e);
    137. } catch (InsufficientDataException e) {
    138. throw new RuntimeException(e);
    139. } catch (InternalException e) {
    140. throw new RuntimeException(e);
    141. } catch (InvalidBucketNameException e) {
    142. throw new RuntimeException(e);
    143. } catch (InvalidKeyException e) {
    144. throw new RuntimeException(e);
    145. } catch (InvalidResponseException e) {
    146. throw new RuntimeException(e);
    147. } catch (NoSuchAlgorithmException e) {
    148. throw new RuntimeException(e);
    149. } catch (XmlParserException e) {
    150. throw new RuntimeException(e);
    151. }
    152. //流转字节码数组
    153. //byte[] bytes = uploadFile.readAllBytes();\
    154. //return bytes;
    155. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    156. int nRead;
    157. byte[] data = new byte[16384];
    158. while ((nRead = uploadFile.read(data, 0, data.length)) != -1) {
    159. buffer.write(data, 0, nRead);
    160. }
    161. return buffer.toByteArray();
    162. }
    163. }

    如果需要通过域名➕桶名➕文件名方式访问需要进行如下设置

  • 相关阅读:
    【leetcode】239. 滑动窗口最大值
    dubbo漫谈(一)
    13 `bs_duixiang.tag标签`得到一个tag对象
    linux下解决 git clone每次都要输入用户名密码问题
    CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
    软件安全性测试要点有哪些?常用软件安全测试工具分享
    Python字典全解析:从基础到高级应用
    微信小程序支付及退款整体流程
    【C++】————类和对象(下)
    【小沐学Python】Python实现Web图表功能(Dash)
  • 原文地址:https://blog.csdn.net/qq_40068304/article/details/126733410