官网:https://www.minio.org.cn/
MinIO 是一种对象存储解决方案,提供与亚马逊云科技 S3 兼容的 API,并支持所有核心 S3 功能。 MinIO 专为部署在任何地方而构建 - 公共云或私有云、裸机基础架构、编排环境和边缘基础架构。
文档地址:https://www.minio.org.cn/docs/minio/container/index.html
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"
上面的示例是这样工作的:
mkdir在主目录中创建新的本地目录。~/minio/data
docker run启动 MinIO 容器。
-p将本地端口绑定到容器端口。
-name为容器创建名称。
-v将文件路径设置为容器要使用的持久卷位置。 当 MinIO 将数据写入 时,该数据将镜像到本地路径,允许它在容器重新启动之间持续存在。 您可以替换为用户具有读取、写入和删除访问权限的另一个本地文件位置。/data ~/minio/data ~/minio/data
-e分别设置环境变量和 。 这些设置根用户凭证。 更改要用于容器的示例值。
通过转到浏览器并转到命令输出中指定的控制台地址之一来访问 MinIO 控制台。 例如,示例输出中的控制台:http://192.0.2.10:9090 http://127.0.0.1:9090 指示用于连接到控制台的两个可能地址。http://127.0.0.1:9000
当端口用于连接到 API 时,MinIO 会自动将浏览器访问重定向到 MinIO 控制台。9000
最低要求 Java 1.8 或更高版本。
文档地址:https://min.io/docs/minio/linux/developers/java/minio-java.html#minio-java-quickstart
<dependency>
<groupId>io.miniogroupId>
<artifactId>minioartifactId>
<version>8.5.4version>
dependency>
<dependency>
<groupId>com.squareup.okhttp3groupId>
<artifactId>okhttpartifactId>
<version>4.9.3version>
dependency>
server:
port: 9954
minio:
url: http://xxx:9000
access-key: monioadmin
secret-key: monioadmin
bucket-name: xxx
代码如下(示例):
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());
}
}
在上面的代码中,我们创建了一个 MinioUtils 工具类,其中包含了一些常用的 MinIO 操作方法。其中包括检查桶是否存在、创建桶、列出所有桶、上传文件、删除文件和下载文件等操作。
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();
}
}
}
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);
}
}
官方文档:https://min.io/docs/minio/linux/developers/javascript/minio-javascript.html
npm install --save minio
<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>