If you want to build an elegant, simple, full-featured static resource services, then use MinIO it.
1 Introduction
Web project development process, often need to deal with static resources (such as images, video, audio, js libraries, css library, etc.), in general, if the project need to use these resource files, we used the following ways:
- Local storage: that is static directory under the project of establishing js / css / icon / font / image / lib / audio / video directory, etc., are stored the corresponding resource file format. Upon use, the relative position in the html file for reference.
- Use a proxy to build a static resource services: resource that is stored in a file directory, using a proxy server (such as nginx, apache, etc.) to the directory mapping to construct resource services. When used, the use of proxy services in the html file url address reference.
- Using third-party tools to build a static resource services: the use of third party open source or object storage file storage tool, or to write a program that implements the interface can get the file. In use, with a corresponding interface address or addresses url.
- Use the online static resource services: such as Ali cloud, CDN and other services.
For local storage, it is obvious shortcomings, resources and code files are mixed together, it is not necessary, and inconvenient extension. For local applications on-premise, apparently static resources to build their own services more secure. For use proxy services and third-party tools, comparison, only do mapping agency services, although available, but features a single, only to do the mapping, no other management functions, is not easy to expand. Use a third-party file storage tool or object, you can file management, can be considered highly scalable, high performance, high availability and other factors, and therefore is a good choice, which, MinIO is such a useful tool for object storage, simple, fast and full-featured.
This article is through the installation of MinIO, configuration and use, building a static resource services to the resource image, video, audio, and other independent third-party js library is deployed, accessed; Java API will be provided by the use of MinIO brief, in order to facilitate further development.
2 MinIO Profile
Press MinIO official introduction, MinIO is a high performance object storage (difference block storage, file storage and object storage, reference may architects are aware of the distributed object storage solutions ), Amazon S3 compatible interface to fully consider the needs of developers and experience; support distributed storage, highly scalable, high-availability; deploying a simple, yet powerful. The official document also very detailed. It has several different deployment models (stand-alone deployment, distributed deployment). Why MinIO easy to use, the reason is that it's up and running, and configuration is very simple. Can be installed by docker run, you can download a binary file, and then use the script to run.
In this paper, the easiest way to explain, in linux machines, stand-alone deployment, run binaries.
3 MinIO run and use static resources
3.1 MinIO get
MinIO development documents , the download address is as follows:
- linux:
https://dl.min.io/server/minio/release/linux-amd64/minio
- windows:
https://dl.min.io/server/minio/release/windows-amd64/minio.exe
This article run in linux.
3.2 MinIO up and running
3.2.1 Front simple start
Storing the downloaded file to a directory as minio run directory (e.g., / opt / minio), create a new directory (e.g., / opt / minio-data) as the data storage location minio, to start, the following script:
cd /opt/minio
chmod +x minio
./minio server /opt/minio-data
After the output will start and endpoint access address corresponding access_key and secret_key, use a browser to access the endpoint address, if you can access, then minio has been successfully installed. But this start will have several drawbacks:
- Is not running in the background,
ctrl+c
will be the end of the process, the service is stopped - No custom username and password to access
- You did not specify IP and port
- No reservations to the log file
To solve these problems, it is recommended to use the following way to start running.
3.2.2 specify parameters for running background
Using nohup
the background program, the parameters also specify a password, and access log output address parameter, as shown below.
MINIO_ACCESS_KEY=minio MINIO_SECRET_KEY=minio123 nohup /opt/minio/minio server --address "${MINIO_HOST}:${MINIO_PORT}" /opt/minio-data > /opt/minio/minio.log 2>&1 &
MINIO_ACCESS_KEY
AndMINIO_SECRET_KEY
is the access password
${MINIO_HOST}:${MINIO_PORT}
Are the host and port access, modify according to the actual situation.
In this way, through the browser to access the address ${MINIO_HOST}:${MINIO_PORT}
, specified MINIO_ACCESS_KEY
and MINIO_SECRET_KEY
sign in.
3.2.3 Creating a bucket and specify access policy
Log in your browser to MinIO storage system, click on the bottom right corner to create a bucket to store the objects were created corresponding bucket to store static resource: image, video, audio. Thus, it may be carried out in a resource type corresponding to the file upload of the bucket, after uploading the file can be shared, resources may be obtained elsewhere sharing url, as shown below.
MinIO default policy is to share the most effective time address is 7 days, to break this limitation, you can set the policy in the bucket. Click on the appropriate bucket, edit policy
add a policy *.*
, Read Only
as follows:
So let go of the visit, no time limit, but simply press http://${MINIO_HOST}:${MINIO_PORT}/${bucketName}/${fileName}
can directly access resources (no need for shared operation).
Mistakes on MinIO directory
- In fact, for object storage, in fact, does not distinguish between a file or a directory, all files and directories are objects, that is, image / temp / xxx.jpg and image / temp / is an object. It has a tree structure with the essential difference between the operating system's file system.
- When you upload a file, objectName can
/temp/xxx.jpg
be considered the system automatically creates a temp directory.- MinIO not provide as deleted directory and delete all files in this directory (that is
rm -rf image/temp
), so in order to catalog image / temp delete, you need to put all the files to image / temp prefix removed.- When querying multiple files, you can use the prefix matching way acquisition, see the API documentation
listObjects(bucketName, prefix, recursive)
3.3 reference a static resource in html file
Through the above settings and run, MinIO as a static resource server has been completed, you can write html to refer MinIO static resources. The following is a test inside the html pictures, video, audio, all using the resources of MinIO address.
<div class="img-list">
<img src="http://${MINIO_HOST}:${MINIO_PORT}/image/test.jpg" alt="图片">
</div>
<div class="audio-list">
<audio src="http://${MINIO_HOST}:${MINIO_PORT}/audio/test.mp3"
controls="controls"></audio>
</div>
<div class="video-list">
<video src="http://${MINIO_HOST}:${MINIO_PORT}/video/test.mp4" controls="controls"></video>
</div>
Can be found in the resource can be loaded normally accessible.
4 Java client API operations
MinIO developer is very friendly, user interface API provides a variety of languages, can refer to specific MinIO development documentation . The following example to do some tests to Java.
4.1 Add dependence
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>6.0.13</version>
</dependency>
4.2 uses the Java API for file operations
Establishing MinIO client operation minioClient = new MinioClient(endpoint, accessKey, secretKey);
, parameter endpoint
is the access address MinIO, when the password corresponding to the next two starts.
4.2.1 upload files
/**
* 上传文件
* @param minioClient 操作客户端
* @param bucketName 上传的bucket名称
* @param objectName 上传后存储在bucket中的文件名
* @param filePath 上传的本地文件路径
*/
public void uploadFile(MinioClient minioClient, String bucketName, String objectName, String filePath) throws XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, IOException {
try {
// 若不存在bucket,则新建
boolean isExist = minioClient.bucketExists(bucketName);
if (!isExist) {
minioClient.makeBucket(bucketName);
}
// 使用 putObject 上传文件
minioClient.putObject(bucketName, objectName, filePath, null, null, null, null);
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}
4.2.2 Download file
/**
* 下载文件
*
* @param minioClient 操作客户端
* @param bucketName 上传的bucket名称
* @param objectName 上传后存储在bucket中的文件名
* @param downloadPath 下载文件保存路径
*/
public void downloadFile(MinioClient minioClient, String bucketName, String objectName, String downloadPath) throws XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, IOException {
File file = new File(downloadPath);
try (OutputStream out = new FileOutputStream(file)) {
InputStream inputStream = minioClient.getObject(bucketName, objectName);
byte[] tempbytes = new byte[1024];
int byteread = 0;
while ((byteread = inputStream.read(tempbytes)) != -1) {
out.write(tempbytes, 0, byteread);
}
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}
4.2.3 deleting files
Simply delete the file, use removeObject
can be.
minioClient.removeObject(bucketName, objectName);
4.2.4 List files
/**
* 罗列文件
* @param minioClient
* @param bucketName
*/
public void listFile(MinioClient minioClient, String bucketName) throws XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, IOException {
try {
Iterable<Result<Item>> results = minioClient.listObjects(bucketName);
Iterator<Result<Item>> iterator = results.iterator();
while (iterator.hasNext()) {
Item item = iterator.next().get();
System.out.println(item.objectName() + ", " + item.objectSize() + "B");
}
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}
5 summary
Because of static resource needs independent access, a static and dynamic separation, by using MinIO, you can quickly and easily implement static resource server for access. By MinIO download, deployment, startup, operation, configuration, etc. are described, and references in html static resource files, for example, to explain the use MinIO, and provides a simple to use the Java API. I hope everyone has to help.
Download
As used herein, the nohup of MinIO start, but the command is too long, we will usually write scripts to achieve startup, shutdown, and status inquiry, therefore, I have written a script full of sh file for all to use. Further, MinIO Java API test, the present example uses Spring Boot projects, in unit testing mode.
sh script and put together Spring Boot project my minio github example in (the script minio-serviced.sh
in the scripts directory), there is a need to download reference.
Script to use:
- Sh script to modify the parameters according to the actual situation
- Modify execute permissions: chmod + x minio-serviced.sh
- Press Start up / shut down / restart / run state: ./ minio-serviced.sh start / stop / restart / status
Reference material
- MinIO official website :
https://min.io/
- MinIO development documents :
https://docs.min.io/
- Go on in practice to test the waters of open source projects MIMIO object storage solutions :
https://mp.weixin.qq.com/s/MzA4ODg0NDkzOA==&mid=2247487119&idx=1&sn=6e09abb32392e015911be3a1d7f066e5&source=41
- Architects are aware of the distributed object storage solutions :
https://juejin.im/post/5cdc16e251882568651553f9
- Use minio building high-performance storage objects :
https://tonybai.com/2020/03/16/build-high-performance-object-storage-with-minio-part1-prototype
Past Articles
- SpringBoot get multiple data sources (3): parametric change source
- SpringBoot get multiple data sources (2): dynamic data sources
- Get SpringBoot multiple data sources (1): multiple sets of source policy
- java development will have learned: Dynamic Proxy
- 2019 read recommended books
- springboot + apache deployment https separated front and rear ends
- springboot + logback log output business practice (under)
- springboot + logback log output business practices (on)
My public number (search Mason技术记录
) for more technical records: