docker运行
docker run
-p 9000:9000
-p 9001:9001
-v /mydata/minio/data:/data
minio/minio server /data --console-address ":9001
java导包
最好是这个版本,其他版本尝试过都出bug了
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
配置文件
spring:
# 上传文件大小设置
servlet:
multipart:
enabled: true
max-file-size: 50MB
minio:
endpoint: xxx:9000
accesskey: xxx
secretkey: xxx
bucketName: xxx
操作
1、编写一个属性文件
@Data
@Component
@ConfigurationProperties(prefix = "minio") // 从配置文件的前缀拿
public class MinioProperties {
private String endpoint;
private String accessKey;
private String secretKey;
}
2、编写一个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;
}
}
本地浏览设置
通过上面这串url就可以直接访问图片了
总结
到此这篇关于springboot中如何使用minio存储容器的文章就介绍到这了,更多相关springboot minio存储容器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!