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 use minio Storage Container in springboot

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

Share

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

This article mainly shows you "how to use minio storage container in springboot", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use minio storage container in springboot" this article.

Docker runs docker run-p 9000 data 9000-p 9001 data 9001-v / mydata/minio/data:/data minio/minio server / data-- console-address ": 9001java Guide package

It is better to use this version. Other versions have been tried with bug.

Io.minio minio 8.2.1 configuration file spring: # upload file size setting servlet: multipart: enabled: true max-file-size: 50MBminio: endpoint: xxx:9000 accesskey: xxx secretkey: xxx bucketName: xxx operation

1. Write a properties file

@ Data@Component@ConfigurationProperties (prefix = "minio") / / get public class MinioProperties {private String endpoint; private String accessKey; private String secretKey;} from the prefix of the configuration file

2. Write a minioClient

@ Configurationpublic class MinioConfig {@ Resource private MinioProperties minioProperties; @ Bean public MinioClient minioClient () {System.out.println (minioProperties.getAccessKey ()); System.out.println (minioProperties.getSecretKey ()); MinioClient minioClient = MinioClient.builder () .builds (minioProperties.getEndpoint ()) .build (); return minioClient }}

3. Upload file Api

Public class MinioServiceImpl implements MinioService {@ Value ("${minio.bucketName}") private String bucketName; @ Value ("${minio.endpoint}") private String endPoint; @ Resource private MinioClient minioClient; @ Override public List uploadFile (MultipartFile [] file) throws ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {if (file = = null | | file.length = = 0) {throw new APIException (ResultCode.PARAM_IS_BLANK) } List fileUrlList = new ArrayList (file.length); String url = ""; for (MultipartFile multipartFile: file) {/ / 1. Get the file name String originalFilename = multipartFile.getOriginalFilename (); / / 2. Intercept suffix String imgSuffix = originalFilename.substring (originalFilename.lastIndexOf (".")); / / 3. Generate the unique name String newFileName = UUID.randomUUID (). ToString () + imgSuffix; / / 4. Date directory SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd"); String dataPath = dateFormat.format (new Date ()); / / 5. Synthesis path String finalFileName = dataPath + "/" + newFileName; / / Don't forget bucketName url = endPoint + "/" + bucketName + "/" + finalFileName; try {/ / File upload InputStream in = multipartFile.getInputStream () MinioClient.putObject (PutObjectArgs.builder (). Bucket (bucketName) .object (finalFileName). Stream (in, multipartFile.getSize (),-1) .contentType (multipartFile.getContentType ()) .build (); in.close (); fileUrlList.add (url) } catch (IOException e) {throw new APIException (ResultCode.COMMON_FAIL);}} return fileUrlList;}} Local browsing Settings

Through the above url, you can access the picture directly.

The above is all the content of the article "how to use minio Storage Container in springboot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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