当前位置:   article > 正文

【分布式文件存储】MinIO部署及实现文件上传下载_minio文件下载

minio文件下载

目录

概述

MinIO集群部署

准备docker-compose.yml

测试启动

MinIO用户管理

Buckets管理

 创建Buckets

MinIO客户端

引入依赖 

 文件上传下载Demo

调用API碰到的问题


概述

MinIO | 高性能, Kubernetes 原生对象存储

MinIO是全球领先的对象存储先锋,目前在全世界有数百万的用户。

  • 高性能 ,在标准硬件上,读/写速度上高达183GB/秒和171GB/秒,拥有更高的吞吐量和更低的延迟

  • 可扩展性 ,为对象存储带来了简单的缩放模型,通过添加更多集群可以扩展空间

  • 简单 ,极简主义是MinIO的指导性设计原则,即可在几分钟内安装和配置

  • 与Amazon S3兼容 ,亚马逊云的 S3 API(接口协议)是在全球范围内达到共识的对象存储的协议,是全世界内大家都认可的标准

  • 数据安全 ,使用纠删码来保护数据免受硬件故障和无声数据损坏

MinIO集群部署

基于docker-compose部署入门案例

准备docker-compose.yml

  1. version: '3.7'
  2. # 所有容器通用的设置和配置
  3. x-minio-common: &minio-common
  4. image: minio/minio
  5. command: server --console-address ":9001" http://minio{1...4}/data
  6. expose:
  7. - "9000"
  8. # environment:
  9. # MINIO_ROOT_USER: minioadmin
  10. # MINIO_ROOT_PASSWORD: minioadmin
  11. healthcheck:
  12. test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
  13. interval: 30s
  14. timeout: 20s
  15. retries: 3
  16. # 启动4个docker容器运行minio服务器实例
  17. # 使用nginx反向代理9000端口,负载均衡, 你可以通过9001900290039004端口访问它们的web console
  18. services:
  19. minio1:
  20. <<: *minio-common
  21. hostname: minio1
  22. ports:
  23. - "9001:9001"
  24. volumes:
  25. - ./data/data1:/data
  26. minio2:
  27. <<: *minio-common
  28. hostname: minio2
  29. ports:
  30. - "9002:9001"
  31. volumes:
  32. - ./data/data2:/data
  33. minio3:
  34. <<: *minio-common
  35. hostname: minio3
  36. ports:
  37. - "9003:9001"
  38. volumes:
  39. - ./data/data3:/data
  40. minio4:
  41. <<: *minio-common
  42. hostname: minio4
  43. ports:
  44. - "9004:9001"
  45. volumes:
  46. - ./data/data4:/data
  47. nginx:
  48. image: nginx:1.19.2-alpine
  49. hostname: nginx
  50. volumes:
  51. - ./config/nginx.conf:/etc/nginx/nginx.conf:ro
  52. ports:
  53. - "9000:9000"
  54. depends_on:
  55. - minio1
  56. - minio2
  57. - minio3
  58. - minio4

 在当前目录下,创建config,创建nginx.conf

  1. user nginx;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 4096;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. keepalive_timeout 65;
  17. # include /etc/nginx/conf.d/*.conf;
  18. upstream minio {
  19. server minio1:9000;
  20. server minio2:9000;
  21. server minio3:9000;
  22. server minio4:9000;
  23. }
  24. server {
  25. listen 9000;
  26. listen [::]:9000;
  27. server_name localhost;
  28. # To allow special characters in headers
  29. ignore_invalid_headers off;
  30. # Allow any size file to be uploaded.
  31. # Set to a value such as 1000m; to restrict file size to a specific value
  32. client_max_body_size 0;
  33. # To disable buffering
  34. proxy_buffering off;
  35. location / {
  36. proxy_set_header Host $http_host;
  37. proxy_set_header X-Real-IP $remote_addr;
  38. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  39. proxy_set_header X-Forwarded-Proto $scheme;
  40. proxy_connect_timeout 300;
  41. # Default is HTTP/1, keepalive is only enabled in HTTP/1.1
  42. proxy_http_version 1.1;
  43. proxy_set_header Connection "";
  44. chunked_transfer_encoding off;
  45. proxy_pass http://minio;
  46. }
  47. }
  48. }

测试启动

docker-compose up -d 

访问任意节点

默认账户密码:minioadmin minioadmin

MinIO用户管理

需要创建一个用户,客户端用户可上传下载权限。

注意:长度最好是32位,否则后续调用API会报错

Buckets管理

MinIO uses buckets to organize objects. A bucket is similar to a folder or directory in a filesystem, where each bucket can hold an arbitrary number of objects.

