最新版8.3的api已经不兼容了,所以发一版供大家参考
============================== pom.xml =================================
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>minio</artifactId>
<version>0.1.1</version>
<dependencies>
<!--springboot-starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<properties>
<Java.version>1.8</Java.version>
<project.build.sourceEndcoding>UTF-8</project.build.sourceEndcoding>
<maven.compliter.source>1.8</maven.compliter.source>
<maven.compliter.target>1.8</maven.compliter.target>
<lombok.version>1.18.8</lombok.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--<configuration>
<skip>true</skip>
</configuration>-->
</plugin>
</plugins>
</build>
</project>
=========================== application.yml ===========================
server:
port: 8088
#minio配置
minio:
endpoint: http://127.0.0.1:9000
access-key: admin
secret-key: admin123
======================miniOconfig======================
package com.wangjian.demo.config;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MinioConfig {
/**
* endPoint是一个URL,域名,IPv4或者IPv6地址
*/
@Value("${minio.endpoint}")
private String endpoint;
/**
* accessKey类似于用户ID,用于唯一标识你的账户
*/
@Value("${minio.access-key}")
private String accessKey;
/**
* secretKey是你账户的密码
*/
@Value("${minio.secret-key}")
private String secretKey;
@Bean
public MinioClient getMinClient() {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
=======================MinioHelper 工具类
=================================
package com.wangjian.demo.utils;
import io.minio.*;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
@Slf4j
@Component
public class MinioHelper {
@Resource
private MinioClient minioClient;
public void checkBucket(String bucket) throws Exception {
try {
// Make 'bucket1' bucket if not exist.
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
if (!found) {
// Make a new bucket called 'bucket1'.
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
} else {
log.info("Bucket {} already exists", bucket);
}
} catch (MinioException e) {
log.error("Error occurred: " + e);
log.error("HTTP trace: " + e.httpTrace());
}
}
public void putKnownSizeObj(InputStream inputStream, long size) throws Exception {
// Upload known sized input stream.
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("my-objectname").stream(
inputStream, size, -1)
.contentType("video/mp4")
.build());
}
public void putUnknownSizeObj(String bucket, String objectName, InputStream inputStream, String contentType) throws Exception {
checkBucket(bucket);
// Upload unknown sized input stream.
minioClient.putObject(
PutObjectArgs.builder().bucket(bucket).object(objectName).stream(
inputStream, -1, 10485760)
.contentType(contentType)
.build());
}
public void makeDir() throws Exception {
// Create object ends with '/' (also called as folder or directory).
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("path/to/").stream(
new ByteArrayInputStream(new byte[]{}), 0, -1)
.build());
}
public void uploadFile(MinioClient minioClient) throws Exception {
// Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
// 'bucket1'.
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket("bucket1")
.object("1.jpg")
.filename("D:\\1.jpg")
.build());
System.out.println("is successfully uploaded as object to bucket 'bucket1'.");
}
public void getObject(String bucket, String objectName) throws Exception {
// get object given the bucket and object name
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucket)
.object(objectName)
.build())) {
// Read data from stream
}
}
public void downloadFile(String bucket, String objectName, String fileName) throws Exception {
// Download object given the bucket, object name and output file name
minioClient.downloadObject(
DownloadObjectArgs.builder()
.bucket(bucket)
.object(objectName)
.filename(fileName)
.build());
}
public InputStream getObject(String bucket, String objectName, long offset) throws Exception {
// get object data from offset
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucket)
.object(objectName)
.offset(offset)
.build())) {
// Read data from stream
return stream;
}
}
}
=======================TestController
===============================
package com.wangjian.demo.controller; import com.wangjian.demo.utils.MinioHelper; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URLEncoder; import java.util.UUID; @RestController @RequestMapping("/test") public class TestController { private static Logger log = LoggerFactory.getLogger(TestController.class); @Resource private MinioHelper minioHelper; @RequestMapping("/downloadFile") public void downloadFile() throws Exception { minioHelper.downloadFile("bucket1", "my-objectname1", "src/" + UUID.randomUUID().toString().substring(24) + ".jpg"); } @RequestMapping("/putUnknownSizeObj") public void putUnknownSizeObj() throws Exception { minioHelper.putUnknownSizeObj("bucket1", "my-objectname2", new FileInputStream(new File("D:\\1.jpg")), "image/jpeg"); } @RequestMapping("/getObject") public void getObject(HttpServletResponse response) throws Exception { InputStream in = null; in = minioHelper.getObject("bucket1", "my-objectname2"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("my-objectname2.jpg", "UTF-8")); response.setCharacterEncoding("UTF-8"); //将字节从InputStream复制到OutputStream 。 IOUtils.copy(in, response.getOutputStream()); } @RequestMapping("/getObjec1t") public void getObject( ) throws Exception { InputStream in = minioHelper.getObject("bucket1", "my-objectname2"); System.out.println(in.available()); //将字节从InputStream复制到OutputStream 。 FileOutputStream outputStream = new FileOutputStream(new File("src/333.jpg")); IOUtils.copy(in, outputStream); outputStream.flush(); } }
========================ApplicationRun
启动类===============================
package com.wangjian.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class ApplicationRun {
public static SpringApplication application;
public static ConfigurableApplicationContext context;
public static String[] args;
public static void main(String[] args) {
application = new SpringApplication(ApplicationRun.class);
ApplicationRun.args = args;
context = application.run(args);
// SpringApplication.run(ApplicationRun.class,args);
}
}