Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate Minio File Storage with SpringBoot

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "SpringBoot how to integrate Minio file storage", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "SpringBoot how to integrate Minio file storage" article.

Background

The company's development framework integrates attachment local storage, Aliyun, Huawei Cloud, etc. The current project requires that attachment storage and application deployment environment cannot be the same server, and cloud storage cannot be used. After technology selection, it is decided that the framework integrates minio, and the problem can be solved by deploying minio on another server to open a public network port.

Minio installation and deployment

Download the minio installation and deployment package and create the corresponding configuration file. An integrated package is provided here.

Download address: http://xiazai.jb51.net/202204/yuanma/minio_jb51.rar

Create a minioData folder as the file storage path, and unzip the installation package to modify the corresponding configuration file according to the placement path.

Minio-service.xml and run.bat

Minio MinIO Service MinIO is a High Performance Object Storage D:\ minio\ logs 10240 8 D:\ minio\ run.batset MINIO_ACCESS_KEY=adminset MINIO_SECRET_KEY=abcd@1234minio.exe server-address: 9999 D:\ minioData

After decompressing the deployment package, cmd enters the corresponding decompression path. Enter the command minio.exe server D:\ minioData and close the cmd command after initialization.

Use the service installation tool to install the service and select minio-service.exe

Download address of windows service installation tool: http://xiazai.jb51.net/202204/yuanma/windowsfuwu_jb51.rar

Access http://127.0.0.1:9999/ after starting the service

User name: admin password: abcd@1234 (port and account password are configured in the run.bat file)

Enter the system to create a bucket to store files (similar to Aliyun)

Configure pom file-- h3 > io.minio minio 7.1.0 configure yml file

Here, the upload / download address is the official project. After the public network port is configured, the corresponding external network port cannot be accessed in the server. Upload to the private network and download to the public network.

# minio configuration # upload address minio_uploadurl: http://192.168.1.42:9999/ # download address minio_downloadurl: http://192.168.1.42:9999/ # account minio_accesskey: admin # password minio_secrectkey: abcd@1234 # Storage folder minio_bucknetname: xxxMinio tool class initialization clientpublic MinioClient InitMinio () {MinioClient minioClient = MinioClient.builder (). Endpoint (frameConfig.getMinio_uploadurl ()) Credentials (frameConfig.getMinio_accesskey (), frameConfig.getMinio_secrectkey ()). Build (); try {boolean isExist = minioClient.bucketExists (frameConfig.getMinio_bucknetname ()); if (! isExist) {minioClient.makeBucket (frameConfig.getMinio_bucknetname ());}} catch (Exception e) {e.printStackTrace ();} return minioClient } upload files

The getkey method simply specifies the corresponding custom storage path

Content-type is specified so that when the browser can open attachments, files such as pictures and pdf can be viewed online.

If it is not specified, the default is stream, and the file is opened for download

Public boolean uploadMinioFile (InputStream stream, AttachmentDO attachmentDO,String contentType) {boolean result = true; try {MinioClient minioClient = InitMinio (); String bucketName = frameConfig.getMinio_bucknetname (); PutObjectOptions option = new PutObjectOptions (stream.available (),-1); option.setContentType (contentType); minioClient.putObject (bucketName,getKey (attachmentDO), stream,option) } catch (Exception e) {logger.error ("Minio failed to upload file:" + e.getMessage ()); result = false;} return result;} download the file

It should be noted that the generated file download address is bound to the server address specified in MinioClient and is invalid after using the nginx proxy.

Public String readMinioCommonFile (AttachmentDO attachmentDO) {String fileurl = ""; try {MinioClient minioClient = InitMinio (); String bucketName = frameConfig.getMinio_bucknetname (); return minioClient.presignedGetObject (bucketName, getKey (attachmentDO));} catch (Exception e) {logger.error ("Minio failed to read the file:" + e.getMessage ());} return fileurl } delete file public boolean deleteMinioFile (AttachmentDO attachmentDO) {boolean result = true; try {MinioClient minioClient = InitMinio (); String bucketName = frameConfig.getMinio_bucknetname (); minioClient.removeObject (bucketName,getKey (attachmentDO));} catch (Exception e) {logger.error ("Minio failed to delete file:" + e.getMessage ()); result = false } return result;} the above is about the content of this article on "how SpringBoot integrates Minio file storage". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report