赞
踩
#拉取minio最新版
docker pull minio/minio
#运行minio
docker run -d -p 9000:9000 -p 9001:9001 --name minio -v /data/minio-data:/data -e "MINIO_ROOT_USER=admin" -e "MINIO_ROOT_PASSWORD=admin@20220331" minio/minio server /data --console-address ":9001"
#查看日志
docker logs minio
#访问minio控制台 http:ip:9001
<!-- minio依赖 -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.0.3</version>
</dependency>
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioPro {
/**
* 端点
*/
private String endpoint;
/**
* 用户名
*/
private String accessKey;
/**
* 密码
*/
private String secretKey;
/**
* 桶名称
*/
private String bucketName;
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
}
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MinIoClientConfig {
@Autowired
private MinioPro minioPro;
/**
* 注入minio 客户端
* @return
*/
@Bean
public MinioClient minioClient(){
return MinioClient.builder()
.endpoint(minioPro.getEndpoint())
.credentials(minioPro.getAccessKey(), minioPro.getSecretKey())
.build();
}
}
import com.lgh.miniospringboot.config.MinioPro;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
@RestController
@RequestMapping("/minio")
@Validated
@Slf4j
public class FileUploadController {
@Autowired
private MinioPro minioPro;
@PostMapping("test")
public String pub(){
return minioPro.toString();
}
@Resource
private MinioClient minioClient;
/**
* 文件上传
* @param file
* @return
*/
@PostMapping("/upload")
public String upload(MultipartFile file){
try {
PutObjectArgs objectArgs = PutObjectArgs.builder().object(file.getOriginalFilename())
.bucket(minioPro.getBucketName())
.contentType(file.getContentType())
.stream(file.getInputStream(),file.getSize(),-1).build();
minioClient.putObject(objectArgs);
return "ok";
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
}
返回ok成功,上传成功。
注意:配置文件application.yml的书写文件大小的限制
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。