Spring boot integrates Minio file storage

background The company's development framework...
Initialize client
Upload file
Download File
Delete file
background

The company's development framework integrates local storage of accessories, Alibaba cloud, Huawei cloud, etc. the current project requires that the accessory storage and application deployment environment cannot be the same server or use cloud storage. After technical selection, it is decided to integrate minio into the framework, deploy minio on another server, and open an external network port to solve the problem

Minio installation deployment

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

Download address: https://download.csdn.net/dow...

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

minio-service.xml and run.bat

<service> <id>minio</id> <name>MinIO Service</name> <description>MinIO is a High Performance Object Storage</description> <logpath>D:\minio\logs</logpath> <log mode="roll-by-size"> <sizeThreshold>10240</sizeThreshold> <keepFiles>8</keepFiles> </log> <executable>D:\minio\run.bat</executable> </service>
set MINIO_ACCESS_KEY=admin set MINIO_SECRET_KEY=abcd@1234 minio.exe server -address :9999 D:\minioData

After decompressing the deployment package, cmd enters the corresponding decompression path, enters the command minio.exe server D:\minioData, and closes 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: https://download.csdn.net/dow...

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

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

Enter the system and create a bucket to store files (similar to Alibaba cloud)

Configuring pom files
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>7.1.0</version> </dependency>
Configuration yml file

The upload / download addresses configured here are formal items. After the external network port is configured, the server cannot access the corresponding external network port. Upload to the internal network and download to the external 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: xxx
Minio tool class

Initialize client

public 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 file

The getkey method only specifies the corresponding custom storage path

Content type is specified so that pictures, pdf and other files can be browsed online when the browser can open the attachment

Do not specify that the default is stream. Open files 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 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 nginx agent

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 fail to read 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; }

20 November 2021, 05:59 | Views: 8563

Add new comment

For adding a comment, please log in
or create account

0 comments