介绍
对象存储服务(Object Storage Service,OSS)是一种海量、安全、低成本、高可靠的云存储服务,适合存放任意类型的文件。容量和处理能力弹性扩展,多种存储类型供选择,全面优化存储成本。
在项目开发过程中,我们会产生大量的对象数据,包括:日志文件,数据库脚本文件、安装包,容器镜像,图像、视频等等,我们不仅仅是需要有一个集中的地方来存储,还需要能基于 Web 的方式来访问它们。
一般的公有云厂家都会提供OBS服务,现在遇到一个需求,研发要求继续使用OBS对接程序开发,将数据存储到桶中,但是客户要求数据只能保存到本地,数据不能走公网上云。
经过查询,发现Minio是个不错的对象存储解决方法。
Minio 是个基于 Golang 编写的开源对象存储套件,虽然轻量,却拥有着不错的性能。
官网地址:MinIO | High Performance, Kubernetes Native Object Storage。
它可以单节点部署,也可以多节点部署,部署方式有很多。
部署和测试
本次采用二进制部署的方式,部署的详细参考官方网站:
Deploy MinIO: Single-Node Single-Drive — MinIO Object Storage for Linux
#下载二级制文件,将可执行程序配置到环境中
wget https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio
sudo mv minio /usr/local/bin/
配置启动服务
#配置服务文件/etc/systemd/system/minio.service
#vim /etc/systemd/system/minio.service
[Unit]
Description=MinIO
Documentation=https://min.io/docs/minio/linux/index.html
Wants=network-online.target
After=network-online.target
AssertFileIsExecutable=/usr/local/bin/minio
[Service]
WorkingDirectory=/usr/local
User=minio-user
Group=minio-user
ProtectProc=invisible
EnvironmentFile=-/etc/default/minio
ExecStartPre=/bin/bash -c "if [ -z \"${MINIO_VOLUMES}\" ]; then echo \"Variable MINIO_VOLUMES not set in /etc/default/minio\"; exit 1; fi"
ExecStart=/usr/local/bin/minio server $MINIO_OPTS $MINIO_VOLUMES
# MinIO RELEASE.2023-05-04T21-44-30Z adds support for Type=notify (https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=)
# This may improve systemctl setups where other services use `After=minio.server`
# Uncomment the line to enable the functionality
# Type=notify
# Let systemd restart this service always
Restart=always
# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65536
# Specifies the maximum number of threads this process can create
TasksMax=infinity
# Disable timeout logic and wait until process is stopped
TimeoutStopSec=infinity
SendSIGKILL=no
[Install]
WantedBy=multi-user.target
配置环境变量文件,/etc/default/minio,如果没有该环境变量配置文件,安装的时候将会采用默认配置
# MINIO_ROOT_USER and MINIO_ROOT_PASSWORD sets the root account for the MinIO server.
# This user has unrestricted permissions to perform S3 and administrative API operations on any resource in the deployment.
# Omit to use the default values 'minioadmin:minioadmin'.
# MinIO recommends setting non-default values as a best practice, regardless of environment
MINIO_ROOT_USER=myminioadmin #此处定义管理员账号
MINIO_ROOT_PASSWORD=minio-secret-key-change-me #此处定义管理员密码
# MINIO_VOLUMES sets the storage volume or path to use for the MinIO server.
MINIO_VOLUMES="/mnt/data" #此处定义数据保存路径
# MINIO_SERVER_URL sets the hostname of the local machine for use with the MinIO Server
# MinIO assumes your network control plane can correctly resolve this hostname to the local machine
# Uncomment the following line and replace the value with the correct hostname for the local machine and port for the MinIO server (9000 by default).
#MINIO_SERVER_URL="http://minio.example.net:9000"
启动服务
sudo systemctl start minio.service #启动
sudo systemctl status minio.service #检查状态
journalctl -f -u minio.service #排查服务状态
web控制台端口9090,API端口9000,默认密码如上所示
配置使用
要使用obs,就要使用所谓的ak和sk,即accesskey和sercetkey,这个需要创建一个用户管理,个个公有云厂家OBS中对AK.SK的描述都很详细,这里不再阐述
创建用户
在administrator->identity->uers中,创建一个test2账户,权限设置为可以读写
创建好后为test2用户配置ak,sk
配置好的ak/sk请自己保存好。
测试用例
我正在学习go语言,参考官方api指导,写了一个go语言用例,用来创建一个桶并上传文件,详细参考官方手册
https://min.io/docs/minio/linux/developers/go/minio-go.html
package main
import (
"context"
"log"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
ctx := context.Background()
endpoint := "10.137.142.236:9000" //minio服务器的API端口
accessKeyID := "5orT0meRrXyYpbSuDxjT" //AK
secretAccessKey := "dVSPNSYmgRDcoy29YIwdzHfgkMS7r7vKI8lB2RjV" //SK
useSSL := false //不使用HTTPS
// Initialize minio client object.
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}
// Make a new bucket called mymusic.
bucketName := "test1" //创建的桶的名称
location := "us-east-1" //保持默认,可以修改
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
if err != nil {
// Check to see if we already own this bucket (which happens if you run this twice)
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
if errBucketExists == nil && exists {
log.Printf("We already own %s\n", bucketName)
} else {
log.Fatalln(err)
}
} else {
log.Printf("Successfully created %s\n", bucketName)
}
// Upload the zip file
objectName := "testfile.txt" //要上传的文件
filePath := "D:\\vscode-COD\\code-test\\go\\testfile.txt" //要上传的文件路径
contentType := "application/zip" //保持默认
// Upload the zip file with FPutObject
info, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
if err != nil {
log.Fatalln(err)
}
log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)
}
运行,可以看到上传成功
查看桶文件
用test2用户直接登录web控制台,可以查看桶中上传的文件,也可以本地上传文件进去