当前位置:   article > 正文

MINIO部署

minio部署

一、MINIO单机部署

Linux下载地址:https://min.io/download#/linux

  1. 服务器创建新的文件夹;
    mkdir -p /home/minio/data
  2. 上传下载好的minio文件,到指定的目录下/home/minio/data

  3. 执行命令;

    1. chmod +x minio //给予权限
    2. export MINIO_ACCESS_KEY=minioadmin //创建账号
    3. export MINIO_SECRET_KEY=minioadmin //创建密码
  4. 启动minio;
    nohup ./minio server  --address :9000 --console-address :9001 /home/minio/data > /home/minio/data/minio.log &

    自定义端口方式:自定义启动端口号以及控制台端口号,不设置则控制台会自动配置其他端口号,非常不方便

  5. 查看状态;

    ps -  ef|grep   minio
  6. 至此安装启动完成;
  7. 访问:127.0.0.1:9001/(注意:端口是控制台的端口);

  8. 输入步骤3设置的账号密码,进入到了管理页面;
  9. 设置桶,创建桶,在服务器中会生成一个和桶名称相同的文件夹;

  10. 设置桶规则;修改权限public,设置规则为readwrite;
  11. 测试上传文件到桶里边;
  12. 上传完文件之后在服务器的桶中会生成一个文件

二、JAVA集成

1) pom.xml导入相关依赖;

  1. <dependency>
  2. <groupId>io.minio</groupId>
  3. <artifactId>minio</artifactId>
  4. <version>8.3.5</version>
  5. </dependency>
  6. <!--OK Http-->
  7. <dependency>
  8. <groupId>com.squareup.okhttp3</groupId>
  9. <artifactId>okhttp</artifactId>
  10. <version>4.9.1</version>
  11. </dependency>

2)配置文件中增加配置信息;

  1. minio:
  2. endpoint: http://127.0.0.1:9000
  3. accesskey: minioadmin #用户名
  4. secretKey: minioadmin #密码
  5. bucketName: files #桶名称

3)配置创建实体类;

  1. @Data
  2. @Component
  3. @ConfigurationProperties(prefix = "minio")
  4. public class MinioProp {
  5. /**
  6. * 连接url
  7. */
  8. private String endpoint;
  9. /**
  10. * 用户名
  11. */
  12. private String accesskey;
  13. /**
  14. * 密码
  15. */
  16. private String secretKey;
  17. /**
  18. * 桶名称
  19. */
  20. private String bucketName;
  21. }

4)创建minioconfig;

  1. @Configuration
  2. @EnableConfigurationProperties(MinioProp.class)
  3. public class MinioConfig {
  4. @Autowired
  5. private MinioProp minioProp;
  6. @Bean
  7. public MinioClient minioClient(){
  8. MinioClient minioClient = MinioClient.builder().endpoint(minioProp.getEndpoint()).
  9. credentials(minioProp.getAccesskey(), minioProp.getSecretKey()).region("china").build();
  10. return minioClient;
  11. }
  12. }

5)编写minioUtil类;

上传:

  1. public JSONObject uploadFile(MultipartFile file) throws Exception {
  2. JSONObject res = new JSONObject();
  3. res.put("code", 0);
  4. // 判断上传文件是否为空
  5. if (null == file || 0 == file.getSize()) {
  6. res.put("msg", "上传文件不能为空");
  7. return res;
  8. }
  9. InputStream is=null;
  10. try {
  11. // 判断存储桶是否存在
  12. createBucket(minioProp.getBucketName());
  13. // 文件名
  14. String originalFilename = file.getOriginalFilename();
  15. // 新的文件名 = 存储桶名称_时间戳.后缀名
  16. String fileName = minioProp.getBucketName() + "_" + IdUtil.getId() + originalFilename.substring(originalFilename.lastIndexOf("."));
  17. // 开始上传
  18. is=file.getInputStream();
  19. PutObjectArgs putObjectArgs = PutObjectArgs.builder()
  20. .bucket(minioProp.getBucketName())
  21. .object(fileName)
  22. .contentType(file.getContentType())
  23. .stream(is, is.available(), -1)
  24. .build();
  25. minioClient.putObject(putObjectArgs);
  26. res.put("code", 1);
  27. res.put("msg", minioProp.getBucketName() + "/" + fileName);
  28. res.put("bucket", minioProp.getBucketName());
  29. res.put("fileName", fileName);
  30. return res;
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. log.error("上传文件失败:{}", e.getMessage());
  34. }finally {
  35. is.close();
  36. }
  37. res.put("msg", "上传失败");
  38. return res;
  39. }

