目录
💖 简介
MinIO是一个基于Apache License V2.0开源协议的轻量级的对象存储服务。
适合存储大容量非结构化的数据,如图片、视频、日志文件、备份数据和容器/虚拟机镜像等。
💖 基于docker环境的minio单机部署
在联网的前提下
🏆 docker环境搭建
# 下载安装docker所需依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
# 设置镜像源(阿里)
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 查看docker源中的docker版本
yum list docker-ce --showduplicates | sort -r
# 默认最新版本安装
yum install -y docker-ce docker-ce-cli containerd.io
# 验证安装情况
docker version
# 重新加载命令
systemctl daemon-reload
# 启动docker
systemctl start docker
🏆 minio 环境搭建
# 下载镜像
docker pull minio/minio
# 启动容器
docker run -p 9000:9000 -p 9090:9090 --name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=minio" \
-e "MINIO_SECRET_KEY=minio123" \
-v /opt/docker_minio/data:/data \
-v /opt/docker_minio/config:/root/.minio \
minio/minio server /data \
--console-address ":9000" --address ":9090"
# 访问测试
http://192.168.xxx.8:9000
# 输入用户名、密码 进行登录
用户名:minio
密码:minio123
访问测试结果如下,表示环境搭建成功:
输入用户名、密码 进行登录:
💖 基于java的文件上传和下载
🏆 文件上传
创建springboot项目,目录结构如下:
pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>minio-client</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.3.0</version>
</dependency>
<dependency>
<groupId>me.tongfei</groupId>
<artifactId>progressbar</artifactId>
<version>0.5.3</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.8.1</version>
</dependency>
</dependencies>
</project>
FileUploader文件:
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* @author NSY
* @date 2022/7/13 - 15:17
*/
public class FileUploader {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
// 创建一个minio客户端
MinioClient minioClient =
MinioClient.builder()
.endpoint("http://192.168.159.8:9090")
.credentials("admin", "admin123456")
.build();
// 如果不存在,创建一个桶
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket("test").build());
if (!found) {
// 创建一个桶
minioClient.makeBucket(MakeBucketArgs.builder().bucket("test").build());
} else {
System.out.println("桶 'test' 早已存在.");
}
//上传文件到桶内
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket("test")
.object("test.txt")
.filename("D:/test.txt")
.build());
System.out.println(
"'D:/test.txt' 文件上传成功!");
} catch (MinioException e) {
System.out.println("出现错误: " + e);
System.out.println("HTTP跟踪: " + e.httpTrace());
}
}
}
测试结果:
test,txt文件已上传成功!
💖 问题
🏆 java连接服务端的用户名、密码和端口问题
访问Console 控制台的用户名、密码和端口与java API操作Minio时用的用户名、密码和端口是不一致的。
docker启动容器的命令中,已经含有访问Console 控制台的用户名、密码和端口,如上所示:
用户名:minio
密 码:minio123
端 口:9000
但是,JAVA API操作Minio的用户名和密码不是这个,需要在控制台进行创建,步骤如下:
创建accessKey和secretKey,
Create之后,弹出如下:
点击,Download for import,进行保存
下载的文件如下:
文件中的9090和admin、admin123456就是JAVA API用来访问Minio的端口、accessKey、secretKey的值,如前面代码所示.
如果用Console的端口、用户、密码访问,会出现如下错误:
Error occurred: 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>
HTTP trace: ---------START-HTTP---------
GET /test01?location= HTTP/1.1
Host: 192.168.XXX.8:9000
Accept-Encoding: identity
User-Agent: MinIO (Windows 10; amd64) minio-java/8.3.0
Content-MD5: 1B2M2Y8AsgTpgAmY7PhCfg==
x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
x-amz-date: 20220713T075848Z
Authorization: AWS4-HMAC-SHA256 Credential=*REDACTED*/20220713/us-east-1/s3/aws4_request, SignedHeaders=content-md5;host;x-amz-content-sha256;x-amz-date, Signature=*REDACTED*
HTTP/1.1 403
Date: Wed, 13 Jul 2022 08:00:52 GMT
Content-Length: 209
Content-Type: text/xml; charset=utf-8
Connection: close
<?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>
----------END-HTTP----------
Process finished with exit code 0
🏆 不能连接上Minio服务
如果9090端口未映射,外部访问不到Minio就会出现如下问题:
Exception in thread "main" java.net.ConnectException: Failed to connect to /192.168.XXX.8:9090
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:297)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:207)
at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
at io.minio.S3Base.execute(S3Base.java:532)
at io.minio.S3Base.getRegion(S3Base.java:694)
at io.minio.S3Base.execute(S3Base.java:470)
at io.minio.S3Base.executeHead(S3Base.java:728)
at io.minio.MinioClient.bucketExists(MinioClient.java:1135)
at FileUploader.main(FileUploader.java:28)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:81)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:120)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:295)
... 21 more
用telenet工具,访问9090端口进行排查:
表示外部访问不到,再次,查看容器,也只是映射了9000端口
原因分析:
启动容器时,没有对9090端口进行映射,如下
docker run -p 9000:9000 --name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=minio" \
-e "MINIO_SECRET_KEY=minio123" \
-v /opt/docker_minio/data:/data \
-v /opt/docker_minio/config:/root/.minio \
minio/minio server /data \
--console-address ":9000" --address ":9090"
解决方法:
停止容器,删除容器,增加9090端口映射,重新生成
# 停止容器
docker stop 容器ID
# 删除容器
docker rm 容器ID
# 重新生成容器
docker run -p 9000:9000 -p 9090:9090 --name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=minio" \
-e "MINIO_SECRET_KEY=minio123" \
-v /opt/docker_minio/data:/data \
-v /opt/docker_minio/config:/root/.minio \
minio/minio server /data \
--console-address ":9000" --address ":9090
🏆 上传文件权限
上传文件时,提示" xxx not a regular file"
原因,文件权限只读,修改文件权限即可。