文件服务器linux安装
linux下载minio安装文件
wget https://dl.minio.io/server/minio/release/linux-amd64/minio
给执行文件权限
chmod 777 minio或 chmod +x minio
linux下启动minio服务
- 设置MINIO_ACCESS_KEY,MINIO_SECERT_KEY
执行
export MINIO_ACCESS_KEY=minio
export MINIO_SECRET_KEY=miniostorage
- 非后台启动minio 服务
./minio server --console-address '0.0.0.0:9999' /data
./ 在minio启动文件所在目录
server 是启动服务命令
--console-address '0.0.0.0:9999'是控制台静态路径
/data 是文件存储目录
- 后台启动minio 服务
第二步启动的minio服务不是后台运行,后台运行是如下命令
minio后台启动命令
nohup ./minio server --console-address 0.0.0.0:9999 /data > /home/minio.log 2>&1 &
- 访问管理后台
使用 ip+:9000 访问,用户名是第一步的帐号和密码(minio和miniostorage)
java集成minio上传图片
- 引入依赖
<!-- minio 图片服务器sdk -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.2</version>
</dependency>
或
<!-- minio 图片服务器sdk -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.0.3</version>
</dependency>
下面会有两个版本的例子
- 配置yml文件
# minio 参数配置
minio:
endpoint: http://xx.xx.xx.xxx:9000
accessKey: minio
secretKey: miniostorage
# springboot 设置上传文件大小限制,默认1M 10M
spring:
servlet:
multipart:
enabled: true
maxFileSize: 500MB
maxRequestSize: 500MB
file-size-threshold: 500MB
- JAVA代码
//版本8.0.3
//MinioConfig.java
@Component
@Configuration
public class MinioConfig{
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
/**
* 注入minio 客户端
* @return
*/
@Bean
public MinioClient minioClient(){
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
//文件上传控制层
//MinioController.java
@Slf4j
@RestController
@RequestMapping(value = "minio")
public class MinioController {
@Value("${minio.endpoint}")
private String endpoint;
@Resource
private MinioClient minioClient;
/**
* 文件上传
* @param file
* @return
*/
@ResponseBody
@PostMapping("/upload")
public Map upload(MultipartFile file){
Map map = new HashMap();
try {
//获取后缀
String prefix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//文件名
String fileName = System.currentTimeMillis()+prefix;
//桶
String bucket = "image";
PutObjectArgs objectArgs = PutObjectArgs.builder().object(file.getOriginalFilename())
.bucket(bucket)
.object(fileName)
.contentType(file.getContentType())
.stream(file.getInputStream(),file.getSize(),-1).build();
minioClient.putObject(objectArgs);
map.put("state",200);
map.put("message",endpoint+"/"+bucket+"/"+fileName);
return map;
} catch (Exception e) {
e.printStackTrace();
map.put("state",500);
map.put("massage",e.getMessage());
return map;
}
}
}
//版本7.0.2
//MinIoProperties.java
@Data
@ConfigurationProperties(prefix = "minio")
public class MinIoProperties {
private String endpoint;
private String accessKey;
private String secretKey;
}
//工具类
//MinIoUtils.java
@Component
@Configuration
@EnableConfigurationProperties({MinIoProperties.class})
public class MinIoUtils {
private MinIoProperties minIo;
public MinIoUtils(MinIoProperties minIo) {
this.minIo = minIo;
}
private MinioClient instance;
@PostConstruct
public void init() {
try {
instance = new MinioClient(minIo.getEndpoint(),minIo.getAccessKey(),minIo.getSecretKey());
} catch (InvalidPortException e) {
e.printStackTrace();
} catch (InvalidEndpointException e) {
e.printStackTrace();
}
}
/**
* 判断 bucket是否存在
* @param bucketName
* @return
*/
public boolean bucketExists(String bucketName){
try {
return instance.bucketExists(bucketName);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 创建 bucket
* @param bucketName
*/
public void makeBucket(String bucketName){
try {
boolean isExist = instance.bucketExists(bucketName);
if(!isExist) {
instance.makeBucket(bucketName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
* @param bucketName
* @param objectName
* @param stream
*/
public void putObject(String bucketName, String objectName, InputStream stream){
try {
instance.putObject(bucketName,objectName,stream, new PutObjectOptions(stream.available(), -1));
} catch (Exception e) {
e.printStackTrace();
}
}
public InputStream getObject(String bucketName, String objectName){
try {
return instance.getObject(bucketName,objectName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 删除文件
* @param bucketName
* @param objectName
*/
public void removeObject(String bucketName, String objectName){
try {
instance.removeObject(bucketName,objectName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//文件操作控制层
//MinioController.java
@Slf4j
@RestController
@RequestMapping(value = "minio")
public class MinioController {
@Value("${minio.endpoint}")
private String endpoint;
@Autowired
private MinIoUtils minIoUtils;
/**
* @return
*/
@ResponseBody
@PostMapping(value = "upload")
public Map upload(String bucketName, MultipartFile file) {
Map map = new HashMap();
//获取后缀
String prefix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//文件名
String fileName = System.currentTimeMillis() + prefix;
try {
minIoUtils.putObject(bucketName,
fileName,
file.getInputStream());
map.put("state", 200);
map.put("message", endpoint + "/" + bucketName + "/" + fileName);
return map;
} catch (IOException e) {
e.printStackTrace();
map.put("state", 500);
map.put("massage", e.getMessage());
return map;
}
}
/**
* Minio文件上传
*
* @param file 文件实体
* @param bucketName 所存文件夹(桶名)
* @return
*/
@PostMapping(value = "minioUpload")
@ResponseBody
public Map minioUpload(MultipartFile file, String bucketName) {
Map<String, Object> map = new HashMap<String, Object>();
try {
boolean bucketExists = minIoUtils.bucketExists(bucketName);
if (bucketExists) {
log.info("仓库" + bucketName + "已经存在,可直接上传文件。");
} else {
minIoUtils.makeBucket(bucketName);
}
if (file.getSize() <= 20971520) {
//获取后缀
String prefix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//文件名
String fileName = System.currentTimeMillis() + prefix;
// minio仓库名
minIoUtils.putObject(bucketName,
fileName,
file.getInputStream());
log.info("成功上传文件 " + fileName + " 至 " + bucketName);
String fileUrl = bucketName + "/" + fileName;
Map<String, Object> map = new HashMap<String, Object>();
map.put("fileUrl", fileUrl);
map.put("bucketName", bucketName);
map.put("originFileName", fileName);
map.put("fileUrl", fileUrl);
map.put("bucketName", bucketName);
map.put("originFileName", fileName);
} else {
throw new Exception("请上传小于20mb的文件");
}
} catch (Exception e) {
e.printStackTrace();
if (e.getMessage().contains("ORA")) {
map.put("state","400");
map.put("message","上传失败:【查询参数错误】");
return map;
}
map.put("state","400");
map.put("message","上传失败:【" + e.getMessage() + "】");
return map;
}
}
/**
* 删除文件
*
* @param bucketName
* @param fileName
* @return
*/
@ResponseBody
@PostMapping(value = "delete")
public Map delete(String bucketName, String fileName) {
Map map = new HashMap();
minIoUtils.removeObject(bucketName, fileName);
map.put("state", 200);
map.put("message", "删除成功");
return map;
}
/**
* 下载文件
* @param objectName
* @param bucketName
* @param response
* @return
*/
@GetMapping(value = "download")
public Map downloadFile(String objectName,String bucketName, HttpServletResponse response) {
Map map = new HashMap();
try {
InputStream file = minIoUtils.getObject(bucketName,objectName);
String filename = new String(objectName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
ServletOutputStream servletOutputStream = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while((len=file.read(buffer)) > 0){
servletOutputStream.write(buffer, 0, len);
}
servletOutputStream.flush();
file.close();
servletOutputStream.close();
map.put("state","200");
map.put("message",objectName + "下载成功");
return map;
} catch (Exception e) {
e.printStackTrace();
if (e.getMessage().contains("ORA")) {
map.put("state","400");
map.put("message","下载失败:【查询参数错误】");
return map;
}
map.put("state","400");
map.put("message","下载失败:【" + e.getMessage() + "】");
return map;
}
}
/**
* 检查桶是否存在
*
* @param bucketName
* @return
*/
@ResponseBody
@PostMapping(value = "checkBucket")
public boolean checkBucket(String bucketName) {
return minIoUtils.bucketExists(bucketName);
}
- 可以通过控制台去查看实际桶和桶内文件情况