今天通过docker安装了一个minio的容器 ,用来存储文件和图片 ,使用的命令为
docker pull minio/minio:RELEASE.2021-01-16T02-19-44Z
docker run --name minio -p 9000:9000 -d \
-v /home/root/data/minio/data:/data \
-v /home/root/data/minio/config:/root/.minio \
-e "MINIO_ACCESS_KEY=admin" -e "MINIO_SECRET_KEY=123@newedu.com" \
--restart=always minio/minio:RELEASE.2021-01-16T02-19-44Z server /data
然后想通过java程序来进行上传文件的存储 ,写好了,记录一下
一:加入依赖
<!-- minio的依赖-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.2</version>
<scope>compile</scope>
</dependency>
上传页面
<form action="uploadFile" method="post" enctype="multipart/form-data">
<p>请选择文件:<input type="file" name="file"></p>
<p><button>上传</button></p>
</form>
配置类
package com.example.testboot.config;
import io.minio.MinioClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Minio 配置类
*
*/
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig
{
/**
* 服务地址
*/
private String url;
/**
* 用户名
*/
private String accessKey;
/**
* 密码
*/
private String secretKey;
/**
* 存储桶名称
*/
private String bucketName;
public String getUrl()
{
return url;
}
public void setUrl(String url)
{
this.url = url;
}
public String getAccessKey()
{
return accessKey;
}
public void setAccessKey(String accessKey)
{
this.accessKey = accessKey;
}
public String getSecretKey()
{
return secretKey;
}
public void setSecretKey(String secretKey)
{
this.secretKey = secretKey;
}
public String getBucketName()
{
return bucketName;
}
public void setBucketName(String bucketName)
{
this.bucketName = bucketName;
}
@Bean
public MinioClient getMinioClient()
{
return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
}
}
请求控制类
package com.example.testboot.controller;
import com.example.testboot.annotation.CustomResponse;
import com.example.testboot.config.MinioConfig;
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
@RestController
public class UserController {
@Autowired
private MinioClient client;
@Autowired
private MinioConfig minioConfig;
public boolean bucketExists(String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
boolean flag = false;
flag = client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (flag) {
return true;
}
return false;
}
/**
*
* @param file 上传的文件
* @return 访问地址
*
*/
@RequestMapping("/uploadFile")
public String uploadFile(MultipartFile file) throws Exception
{
String fileName = file.getOriginalFilename(); //对文件名进行编码
PutObjectArgs args = PutObjectArgs.builder()
.bucket(minioConfig.getBucketName()) //储存桶名称
.object(fileName) //文件路径加文件名
.stream(file.getInputStream(), file.getSize(), -1)
.contentType(file.getContentType())
.build();
client.putObject(args); //上传文件
//返回上传的路径
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
}
}
配置文件
minio:
url: http://192.168.33.168:9000/
accessKey: admin
secretKey: 创建的时候设置的密码
bucketName: 桶的名字
上传后,图片直接保存到minio服务器的桶中,通过返回的路径可以直接访问上传后的图片,效果不错
网友评论