spring cloud 文件服务器,SpringBoot集成MinIO文件服务器

docker-compose安装minio

version: '3'

services:

minio:

image: minio/minio:latest # 原镜像`minio/minio:latest`

container_name: minio # 容器名为'minio'

restart: always # 指定容器退出后的重启策略为始终重启

volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录

- "./data:/data"

- "./minio:/minio"

- "./config:/root/.minio"

environment:

TZ: Asia/Shanghai

LANG: en_US.UTF-8

MINIO_PROMETHEUS_AUTH_TYPE: "public"

MINIO_ACCESS_KEY: "root" # 登录账号

MINIO_SECRET_KEY: "password" # 登录密码

command: server /data

logging:

driver: "json-file"

options:

max-size: "100m"

ports: # 映射端口

- "9293:9000"

- "42330:42330"

运行

docker-compose -f ./docker-compose-minio.yml -p minio up -d --build

设置minio基本参数

3cc3ab005d1c

登录http://localhost:9293/minio输入root 和password

点击右下角加号选择创建桶create buket

3cc3ab005d1c

设置桶的权限

3cc3ab005d1c

集成SpringBoot

引入依赖

io.minio

minio

6.0.11

commons-io

commons-io

2.6

配置文件

@Getter

@Setter

@ConfigurationProperties(prefix = "minio")

public class MinioProp {

public static final String MINIO_BUCKET = "mxf";

private String endpoint;//连接url

private String accesskey;//用户名

private String secretKey;//密码

}

@Configuration

@EnableConfigurationProperties(MinioProp.class)

public class MinioConfiguration {

@Autowired

private MinioProp minioProp;

@Bean

public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {

MinioClient client =new MinioClient(minioProp.getEndpoint(),minioProp.getAccesskey(),minioProp.getSecretKey());

return client;

}

}

@RestController

@RequestMapping("/minio")

public class MinioController {

@Autowired

private MinioClient minioClient;

@GetMapping("/upload")

public String upload(){

return "/upload.jsp";

}

@ResponseBody

@PostMapping("/upload")

public Object upload(@RequestParam(name = "file", required = false) MultipartFile file){

if(file==null || file.getSize()==0){

return "上传文件不能为空";

}

String orgfileName = file.getOriginalFilename();

try {

InputStream in = file.getInputStream();

String contentType= file.getContentType();

minioClient.putObject(MinioProp.MINIO_BUCKET,orgfileName,in,null, null, null, contentType);

Map data=new HashMap<>();

data.put("bucketName",MinioProp.MINIO_BUCKET);

data.put("fileName",orgfileName);

return data;

} catch (Exception e) {

e.printStackTrace();

}

return "上传失败";

}

@RequestMapping("/download/{fileName}")

public void download(HttpServletResponse response, @PathVariable(name = "fileName") String fileName) {

InputStream in=null;

try {

//获取文件对象 stat原信息

ObjectStat stat =minioClient.statObject(MinioProp.MINIO_BUCKET, fileName);

response.setContentType(stat.contentType());

response.setContentType("application/octet-stream; charset=UTF-8");

in = minioClient.getObject(MinioProp.MINIO_BUCKET, fileName);

IOUtils.copy(in,response.getOutputStream());

}catch (Exception e){

e.printStackTrace();

}finally {

if(in!=null){

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

使用postman测试

3cc3ab005d1c

3cc3ab005d1c

浏览器直接访问

3cc3ab005d1c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值