头图
When we usually do projects, file storage is a very common requirement. At this time, we will use object storage services. Usually we may choose third-party services such as OSS and AWS S3. Today, I will take you to build your own object storage service with visual management, which is very easy to use!

SpringBoot actual combat e-commerce project mall (50k+star) address: https://github.com/macrozheng/mall

Introduction to MinIO

MinIO is a high-performance object storage service based on the Go language, which has 28K+Star on Github. It uses the Apache License v2.0 open source protocol, which is very suitable for storing large-capacity unstructured data, such as pictures, videos, log files, backup data, and container/virtual machine images.

Install

Using Docker to install the MinIO service is very simple, just a few commands!
  • First download MinIO's Docker image;
docker pull minio/minio
  • After the download is complete, use the following command to run the MinIO service, pay attention to use --console-address specify the running port of the MinIO Console (otherwise it will run on a random port):
docker run -p 9090:9000 -p 9001:9001 --name minio \
-v /mydata/minio/data:/data \
-e MINIO_ROOT_USER=minioadmin \
-e MINIO_ROOT_PASSWORD=minioadmin \
-d minio/minio server /data --console-address ":9001"
  • After running successfully, you can access the management interface of MinIO Console. Enter the account password minioadmin:minioadmin to log in. The access address is: http://192.168.7.142:9090

MinIO Console use

MinIO Console is a visual management tool that comes with MinIO. Compared with the previous generation of visual tools, it is still a lot more powerful. Let's experience this tool below.
  • First look at the previous generation of MinIO Browser, which basically only supports the management functions of buckets and files;

  • Let’s take a look at the MinIO Console. It not only supports the management of storage buckets and files, but also adds management functions such as users, permissions, and logs, which is much stronger;

  • Before storing files, we must first create a bucket;

  • After the creation is successful, upload another file;

  • After the upload is successful, if you want to access the file from the outside, you need to set the access policy to public. The policy here is only public and private, which feels not very flexible;

  • After that, change the address to the Internet access address to access the pictures. By default, you can only download but not directly view (this problem will be solved below). The Internet access address: http://192.168.7.142:9090/blog/avatar. png

Client use

In fact, for object storage, the functions of the MinIO Console are still not enough, so the official also provides a command-line-based client MinIO Client (mc for short). Let's talk about its usage.

Common commands

Let's get familiar with the commands of mc first, these commands have many similarities with the commands in Linux.
Ordereffect
lsList files and folders
mbCreate a bucket or a folder
rbDelete a bucket or a folder
catDisplay file and object content
pipeRedirect a STDIN to an object or file or STDOUT
shareGenerate URL for sharing
cpCopy files and objects
mirrorMirror storage buckets and folders
findFind files based on parameters
diffCompare the differences between two folders or buckets
rmDelete files and objects
eventsManaged object notification
watchListen to events of files and objects
policyManage access policies
sessionManage saved sessions for the cp command
configManage mc configuration files
updateCheck for software updates
versionOutput version information

Installation and configuration

Since the MinIO server does not have its own client, we need to install and configure the client before it can be used. Here we take the installation in the Docker environment as an example.
  • Download the Docker image of MinIO Client;
docker pull minio/mc
  • Run mc in a Docker container;
docker run -it --entrypoint=/bin/sh minio/mc
  • After the operation is complete, we need to configure, configure our own MinIO service to the client, the configuration format is as follows;
mc config host add <ALIAS> <YOUR-S3-ENDPOINT> <YOUR-ACCESS-KEY> <YOUR-SECRET-KEY>
  • This can be configured for our MinIO service.
mc config host add minio http://192.168.7.142:9090 minioadmin minioadmin

Common operations

  • View the bucket and view the files existing in the bucket;
# 查看存储桶
mc ls minio
# 查看存储桶中存在的文件
mc ls minio/blog

  • Create a bucket test
mc mb minio/test

  • The download path of the shared avatar.png
mc share download minio/blog/avatar.png

  • Find the png file in the blog
mc find minio/blog --name "*.png"

  • Set test buckets access for read-only.
# 目前可以设置这四种权限:none, download, upload, public
mc policy set download minio/test/
# 查看存储桶当前权限
mc policy list minio/test/

Compatible with AWS S3

When we need to use object storage to connect to third-party services, these services often support AWS S3. For example, a live broadcast playback function requires object storage to store the playback video. Since MinIO is compatible with most APIs of AWS S3, we can directly use it as AWS S3.
  • We can download an AWS S3 client to try, whether MinIO can support S3 API, here is S3 Browser , download address: https://s3browser.com/

  • After installing S3 Browser , add an Account, enter the relevant login information, and pay attention to selecting the Account type as S3 Compatible Storage ;

  • After the connection is successful, we can see the bucket we created and the uploaded files;

  • S3 Browser This tool is still very powerful, and the MinIO Console is too weak compared to it;

  • There is a problem mentioned above, the picture files can not be viewed directly, in fact, because when accessing image files, MinIO returned Content-Type is application/octet-stream caused;

  • Next, we can modify the response header returned by default S3 Browser

  • Then .png response header at the beginning of change image/png on it;

  • It should be noted that the previously uploaded file needs to be re-uploaded to take effect. At this time, you can view the picture directly by visiting the link;

  • If you want to modify the access permissions of the bucket, you can modify it directly through the Permissions tag, is it much more flexible than the MinIO Console.

Summarize

If you want to build your own object storage service, MinIO is indeed the first choice. It is compatible with AWS S3 API. Using MinIO is equivalent to using AWS S3, and it is compatible with some mainstream third-party services. However, its own client MinIO Console is really a bit tasteless, fortunately it supports AWS S3, and you can use some powerful S3 client tools.

Reference

Official document: https://docs.min.io/

This article GitHub https://github.com/macrozheng/mall-learning has been included, welcome to Star!

macrozheng
1.1k 声望1.3k 粉丝