您好,登錄后才能下訂單哦!
這篇文章主要介紹“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。