您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“docker怎么安装minio及实现文件上传、删除、下载”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“docker怎么安装minio及实现文件上传、删除、下载”文章能帮助大家解决问题。
第一步 查镜像
docker search minio
第二步 拉镜像
docker pull minio/minio
第三步 启动容器
docker run -p 9000:9000 --name minio -d --restart=always -e “MINIO_ACCESS_KEY=admin” -e “MINIO_SECRET_KEY=admin123456” -v /home/data:/data -v /home/config:/root/.minio minio/minio server /data
第四步 登录界面
http//:ip+9000
ACCESS_KEY:damin
SECRET_KEY:admin123456
项目结构
pom依赖:
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>3.0.10</version> </dependency> </dependencies>
启动类:
@SpringBootApplication @EnableDiscoveryClient public class CfUploadApplication { public static void main(String[] args) { SpringApplication.run(CfUploadApplication.class,args); } }
yml配置文件:
server: port: 8002 spring: application: name: upload-service cloud: nacos: discovery: server-addr: localhost:8848 servlet: multipart: enabled: true #开启文件上传 max-file-size: 500MB max-request-size: 500MB minio: endpoint: http://localhost:9000 #Minio服务所在地址 bucketName: cheung #存储桶名称 accessKey: admin #访问的key secretKey: admin123456 #访问的秘钥 logging: level: com.heima: debug
controller代码:
package com.cheung.upload.controller; import io.minio.MinioClient; import io.minio.policy.PolicyType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; @RestController @RequestMapping("file") public class UploadController { private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class); @Value("${minio.endpoint}") private String ENDPOINT; @Value("${minio.bucketName}") private String BUCKETNAME; @Value("${minio.accessKey}") private String ACCESSKEY; @Value("${minio.secretKey}") private String SECRETKEY; //文件创建 @PostMapping("/upload") public String upload(MultipartFile file) { String s = null; try { MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY); //存入bucket不存在则创建,并设置为只读 if (!minioClient.bucketExists(BUCKETNAME)) { minioClient.makeBucket(BUCKETNAME); minioClient.setBucketPolicy(BUCKETNAME, "*.*", PolicyType.READ_ONLY); } String filename = file.getOriginalFilename(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 文件存储的目录结构 String objectName = sdf.format(new Date()) + "/" + filename; // 存储文件 minioClient.putObject(BUCKETNAME, objectName, file.getInputStream(), file.getContentType()); LOGGER.info("文件上传成功!"); s = ENDPOINT + "/" + BUCKETNAME + "/" + objectName; } catch (Exception e) { LOGGER.info("上传发生错误: {}!", e.getMessage()); } return s; } //文件删除 @DeleteMapping("/delete") public String delete(String name) { try { MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY); minioClient.removeObject(BUCKETNAME, name); } catch (Exception e) { return "删除失败" + e.getMessage(); } return "删除成功"; } //文件下载 @GetMapping("/download") public void downloadFiles(@RequestParam("filename") String filename, HttpServletResponse httpResponse) { try { MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY); InputStream object = minioClient.getObject(BUCKETNAME, filename); byte buf[] = new byte[1024]; int length = 0; httpResponse.reset(); httpResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); httpResponse.setContentType("application/octet-stream"); httpResponse.setCharacterEncoding("utf-8"); OutputStream outputStream = httpResponse.getOutputStream(); while ((length = object.read(buf)) > 0) { outputStream.write(buf, 0, length); } outputStream.close(); } catch (Exception ex) { LOGGER.info("导出失败:", ex.getMessage()); } } }
使用postman进行测试
关于“docker怎么安装minio及实现文件上传、删除、下载”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。