Article directory
Preface
Official website: https://www.minio.org.cn/
MinIO is an object storage solution that provides an API compatible with Amazon Cloud Technology S3 and supports all core S3 functions. MinIO is built to be deployed anywhere - public or private cloud, bare metal infrastructure, orchestration environments and edge infrastructure.
1. Use docker to install minio
Document address: https://www.minio.org.cn/docs/minio/container/index.html
1. Start the container
mkdir -p ~/minio/data
docker run \
-p 9000:9000 \
-p 9090:9090 \
--name minio \
-v ~/minio/data:/data \
-e "MINIO_ROOT_USER=ROOTNAME" \
-e "MINIO_ROOT_PASSWORD=CHANGEME123" \
quay.io/minio/minio server /data --console-address ":9090"
The above example works like this:
-
mkdir creates a new local directory within the home directory. ~/minio/data
-
docker run starts the MinIO container.
-
-p binds the local port to the container port.
-
-name creates a name for the container.
-
-v sets the file path to the persistent volume location to be used by the container. When MinIO writes data, that data is mirrored to a local path, allowing it to persist between container restarts. You can replace it with another local file location where the user has read, write, and delete access. /data ~/minio/data ~/minio/data
-
-e sets environment variables and . These set the root user credentials . Change the sample value to use for the container.
2. Connect the browser to the MinIO server
Access the MinIO console by going to your browser and going to one of the console addresses specified in the command output. For example, the console in the sample output: http://192.0.2.10:9090 http://127.0.0.1:9090 indicates two possible addresses for connecting to the console. http://127.0.0.1:9000
MinIO automatically redirects browser access to the MinIO console when the port is used to connect to the API. 9000
Log in to the console using the credentials you defined in the and environment variables.
2. Java integrated minio
最低要求 Java 1.8 或更高版本。
Document address: https://min.io/docs/minio/linux/developers/java/minio-java.html#minio-java-quickstart
1. lead package
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.4</version>
</dependency>
<!-- minio8.3以上版本需要手动引入okhttp-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
2. Create the key and configure it
- Open the minio console
- Click on the menu bar Access Keys
- Click Create access key to create it
- Finally, download the access key everywhere and open it to a json
application.yml .
server:
port: 9954
minio:
url: http://xxx:9000
access-key: monioadmin
secret-key: monioadmin
bucket-name: xxx
3. Create minio tool class code
The code is as follows (example):
import com.erfou.minio.demo.properties.MinioProperties;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Component
@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class MinioTemplate {
@Resource
private MinioProperties minioProperties;
public MinioTemplate() {
}
public MinioClient getMinioClient() {
return MinioClient.builder().endpoint(minioProperties.getUrl()).credentials(minioProperties.getAccessKey(),minioProperties.getSecretKey()).build();
}
public void createBucket(String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
MinioClient minioClient = getMinioClient();
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket(bucketName)
.build());
}
}
/**
* 获取文件外链
* @param bucketName bucket 名称
* @param objectName 文件名称
* @return
*/
public String getObjectURL(String bucketName,String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
return getMinioClient().getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(bucketName)
.object(objectName)
.build());
}
/**
* 获取文件
* @param bucketName
* @param objectName
* @return
*/
public InputStream getObject(String bucketName,String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
return getMinioClient().getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
}
/**
* 上传文件
* @param bucketName
* @param objectName
* @param stream
*/
public void putObject(String bucketName, String objectName, InputStream stream) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
createBucket(bucketName);
getMinioClient().putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.stream(stream, stream.available(), -1)
.build());
}
/**
* 删除文件
* @param bucketName
* @param objectName
*/
public void removeObject(String bucketName, String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
getMinioClient().removeObject(RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
}
/**
* 下载文件
* @param bucketName
* @param objectName
*/
public InputStream downloadFile(String bucketName,String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
return getMinioClient().getObject(GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
}
}
In the above code, we created a MinioUtils tool class, which contains some commonly used MinIO operation methods. These include operations such as checking whether a bucket exists, creating a bucket, listing all buckets, uploading files, deleting files, and downloading files.
4. Create MinioStorageService
import com.erfou.minio.demo.config.MinioTemplate;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Service
public class MinioStorageService {
@Resource
private MinioTemplate minioTemplate;
@Value("${minio.bucket-name}")
private String bucketName;
/**
* 获取文件外链
*
* @param objectName 文件名称
*/
public String getObjectURL(String objectName) {
try {
return minioTemplate.getObjectURL(bucketName, objectName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String uploadFile(InputStream inputStream, String filePath) {
String path = null;
try {
minioTemplate.putObject(bucketName, filePath, inputStream);
path = filePath;
} catch (Exception e) {
}
return path;
}
public InputStream downloadFile(String filePath) {
InputStream inputStream = null;
try {
inputStream = minioTemplate.getObject(bucketName, filePath);
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}
public void removeFile(String filePath){
try{
minioTemplate.removeObject(bucketName,filePath);
}catch (Exception e){
e.printStackTrace();
}
}
public void createBucket(String bucketName) {
try {
minioTemplate.createBucket(bucketName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.Create controller
import com.erfou.minio.demo.service.MinioStorageService;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@RestController
public class MinioController {
@Resource
private MinioStorageService minioStorageService;
@RequestMapping("/createBucket")
public void createBucket(@RequestParam("bucketName") String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
minioStorageService.createBucket(bucketName);
}
@RequestMapping("/uploadFile")
public void uploadFile(@RequestParam("file") MultipartFile file,@RequestParam("fileName") String fileName) throws IOException {
minioStorageService.uploadFile(file.getInputStream(),fileName);
}
}
After the upload is successful, you can see this file in the console
3. Vue integrates minio
Official documentation: https://min.io/docs/minio/linux/developers/javascript/minio-javascript.html
1.Introduce dependencies
npm install --save minio
2.Integration
<template>
<div id="nav">
<p>上传文件:</p>
<input ref="input" type="file" id="uploader" @change="handleFiles">
<!-- <button @click="add">点击创建桶并上传文件</button> -->
<!-- <img src="./assets/logo.png" alt=""> -->
</div>
<!-- <router-view /> -->
</template>
<script >
export default {
methods: {
handleFiles(event) {
var f = event.target.files[0]
let reader = new FileReader();
reader.readAsArrayBuffer(f);
reader.onload = function (e) {
let res = e.target.result;//ArrayBuffer
let buf = Buffer.from(res);//Buffer
var Minio = require('minio')
var minioClient = new Minio.Client({
endPoint: 'xxxxx2',
port: 9000,
useSSL: false,
accessKey: 'xxx',
secretKey: 'xxx'
});
minioClient.putObject('europetrip2', f.name, buf, null, f.size, function (err, data) {
if (err)
console.log(err)
else
console.log("Successfully uploaded data to testbucket/testobject");
});
}
}
}
}
</script>