MinIO使用bucket来组织对象。bucket类似于文件系统中的文件夹或目录,其中每个bucket可以容纳任意数量的对象。

 创建Buckets

注意桶名的命名规范,否则后续调用api创建桶会报错

 可以在控制台管理文件,上传下载预览删除等

Simple Storage Service(Amazon S3)存储桶命名要求 - AWS CloudTrail

MinIO客户端

官方接口API文档

Java Client API Reference — MinIO Object Storage for Linux

MinIO支持多种客户端,本文以java为例

引入依赖 

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

 文件上传下载Demo

  1. package pers.kw.minio.demo;
  2. import com.alibaba.fastjson.JSON;
  3. import io.minio.*;
  4. import io.minio.errors.*;
  5. import org.apache.commons.io.FileUtils;
  6. import org.apache.commons.io.IOUtils;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.security.InvalidKeyException;
  11. import java.security.NoSuchAlgorithmException;
  12. import java.util.List;
  13. public class MinIODemo {
  14. private static final String accessKey = "PPeCeQbHtFsEcGNxnlmSpbYasxELHoGy";
  15. private static final String secretKey = "GEMQDMUhQSHaIJCqmxperpESNYCJXvjJ";
  16. private static final String endpoint = "http://192.168.8.88:9000";
  17. public static void main(String[] args)
  18. throws ServerException, InsufficientDataException, ErrorResponseException,
  19. IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
  20. String bucket = "test01";
  21. String objectName = "small125750JkEMk1625374670.jpg";
  22. downloadFile(bucket, objectName);
  23. }
  24. public static void downloadFile(String bucket, String objectName)
  25. throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
  26. MinioClient minioClient =
  27. MinioClient.builder()
  28. .endpoint(endpoint)
  29. .credentials(accessKey, secretKey)
  30. .build();
  31. //todo 需要关闭流
  32. try (InputStream inputStream = minioClient.getObject(
  33. GetObjectArgs.builder()
  34. .bucket(bucket)
  35. .object(objectName)
  36. .build())) {
  37. // Read data from stream
  38. byte[] bytes = IOUtils.toByteArray(inputStream);
  39. String fileName = "22.jpg";
  40. System.out.println("下载文件:" + bytes.length);
  41. FileUtils.writeByteArrayToFile(new File("C:\\Users\\pc\\Desktop\\" + fileName), bytes);
  42. System.out.println("文件下载成功");
  43. IOUtils.closeQuietly(inputStream);
  44. }
  45. }
  46. public static void uploadFile(String bucket) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
  47. try {
  48. // Create a minioClient with the MinIO server playground, its access key and secret key.
  49. MinioClient minioClient =
  50. MinioClient.builder()
  51. .endpoint(endpoint)
  52. .credentials(accessKey, secretKey)
  53. .build();
  54. // Make 'asiatrip' bucket if not exist.
  55. boolean found =
  56. minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
  57. if (!found) {
  58. // Make a new bucket called bucket.
  59. minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
  60. } else {
  61. System.out.println("Bucket " + bucket + " already exists.");
  62. }
  63. // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
  64. // 'asiatrip'.
  65. String fileName = "C:\\Users\\pc\\Desktop\\gateway-server.rar";
  66. String objectName = "gateway-server_2023058.rar";
  67. minioClient.uploadObject(
  68. UploadObjectArgs.builder()
  69. .bucket(bucket)
  70. .object(objectName)
  71. .filename(fileName)
  72. .build());
  73. System.out.println(
  74. fileName + " is successfully uploaded as"
  75. + objectName + " to bucket " + bucket + ".");
  76. } catch (MinioException e) {
  77. System.out.println("Error occurred: " + e);
  78. System.out.println("HTTP trace: " + e.httpTrace());
  79. }
  80. }
  81. }

调用API碰到的问题

问题一:

java.lang.NoSuchMethodError: okhttp3.RequestBody.create ( BLokhttp3/Medialype:)Lokhttp3/RequestBody;

 解决: 引入如下依赖

  1. <dependency>
  2. <groupId>com.squareup.okhttp3</groupId>
  3. <artifactId>okhttp</artifactId>
  4. <version>4.9.0</version>
  5. </dependency>

问题二:

bucket name does not follow Amazon S3 standards.

命名不符合规则换个名字,最好在代码中增加正则校验

问题三:

RequestTimeTooSkewed

客户端时间与服务器时间相差过大导致

 设置服务器时间

 ntpdate time.windows.com

问题4:

InvalidAccessKeyId

秘钥信息太短

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

闽ICP备14008679号