springboot中如何使用minio存儲(chǔ)容器
docker運(yùn)行
docker run -p 9000:9000 -p 9001:9001 -v /mydata/minio/data:/data minio/minio server /data --console-address ":9001
java導(dǎo)包
最好是這個(gè)版本,其他版本嘗試過都出bug了
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.2.1</version> </dependency>
配置文件
spring: # 上傳文件大小設(shè)置 servlet: multipart: enabled: true max-file-size: 50MB minio: endpoint: xxx:9000 accesskey: xxx secretkey: xxx bucketName: xxx
操作
1、編寫一個(gè)屬性文件
@Data @Component @ConfigurationProperties(prefix = "minio") // 從配置文件的前綴拿 public class MinioProperties { private String endpoint; private String accessKey; private String secretKey; }
2、編寫一個(gè)minioClient
@Configuration public class MinioConfig { @Resource private MinioProperties minioProperties; @Bean public MinioClient minioClient() { System.out.println(minioProperties.getAccessKey()); System.out.println(minioProperties.getSecretKey()); MinioClient minioClient = MinioClient.builder() .endpoint(minioProperties.getEndpoint()) .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey()) .build(); return minioClient; } }
3、上傳文件Api
public class MinioServiceImpl implements MinioService { @Value("${minio.bucketName}") private String bucketName; @Value("${minio.endpoint}") private String endPoint; @Resource private MinioClient minioClient; @Override public List<String> uploadFile(MultipartFile[] file) throws ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException { if (file == null || file.length == 0) { throw new APIException(ResultCode.PARAM_IS_BLANK); } List<String> fileUrlList = new ArrayList<>(file.length); String url = ""; for (MultipartFile multipartFile : file) { // 1.獲取文件名 String originalFilename = multipartFile.getOriginalFilename(); // 2.截取后綴名 String imgSuffix = originalFilename.substring(originalFilename.lastIndexOf(".")); // 3.生成唯一名 String newFileName = UUID.randomUUID().toString() + imgSuffix; // 4.日期目錄 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); String dataPath = dateFormat.format(new Date()); // 5.合成路徑 String finalFileName = dataPath + "/" + newFileName; // 別忘了bucketName url = endPoint + "/" + bucketName + "/" + finalFileName; try { // 文件上傳 InputStream in = multipartFile.getInputStream(); minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(finalFileName).stream( in, multipartFile.getSize(), -1) .contentType(multipartFile.getContentType()) .build()); in.close(); fileUrlList.add(url); } catch (IOException e) { throw new APIException(ResultCode.COMMON_FAIL); } } return fileUrlList; } }
本地瀏覽設(shè)置
通過上面這串url就可以直接訪問圖片了
總結(jié)
到此這篇關(guān)于springboot中如何使用minio存儲(chǔ)容器的文章就介紹到這了,更多相關(guān)springboot minio存儲(chǔ)容器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)手機(jī)短信驗(yàn)證的基本思路
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)手機(jī)短信驗(yàn)證的基本思路,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11springboot響應(yīng)json?null值過濾方式
這篇文章主要介紹了springboot響應(yīng)json?null值過濾方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11java實(shí)現(xiàn)微信公眾平臺(tái)自定義菜單的創(chuàng)建示例
這篇文章主要介紹了java實(shí)現(xiàn)微信公眾平臺(tái)自定義菜單的創(chuàng)建示例,需要的朋友可以參考下2014-04-04springmvc使用REST出現(xiàn):Request?method?'PUT'?not?sup
這篇文章主要介紹了springmvc使用REST出現(xiàn):Request?method?'PUT'?not?supported問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02使用Lombok時(shí)@JsonIgnore注解失效解決方案
這篇文章主要為大家介紹了使用Lombok時(shí)@JsonIgnore注解失效問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06SpringBoot使用編程方式配置DataSource的方法
這篇文章主要介紹了SpringBoot使用編程方式配置DataSource的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01