前言:
确保已经安装了minio的服务端
代码:
pom.xml
1 2 3 4 5 |
io.minio minio 7.0.2
|
|
1 配置类
创建项目 操作 MinIOpom.xml 相关依赖
com.alibaba
fastjson
LATEST
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
io.minio
minio
7.0.1
commons-io
commons-io
2.6
编辑配置文件application.properties修改MinIO相关配置
server.port=80
spring.application.name=book-minio
spring.thymeleaf.cache=false
spring.servlet.multipart.max-file-size = 10MB
spring.servlet.multipart.max-request-size=100MB
minio.endpoint=http://192.168.1.6:9000
minio.accesskey=minio
minio.secretKey=Aa123456
连接 MinIO 配置
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@ConfigurationProperties(prefix = “minio”)
@Component
public class MinioProp {
private String endpoint;
private String accesskey;
private String secretKey;
}
创建 MinioClient
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MinioConfiguration {
@Autowired
private MinioProp minioProp;
@Bean
public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
MinioClient client = new MinioClient(minioProp.getEndpoint(), minioProp.getAccesskey(), minioProp.getSecretKey());
return client;
}
}
MinIO 查看桶列表,存入,删除 操作 MinioController
import com.alibaba.fastjson.JSON;
import com.lab.book.minio.common.Res;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.minio.Result;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.;
@Slf4j
@RestController
public class MinioController {
@Autowired
private MinioClient minioClient;
private static final String MINIO_BUCKET = “mybucket”;
@GetMapping(“/list”)
public List