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

SpringBoot 2.x how to integrate MinIo file service

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

Share

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

This article mainly explains "SpringBoot 2.x how to integrate MinIo file service", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "SpringBoot 2.x how to integrate MinIo file services"!

MinIO is a high-performance object storage, compatible with Amazon S3 interface, fully taking into account the needs and experience of developers; supporting distributed storage, with high scalability and high availability; simple deployment but rich features. The official documentation is also very detailed. It has many different deployment models (stand-alone deployment, distributed deployment).

The reason why MinIO is easy to use is that it is easy to start, run, and configure. You can install and run it through docker, or you can download binaries and run them using scripts.

Installation

It is recommended to install with one click of docker:

Docker run-it-p 9000 9000-- name minio\

-d-- restart=always\

-e "MINIO_ACCESS_KEY=admin"\

-e "MINIO_SECRET_KEY=admin123456"\

-v / mnt/minio/data:/data\

-v / mnt/minio/config:/root/.minio\

Minio/minio server / data

Note:

The key must be greater than 8 bits, otherwise the creation will fail

File directories and configuration files must be mapped to the host, you know

Integrate Nginx:

Server {

Listen 80

Server_name minio.cloudbed.vip

Location / {

Proxy_set_header Host $http_host

Proxy_pass http://localhost:9000;

}

Location ~ /\ .ht {

Deny all

}

}

In this way, access the configured address through the browser and log in using the specified MINIO_ACCESS_KEY and MINIO_SECRET_KEY.

After a brief look, the function is OK. It supports creating Bucket, uploading, deleting, sharing and downloading files. At the same time, you can set read and write permissions on Bucket.

Integration

Minio supports access to JavaScript, Java, Python, Golang and other languages, here we choose the most familiar Java language, using the most popular framework SpringBoot 2.x.

Pom.xml introduces:

Io.minio

Minio

7.0.2

Application.properties introduces:

# MinIo File Server

Min.io.endpoint = http://minio.cloudbed.vip

Min.io.accessKey = admin

Min.io.secretKey = admin123456

MinIoProperties.java configuration entity:

/ * *

* entity class

* Java Notes: https://blog.52itstyle.vip

, /

@ Data

@ ConfigurationProperties (prefix = "min.io")

Public class MinIoProperties {

Private String endpoint

Private String accessKey

Private String secretKey

}

Make a tool class:

/ * *

* tool class

* Java Notes: https://blog.52itstyle.vip

, /

@ Component

@ Configuration

@ EnableConfigurationProperties ({MinIoProperties.class})

Public class MinIoUtils {

Private MinIoProperties minIo

Public MinIoUtils (MinIoProperties minIo) {

This.minIo = minIo

}

Private MinioClient instance

@ PostConstruct

Public void init () {

Try {

Instance = new MinioClient (minIo.getEndpoint (), minIo.getAccessKey (), minIo.getSecretKey ())

} catch (InvalidPortException e) {

E.printStackTrace ()

} catch (InvalidEndpointException e) {

E.printStackTrace ()

}

}

/ * *

* determine whether bucket exists

* @ param bucketName

* @ return

, /

Public boolean bucketExists (String bucketName) {

Try {

Return instance.bucketExists (bucketName)

} catch (Exception e) {

E.printStackTrace ()

}

Return false

}

/ * *

* create bucket

* @ param bucketName

, /

Public void makeBucket (String bucketName) {

Try {

Boolean isExist = instance.bucketExists (bucketName)

If (! isExist) {

Instance.makeBucket (bucketName)

}

} catch (Exception e) {

E.printStackTrace ()

}

}

/ * *

* File upload

* @ param bucketName

* @ param objectName

* @ param filename

, /

Public void putObject (String bucketName, String objectName, String filename) {

Try {

Instance.putObject (bucketName,objectName,filename,null)

} catch (Exception e) {

E.printStackTrace ()

}

}

/ * *

* File upload

* @ param bucketName

* @ param objectName

* @ param stream

, /

Public void putObject (String bucketName, String objectName, InputStream stream) {

Try {

Instance.putObject (bucketName,objectName,stream,null)

} catch (Exception e) {

E.printStackTrace ()

}

}

/ * *

* Delete files

* @ param bucketName

* @ param objectName

, /

Public void removeObject (String bucketName, String objectName) {

Try {

Instance.removeObject (bucketName,objectName)

} catch (Exception e) {

E.printStackTrace ()

}

}

/ / omit all kinds of CRUD

}

Currently, SDK does not support the creation of folders. If you want to create folders, you can only upload and create them through files.

MinIoUtils.putObject ("itstyle", "Girl Picture / Java Girl .jpg", "C:\\ Java Girl .jpg")

An instance can only have one account. If you want to use multiple accounts, you need to create multiple instances. In addition, minio supports single host, multiple disks and distributed deployment, but for most monolithic applications, monolithic is sufficient.

At this point, I believe you have a deeper understanding of "SpringBoot 2.x how to integrate MinIo file services". 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