您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“springboot中怎么使用minio存儲(chǔ)容器”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“springboot中怎么使用minio存儲(chǔ)容器”這篇文章吧。
docker run -p 9000:9000 -p 9001:9001 -v /mydata/minio/data:/data minio/minio server /data --console-address ":9001
最好是這個(gè)版本,其他版本嘗試過(guò)都出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、編寫(xiě)一個(gè)屬性文件
@Data @Component @ConfigurationProperties(prefix = "minio") // 從配置文件的前綴拿 public class MinioProperties { private String endpoint; private String accessKey; private String secretKey; }
2、編寫(xiě)一個(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; } }
通過(guò)上面這串url就可以直接訪問(wèn)圖片了
以上是“springboot中怎么使用minio存儲(chǔ)容器”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。