SpringBoot e-Commerce project mall (25K + STAR) address: github.com/macrozheng/…

Abstract

The object storage service can be used to store all kinds of files. The image storage of mall project adopts OSS. Today, we will talk about how to build an object storage service to store pictures.

Introduction of MinIO

MinIO is a high-performance object storage service based on the Go language. It has 19K+Star on Github. It uses the Apache License V2.0 open source protocol and is suitable for storing large amounts of unstructured data, such as images, videos, log files, backup data, and container/VM images. This article will use MinIO to build an object storage service for storing images.

Installation and Deployment

MinIO can be installed in a variety of ways. Here we will use it in the Docker environment.

  • Download MinIO’s Docker image:
docker pull minio/minio
Copy the code
  • Run MinIO in the Docker container. Here we attach the data and configuration folder of MiniIO to the host:
docker run -p 9090:9000 --name minio \
  -v /mydata/minio/data:/data \
  -v /mydata/minio/config:/root/.minio \
  -d minio/minio server /data
Copy the code
  • After successful operation, Access this address to log in and use MinIO, default Access Key and Secretminioadmin:http://192.168.6.132:9090

Upload files and use them

File upload and download can be completed by using MinIO’s web page. Let’s take picture upload and download as an example to demonstrate this function.

  • Before we can store the file, we need to create a new bucket:

  • After the bucket is created, you can upload files by clicking the upload button. Here we upload an image:

  • After uploading the image, we can copy the link button to get the image access path, but this is only a temporary access path:

  • To obtain a permanent access path, modify the bucket access policy. Click Edit Policy in the upper right corner of the bucket to modify the access policy.

  • There are three access policies to choose from, one is read-only, one is write-only, and one is read-write. In this case, we choose read-only, but it should be noted that the access prefix must be set to*. *Otherwise, it will not be accessible;

  • After setting up, we only need to copy the previous path in the link to permanently access the file;

  • Visit image effect display:

Use the MinIO client

Although MinIO is easy to manage on the web, the official website provides a command line based MinIO Client(MC). Let’s talk about how to use it.

Common commands

Let’s start by familiarizing ourselves with MC commands, which have a lot in common with Linux commands.

The command role
ls List files and folders
mb Create a bucket or folder
cat Displays file and object contents
pipe Redirect a STDIN to an object or file or STDOUT
share Generate urls for sharing
cp Copy files and objects
mirror Mirror buckets and folders
find Find files based on parameters
diff Compare the differences between two folders or buckets
rm Delete files and objects
events Notification of Managed Objects
watch Listen for events on files and objects
policy Managing Access Policies
session Manages saved sessions for the cp command
config Manage THE MC configuration file
update Checking software Updates
version Output Version Information

Installation and configuration

Since there is no built-in client in the MinIO server, we need to install and configure the client before using it. Here, the installation in Docker environment is taken as an example.

  • Download the Docker image of MinIO Client:
docker pull minio/mc
Copy the code
  • Running an MC in a Docker container:
docker run -it --entrypoint=/bin/sh minio/mc
Copy the code
  • After running, we need to 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> <API-SIGNATURE>
Copy the code
  • Our MinIO service can be configured like this:
MC config host add minio http://192.168.6.132:9090 minioadmin minioadmin S3v4Copy the code

Common operations

  • View buckets and files in buckets:
# View buckets
mc ls minio
# Check the files in the bucket
mc ls minio/blog
Copy the code

  • Create a file namedtestStorage bucket for:
mc mb minio/test
Copy the code

  • Sharedavatar.pngFile download path:
mc share download minio/blog/avatar.png
Copy the code

  • To find theblogPNG file in bucket:
mc find minio/blog --name "*.png"
Copy the code

  • Set up thetestThe access permission of a bucket isread-only:
Upload: none, download, upload, public
mc policy set download minio/test/
# Check the current permission of the bucket
mc policy list minio/test/
Copy the code

The resources

IO /cn/minio-qu…

The public,

Mall project full set of learning tutorials serialized, attention to the public number the first time access.