下载:

  1. public void downLoad(String fileName,String realFileName, HttpServletResponse response, HttpServletRequest request) {
  2. InputStream is=null;
  3. OutputStream os =null;
  4. try {
  5. is=getObjectInputStream(fileName,minioProp.getBucketName());
  6. if(is!=null){
  7. byte buf[] = new byte[1024];
  8. int length = 0;
  9. String codedfilename = "";
  10. String agent = request.getHeader("USER-AGENT");
  11. System.out.println("agent:" + agent);
  12. if ((null != agent && -1 != agent.indexOf("MSIE")) || (null != agent && -1 != agent.indexOf("Trident"))) {
  13. String name = URLEncoder.encode(realFileName, "UTF8");
  14. codedfilename = name;
  15. } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
  16. codedfilename = new String(realFileName.getBytes("UTF-8"), "iso-8859-1");
  17. } else {
  18. codedfilename = new String(realFileName.getBytes("UTF-8"), "iso-8859-1");
  19. }
  20. response.reset();
  21. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName.substring(realFileName.lastIndexOf("/") + 1), "UTF-8"));
  22. response.setContentType("application/octet-stream");
  23. response.setCharacterEncoding("UTF-8");
  24. os = response.getOutputStream();
  25. // 输出文件
  26. while ((length = is.read(buf)) > 0) {
  27. os.write(buf, 0, length);
  28. }
  29. // 关闭输出流
  30. os.close();
  31. }else{
  32. log.error("下载失败");
  33. }
  34. }catch (Exception e){
  35. e.printStackTrace();
  36. log.error("错误:"+e.getMessage());
  37. }finally {
  38. if(is!=null){
  39. try {
  40. is.close();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. if(os!=null){
  46. try {
  47. os.close();
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }
  53. }

删除:

  1. public void deleteObject(String objectName) {
  2. try {
  3. RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
  4. .bucket(minioProp.getBucketName())
  5. .object(objectName)
  6. .build();
  7. minioClient.removeObject(removeObjectArgs);
  8. }catch (Exception e){
  9. log.error("错误:"+e.getMessage());
  10. }
  11. }

查看:一个url

例如127.0.0.1:9000/files/files_1660635187005.png;

格式:ip:端口/桶/文件在桶里的真实名字;

完整的util类:

  1. import com.alibaba.fastjson2.JSONObject;
  2. import com.crcc.statistics.common.entity.MinioProp;
  3. import io.minio.*;
  4. import lombok.SneakyThrows;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import java.net.URLEncoder;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. /**
  18. * @Author smallhou
  19. * @Date 2022-08-15 14:31
  20. * @Version V1.0
  21. */
  22. @Slf4j
  23. @Component
  24. public class MinioUtils {
  25. @Autowired
  26. private MinioClient minioClient;
  27. @Autowired
  28. private MinioProp minioProp;
  29. /**
  30. * @Author smallhou
  31. * @Description //TODO 判断桶存在不存在,不存在创建桶
  32. * @Date 10:49 2022-08-16
  33. * @Param [bucketName]
  34. * @return void
  35. **/
  36. @SneakyThrows
  37. public void createBucket(String bucketName) {
  38. if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
  39. minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
  40. }
  41. }
  42. @SneakyThrows
  43. public InputStream getObjectInputStream(String objectName,String bucketName){
  44. GetObjectArgs getObjectArgs = GetObjectArgs.builder()
  45. .bucket(bucketName)
  46. .object(objectName)
  47. .build();
  48. return minioClient.getObject(getObjectArgs);
  49. }
  50. public JSONObject uploadFile(MultipartFile file) throws Exception {
  51. JSONObject res = new JSONObject();
  52. res.put("code", 0);
  53. // 判断上传文件是否为空
  54. if (null == file || 0 == file.getSize()) {
  55. res.put("msg", "上传文件不能为空");
  56. return res;
  57. }
  58. InputStream is=null;
  59. try {
  60. // 判断存储桶是否存在
  61. createBucket(minioProp.getBucketName());
  62. // 文件名
  63. String originalFilename = file.getOriginalFilename();
  64. // 新的文件名 = 存储桶名称_时间戳.后缀名
  65. String fileName = minioProp.getBucketName() + "_" + IdUtil.getId() + originalFilename.substring(originalFilename.lastIndexOf("."));
  66. // 开始上传
  67. is=file.getInputStream();
  68. PutObjectArgs putObjectArgs = PutObjectArgs.builder()
  69. .bucket(minioProp.getBucketName())
  70. .object(fileName)
  71. .contentType(file.getContentType())
  72. .stream(is, is.available(), -1)
  73. .build();
  74. minioClient.putObject(putObjectArgs);
  75. res.put("code", 1);
  76. res.put("msg", minioProp.getBucketName() + "/" + fileName);
  77. res.put("bucket", minioProp.getBucketName());
  78. res.put("fileName", fileName);
  79. return res;
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. log.error("上传文件失败:{}", e.getMessage());
  83. }finally {
  84. is.close();
  85. }
  86. res.put("msg", "上传失败");
  87. return res;
  88. }
  89. public void downLoad(String fileName,String realFileName, HttpServletResponse response, HttpServletRequest request) {
  90. InputStream is=null;
  91. OutputStream os =null;
  92. try {
  93. is=getObjectInputStream(fileName,minioProp.getBucketName());
  94. if(is!=null){
  95. byte buf[] = new byte[1024];
  96. int length = 0;
  97. String codedfilename = "";
  98. String agent = request.getHeader("USER-AGENT");
  99. System.out.println("agent:" + agent);
  100. if ((null != agent && -1 != agent.indexOf("MSIE")) || (null != agent && -1 != agent.indexOf("Trident"))) {
  101. String name = URLEncoder.encode(realFileName, "UTF8");
  102. codedfilename = name;
  103. } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
  104. codedfilename = new String(realFileName.getBytes("UTF-8"), "iso-8859-1");
  105. } else {
  106. codedfilename = new String(realFileName.getBytes("UTF-8"), "iso-8859-1");
  107. }
  108. response.reset();
  109. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName.substring(realFileName.lastIndexOf("/") + 1), "UTF-8"));
  110. response.setContentType("application/octet-stream");
  111. response.setCharacterEncoding("UTF-8");
  112. os = response.getOutputStream();
  113. // 输出文件
  114. while ((length = is.read(buf)) > 0) {
  115. os.write(buf, 0, length);
  116. }
  117. // 关闭输出流
  118. os.close();
  119. }else{
  120. log.error("下载失败");
  121. }
  122. }catch (Exception e){
  123. e.printStackTrace();
  124. log.error("错误:"+e.getMessage());
  125. }finally {
  126. if(is!=null){
  127. try {
  128. is.close();
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132. }
  133. if(os!=null){
  134. try {
  135. os.close();
  136. } catch (IOException e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. }
  141. }
  142. public void deleteObject(String objectName) {
  143. try {
  144. RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
  145. .bucket(minioProp.getBucketName())
  146. .object(objectName)
  147. .build();
  148. minioClient.removeObject(removeObjectArgs);
  149. }catch (Exception e){
  150. log.error("错误:"+e.getMessage());
  151. }
  152. }
  153. }

三、总结:

部署相对简单,JAVA集成快,上手更容易;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/523843
推荐阅读
相关标签
  

闽ICP备14008679号