如何使用對(duì)象存儲(chǔ)服務(wù)Minio
這篇文章主要介紹“如何使用對(duì)象存儲(chǔ)服務(wù)Minio”,在日常操作中,相信很多人在如何使用對(duì)象存儲(chǔ)服務(wù)Minio問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何使用對(duì)象存儲(chǔ)服務(wù)Minio”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
推薦理由
開源免費(fèi)(是我們考慮的首要元素),高性能
長(zhǎng)的好看:有漂亮的界面。
為云環(huán)境而生:與k8s、etcd、docker等深度集成。
文檔詳細(xì):提供了Java、JavaScript、Python、Golang、.Net等各種語言的sdk,集成變得更加容易
部署簡(jiǎn)單
docker部署
將MiniIO的數(shù)據(jù)和配置文件夾掛在到宿主機(jī)上
docker run -p 9090:9000 --name minio \ -e MINIO_ACCESS_KEY=admin -e MINIO_SECRET_KEY=123123123 \ -v /mydata/minio/data:/data \ -v /mydata/minio/config:/root/.minio \ -d minio/minio server /data;# 如果不創(chuàng)建用戶名密碼,默認(rèn)用戶名密碼: minioadmin:minioadmin
訪問
springboot 使用minio
1 引入maven
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>7.0.2</version> </dependency>
2 配置 application.properties
minio.url= http://192.168.3.189:9090 minio.accessKey= adminminio.secretKey= 123123123 minio.secure=false minio.bucketName=testminio.configDir=/home/data/
3 注入屬性
@Component @ConfigurationProperties(prefix = "minio") public class MinioConfig { // "endPoint是一個(gè)URL,域名,IPv4或者IPv6地址" private String url; //("accessKey類似于用戶ID,用于唯一標(biāo)識(shí)你的賬戶") private String accessKey; //("secretKey是你賬戶的密碼") private String secretKey; //("如果是true,則用的是https而不是http,默認(rèn)值是true") private Boolean secure; //("默認(rèn)存儲(chǔ)桶") private String bucketName; //("配置目錄") private String configDir; @Bean public MinioClient getMinioClient() throws InvalidEndpointException, InvalidPortException { MinioClient minioClient = new MinioClient(url, accessKey, secretKey,secure); return minioClient; } public String getBucketName() { return bucketName; } public String getConfigDir() { return configDir; } 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 Boolean getSecure() { return secure; } public void setSecure(Boolean secure) { this.secure = secure; } public void setBucketName(String bucketName) { this.bucketName = bucketName; } public void setConfigDir(String configDir) { this.configDir = configDir; }}
4 創(chuàng)建工具類
@Component public class MinioUtil { @Autowired private MinioClient minioClient; /** * 上傳文件 */ public void uploadFile(InputStream inputStream, String objectName) { String buckName = "test"; try { if(!minioClient.bucketExists(buckName)) { minioClient.makeBucket(buckName); } minioClient.putObject(buckName, objectName, inputStream, inputStream.available(), "image/jpeg"); } catch (Exception e) { e.printStackTrace(); } } public void downloadFile(String bucketName, String fileName, String originalName, HttpServletResponse response) { try { InputStream file = minioClient.getObject(bucketName, fileName); String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8); response.setHeader("Content-Disposition", "attachment;filename=" + filename); ServletOutputStream servletOutputStream = response.getOutputStream(); int len; byte[] buffer = new byte[1024]; while ((len = file.read(buffer)) > 0) { servletOutputStream.write(buffer, 0, len); } servletOutputStream.flush(); file.close(); servletOutputStream.close(); } catch (ErrorResponseException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }}
5 測(cè)試
@RestController public class GreetingsController { @Autowired MinioUtil minioUtil; @RequestMapping(value = "/{name}", method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public String greetingText(@PathVariable String name,HttpServletResponse response) throws FileNotFoundException { minioUtil.uploadFile(new FileInputStream(new File("C:\\Users\\ctyc\\Desktop\\1.jpg")), "test1.jpg"); minioUtil.downloadFile("test", "test1.jpg", "t1.jpg", response); return "Hello " + name + "!"; }}
到此,關(guān)于“如何使用對(duì)象存儲(chǔ)服務(wù)Minio”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!