SpringBoot整合Minio
SpringBoot整合Minio
1、添加依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.12</version>
</dependency>
2、MinioConfig
@Configuration
public class MinioConfig {
// 这是从minio生成的密钥
private static final String ACCESS_KEY = "pkRecBbz3DqdkLy6Ol6Z";
private static final String SECRET_KEY = "ZK3OelmZYjlxxxxxlWKxn4MwxxxxxxxxxH3EY";
// 注意这里是9000端口,如果自已映射了其他的要换成对应的
private static final String URL = "http://xxxx:9000";
@Bean
public MinioClient minioClient() {
return MinioClient.builder().endpoint(URL).credentials(ACCESS_KEY, SECRET_KEY).build();
}
}
3、MinioUtil
这里的bucket是在minio里面创建的桶
@Component
public class MinioUtil {
@Autowired
private MinioClient minioClient;
public void upload(InputStream inputStream, String filename, long size, String contentType) {
try {
minioClient.putObject(PutObjectArgs.builder()
.bucket("public")
.object(filename)
.stream(inputStream, size, -1)
.contentType(contentType)
.build());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public InputStream download(String filename) {
try {
return minioClient.getObject(GetObjectArgs.builder()
.bucket("public")
.object(filename)
.build()
);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void delete(String filename) {
try {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket("public")
.object(filename)
.build()
);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String preview(String filename) {
try {
return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket("public")
.object(filename)
.expiry(7, TimeUnit.DAYS)
.build()
);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
4、OSSService
@Service
public class OSSService {
@Autowired
private MinioUtil minioUtil;
public void upload(MultipartFile file) {
try {
minioUtil.upload(file.getInputStream(), file.getOriginalFilename(), file.getSize(), file.getContentType());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public InputStream download(String filename) {
return minioUtil.download(filename);
}
public void delete(String filename) {
minioUtil.delete(filename);
}
public String preview(String filename) {
return minioUtil.preview(filename);
}
}
5、OSSController
@RestController
@RequestMapping("/api/oss")
public class OSSController {
@Autowired
private OSSService ossService;
@RequestMapping("/hello")
public ResponseEntity<?> hello() {
return ResponseEntity.Success("hello world!");
}
@PostMapping("/upload")
public ResponseEntity<?> upload(@RequestParam("file") MultipartFile file) {
ossService.upload(file);
return ResponseEntity.Success("upload file success");
}
@RequestMapping("/download/{filename}")
public void download(@PathVariable String filename, HttpServletResponse response) throws IOException {
InputStream inputStream = ossService.download(filename);
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
response.setContentType("application/octet-stream");
IOUtils.copy(inputStream, response.getOutputStream());
}
@RequestMapping("/delete/{filename}")
public ResponseEntity<?> delete(@PathVariable String filename) {
ossService.delete(filename);
return ResponseEntity.Success("delete file success");
}
@RequestMapping("/preview/{filename}")
public ResponseEntity<?> preview(@PathVariable String filename) {
String url = ossService.preview(filename);
return ResponseEntity.Success(url);
}
}