配置类
package top.canye.minio;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* minio 核心配置类
*/
@Configuration
@EnableConfigurationProperties(MinioProp.class)
public class MinioConfig {
@Autowired
private MinioProp minioProp;
/**
* 获取 MinioClient
*
* @return
* @throws InvalidPortException
* @throws InvalidEndpointException
*/
@Bean
public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
return new MinioClient(minioProp.getEndpoint(), minioProp.getAccesskey(), minioProp.getSecretKey());
}
}
package top.canye.minio;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* minio 属性值
*/
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProp {
/**
* 连接url
*/
private String endpoint;
/**
* 用户名
*/
private String accesskey;
/**
* 密码
*/
private String secretKey;
}
package top.canye.minio;
import com.alibaba.fastjson.JSONObject;
import io.minio.MinioClient;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@Component
public class MinioUtils {
@Autowired
private MinioClient client;
@Autowired
private MinioProp minioProp;
/**
* 创建bucket
*
* @param bucketName bucket名称
*/
@SneakyThrows
public void createBucket(String bucketName) {
if (!client.bucketExists(bucketName)) {
client.makeBucket(bucketName);
}
}
/**
* 上传文件
*
* @param file 文件
* @param bucketName 存储桶
* @return
*/
public JSONObject uploadFile(MultipartFile file, String bucketName) throws Exception {
JSONObject res = new JSONObject();
res.put("code", 0);
// 判断上传文件是否为空
if (null == file || 0 == file.getSize()) {
res.put("msg", "上传文件不能为空");
return res;
}
// 判断存储桶是否存在
createBucket(bucketName);
// 文件名
String originalFilename = file.getOriginalFilename();
// 新的文件名 = 存储桶名称_时间戳.后缀名
String fileName = bucketName + System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));
//用文件原来的名字
fileName=originalFilename;
// 开始上传
client.putObject(bucketName, fileName, file.getInputStream(), file.getContentType());
res.put("code", 1);
res.put("msg", minioProp.getEndpoint() + "/" + bucketName + "/" + fileName);
return res;
}
}
引用
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>3.0.10</version> </dependency>
minio: # endpoint: http://127.0.0.1:9002 endpoint: http://121.196.120.59:9002 accesskey: minioadmin secretKey: minioadmin
上面是我的配置,个人根据实际情况配置
测试
@Autowired
private MinioClient client;
@RequestMapping("/minio")
public void testMinio(){
try {
InputStream inputStream = client.getObject("shanghai", "2022_01_24-22_20_25 BrillouinShift.txt");
BufferedReader bdr = new BufferedReader(new InputStreamReader(inputStream));
String line ="";
int i=1;
while ((line =bdr.readLine())!= null){
System.out.println("i----"+i+"----"+line);
i++;
}
} catch (InvalidBucketNameException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
bucketName:桶的名字
objectName:文件名字
根据实际情况填写