1 MinIO安装(Docker)
●在root目录下新建docker_minio文件夹
●在docker_minio文件夹下新建config文件夹,data文件夹
●在root目录下新建docker_compose文件夹,在docker_compose文件夹中添加docker-compose.yaml
services:
minio:
image: quay.io/minio/minio
container_name: minio_container
ports:
- "9000:9000" #api接口(内部访问)
- "9001:9001" #控制台端口(外部界面)
environment:
TZ: Asia/Shanghai #时区上海
MINIO_ROOT_USER: abc #管理后台用户名
MINIO_ROOT_PASSWORD: abc #管理后台密码(最少8个字符)
MINIO_COMPRESS: "off" #开启压缩(on开启,off关闭)
MINIO_COMPRESS_EXTENSIONS: "" #扩展名 .pdf .doc为空,所有类型均压缩
MINIO_COMPRESS_MIME_TYPES: "" #mine类型application/pdf为空,所有类型均压缩
volumes:
- "/root/docker_minio/data:/data" #映射当前目录下的打他目录至容器内/data目录
- "/root/docker_minio/config:/root/.minio" #映射配置目录
command: server --address ':9000' --console-address ':9001' /data #服务器地址是9000,服务器控制台地址是9001,数据储存在/data下
privileged: true
networks:
- sunner_network
●通过docker compose up -d命令执行docker-compose.yaml文件(必须在docker_compose文件夹目录下执行),然后系统会自动拉取minio镜像,创建并启动minio容器
●执行docker ps -a命令,出现下图,表示安装和启动minio成功
2 MinIO的使用
访问MinIO有2个端口,分别是9000端口和9001端口
9000端口是用于内部访问,比如用户通过SpringBoot接口间接访问MinIO
9001端口是用于外部访问,即通过浏览器访问
2.1 桶
MinIO所有的文件都保存在桶中,可以将桶理解为文件夹
2.1.1新建桶
2.1.2 上传文件
2.2 网页访问图片
2.2.1 复制访问链接
●将127.0.0.1修改为MinIO所在服务器地址
●去掉访问链接后半段(去掉.png之后的)
2.2.2 修改桶权限为public
2.2.3 访问图片
在浏览器的地址栏输入"访问链接"
3 SpringBoot集成MinIO
3.1 引入依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.9</version>
</dependency>
3.2 application.yaml
minio:
endpoint: http://123.123.123.123:9000 #minio所在服务器访问地址
accessKey: abc #用户名
secretKey: abc #密码
3.3 MinIO配置类
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProp {
private String endpoint;
private String accessKey;
private String secretKey;
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
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;
}
}
3.4 通过MinIO配置类创建MinioClient,并注册进SpringIOC容器
@Configuration //"配置类"注解
@MapperScan({"xyz.aboluo.minio.dao"}) //dao接口包扫描
//@EnableConfigurationProperties({Teacher.class})
public class MinioAutoConfiguration {
@Autowired
private MinioProp minioProp;
@Bean
public MinioClient getMinioClient() {
return MinioClient.builder()
.endpoint(minioProp.getEndpoint())
.credentials(minioProp.getAccessKey(), minioProp.getSecretKey())
.build();
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址(允许任何地址访问)
corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头(允许任何请求头访问)
corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法(允许任何方法(GET,POST等等)方法)
source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}
3.5 Controller接口
@RestController
@RequestMapping("/minioFileHandle")
public class MinioController {
private static final Logger LOGGER = LoggerFactory.getLogger(MinioController.class);
@Autowired
private MinioClient minioClient;
/**
* 获取bucket列表
*
* @return
*/
@RequestMapping("/bucketList.do")
public Result bucketList() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
List<Map<String, String>> buckets = new ArrayList<>();
for (Bucket bucket : minioClient.listBuckets()) {
Map<String, String> returnBucket = new HashMap<>();
returnBucket.put("bucketName", bucket.name());
returnBucket.put("creationDateTime", bucket.creationDate().toString());
buckets.add(returnBucket);
}
return Result.success(buckets);
}
/**
* 查询bucket下的所有文件信息
*
* @return
*/
@RequestMapping("/queryBucketFiles.do")
public Result queryBucketFiles(@RequestParam String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
//检查桶是否存在
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
throw new SunnerRuntimeException(Result.ResultEnum.BUCKET_NOT_EXIST);
}
//遍历封装桶中的文件信息
Iterator<io.minio.Result<Item>> iterator = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build()).iterator();
List<Object> items = new ArrayList<>();
while (iterator.hasNext()) {
Item item = iterator.next().get();
items.add(JSON.parse(String.format("{'fileName':'%s','fileSize':'%s','lastModified':'%s'}", item.objectName(), item.size(), Date.from(item.lastModified().toInstant()))));
}
return Result.success(items);
}
/**
* 文件下载
*
* @return
*/
@RequestMapping("/download.do")
public Result download(@RequestParam String bucketName, @RequestParam String fileName, HttpServletResponse response) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
response.setHeader("picture-upload-date-time", URLEncoder.encode("abccc", StandardCharsets.UTF_8));
response.getOutputStream().write(minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build()).readAllBytes());
return Result.returnVoid();
}
}