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 into Springboot to realize File Storage

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to integrate Springboot with minio to achieve file storage". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to integrate Springboot with minio to achieve file storage"!

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

1. Install and deploy 1.1 Linux simple deployment wget https://dl.min.io/server/minio/release/linux-amd64/miniochmod + x minio MINIO_ROOT_USER=admin MINIO_ROOT_PASSWORD=123456 # # launch and specify port. / minio server / mnt/data-- console-address ": 9001" # # or launch nohup. / minio server / mnt/data > / opt/minio/minio.log 2 > & 1 & # in the background

Then access the corresponding address: if you go to the CVM, remember to go to the security group to open the corresponding port, and the account password is shown in the figure:

1.2 Docker deployment # download MinIO's Docker image docker pull minio/minio #-- console-address specifies the operating port of the MinIO Console (otherwise it will run at random) exposing port 9001 or 9000 docker run-p 9090 docker pull minio/minio 9000-p 9001Docker 9001-- name minio\-v / mydata/minio/data:/data\-e MINIO_ROOT_USER=minioadmin\-d minio/minio server / data-- console-address ": 9001" 2. Spring boot integration

Add related dependencies

Io.minio minio 8.0.3

Add relevant configuration information

If the default installation does not specify both Access key and Secret key, it is minioadmin, and Endpoint is the server API address.

Spring: # configuration file upload size limit servlet: multipart: max-file-size: 100MB max-request-size: 100MB# minio parameter configuration minio: endpoint: http://127.0.01:9000 accessKey: minioadmin secretKey: minioadmin

Injection client

Inject the client into the Spring container and get it directly when you use it.

@ Configurationpublic class MinIoConfig {@ Value ("${minio.endpoint}") private String endpoint; @ Value ("${minio.accessKey}") private String accessKey; @ Value ("${minio.secretKey}") private String secretKey / * inject minio client * * @ return * / @ Bean public MinioClient minioClient () {return MinioClient.builder () .injection (endpoint) .injection (accessKey, secretKey) .build ();}}

Write related business codes

Write the relevant business code, upload the picture and return the relevant path.

/ * File upload (custom file name) * / public MinIoUploadVo upload (String strDir, MultipartFile multipartFile) throws Exception {/ / bucket does not exist. Create if (! this.bucketExists (strDir)) {this.makeBucket (strDir);} InputStream inputStream = multipartFile.getInputStream (); / / create a headers Map headers = new HashMap () / / add the ContentType dynamic configuration of the request header file multipartFile.getContentType () headers.put ("Content-Type", "application/octet-stream"); String fileName = multipartFile.getOriginalFilename (); String minFileName = minFileName (fileName) Instance.putObject (PutObjectArgs.builder (). Bucket (strDir) .object (minFileName). Stream (inputStream, inputStream.available (),-1) / / PutObjectOptions, upload configuration (file size, file size in memory) .headers (headers) .build () String url = endpoint.concat ("/") .concat (strDir) .concat ("/") .concat (minFileName); / / returns the generated file name and access path return new MinIoUploadVo (strDir, fileName, minFileName, url);}

Upload file API

@ RequestMapping (value = "/ upload", method = RequestMethod.POST) public R upload (MultipartFile file, HttpServletRequest request) throws IOException {String strDir = request.getParameter ("bucketName") = = null? "car": request.getParameter ("bucketName"); try {MinIoUploadVo uploadVo = minioService.upload (strDir, file); return R.ok () .message ("File uploaded successfully") .data (uploadVo);} catch (Exception e) {log.error ("File upload failed, msg= {}", e.getMessage ()); e.printStackTrace (); return R.error ();}}

Test related interfaces

If you can't access the address, remember to turn on the relevant permissions.

3. Problem record

S3 API Request made to Console port. S3 Requests should be sent to API port.

The reason is that the Console console port is used in the configuration file, and the API port should be used:

At this point, I believe you have a deeper understanding of "how to integrate Springboot with minio to achieve file storage". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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