一.minio文件服务搭建
非docker环境部署(Linux部署)
1.官网下载安装包:MinIO | Code and downloads to create high performance object storage
上传安装包文件到目录(这个可以自由选择)
/home/minio/
并创建data文件夹与在data下创建minio.log文件
2. 添加操作权限
chmod +x minio
用户名跟密码非必需执行的代码,如果不需要可以直接执行第3步.
# 自定义设置用户名(默认minioadmin)
export MINIO_ACCESS_KEY=admin
# 自定义设置密码(默认minioadmin)
export MINIO_SECRET_KEY=123456
3.设置后台服务启动(9005是后台登录页面,9006是webapi接口端口,用于服务调用)
nohup ./minio server --console-address :9005 --address :9006 /home/minio/data > /home/minio/data/minio.log 2>&1 &
4. 防火墙开通端口(非必需,nginx跟防火墙端口任意选一个)
firewall-cmd --add-port=9005/tcp --permanent
firewall-cmd --add-port=9006/tcp --permanent
firewall-cmd --reload
或者nginx代理
二、Springboot项目配置
1.yml配置
minio:
url: http://bd.baidu123.xyz
accessKey: 1234567890
secretKey: 1234567890
bucketName: myjlis
注释:
url:就是api接口url地址的nginx代理地址,你也可以直接填ip+9006
accessKey与secretKey的值取自:后台管理平台(ip+9005后台平台地址)
bucketName创建的桶名
maven依赖:
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.2</version>
</dependency>
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author :jerry
* @date :Created in 2022/9/6 10:32
* @description:bean配置类
* @version: V1.1
*/
@Data
@ConfigurationProperties(prefix = "minio")
public class MinioPro {
/**
* 端点
*/
private String url;
/**
* 用户名
*/
private String accesskey;
/**
* 密码
*/
private String secretKey;
/**
* 桶名称
*/
private String bucketName;
}
import com.myj.common.MinioPro;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
* @author :jerry
* @date :Created in 2022/9/6 10:31
* @description:minio
* @version: V1.1
*/
@Configuration
@EnableConfigurationProperties(MinioPro.class)
public class MinioConfiguration {
@Resource
private MinioPro minioPro;
/**
* 初始化 MinIO 客户端
*/
@Bean
public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
return new MinioClient(minioPro.getUrl(), minioPro.getAccesskey(), minioPro.getSecretKey());
}
}
import java.io.IOException;
/**
* @author :jerry
* @date :Created in 2022/9/6 08:55
* @description:
* @version: V1.1
*/
public interface MinioService {
/**上传文件*/
String upload(byte[] base64Str);
/**删除文件*/
String delete(String fileName);
/**获取文件url预览*/
String getFileUrl(String objectName);
/**获取文件流*/
byte[] uploadFile(String objectName) throws IOException;
}
import com.myj.common.MinioPro;
import com.myj.service.MinioService;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
/**
* @author :jerry
* @date :Created in 2022/9/6 09:06
* @description:
* @version: V1.1
*/
@Slf4j
@Service
public class MinioServiceImpl implements MinioService {
@Resource
private MinioPro minioPro;
@Autowired
private MinioClient minioClient;
/**base64编码的图片上传*/
@Override
public String upload(byte[] base64Str) {
//oss是前端传来的base64对象,第一行代码将获取对象中的base64字符串
try {
InputStream byteArrayInputStream = new ByteArrayInputStream(base64Str);
boolean bucketExists = minioClient.bucketExists(minioPro.getBucketName());
if (!bucketExists) {
minioClient.makeBucket(minioPro.getBucketName());
}
// 生成文件名称
String nameSuffix = UUID.randomUUID().toString() + ".jpg";
// 上传配置
PutObjectOptions options = new PutObjectOptions(byteArrayInputStream.available(), PutObjectOptions.MIN_MULTIPART_SIZE);
// options.setContentType("image/jpeg");
options.setContentType("image/jpg");
minioClient.putObject(minioPro.getBucketName(), nameSuffix, byteArrayInputStream, options);
return nameSuffix;
} catch (Exception e) {
log.error("上传文件失败", e.getMessage());
return "上传文件失败";
}
}
// @Override
// public void show(String fileName, HttpServletResponse response) {
// InputStream in = null;
// OutputStream out = null;
// try {
// MinioClient minioClient = new MinioClient(minioPro.getUrl(), minioPro.getAccesskey(), minioPro.getSecretKey());
// in = minioClient.getObject(minioPro.getBucketName(), fileName);
// int length = 0;
// byte buf[] = new byte[1024];
// out = response.getOutputStream();
// response.reset();
// //Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显 示附加的文件。
// // Content-disposition其实可以控制用户请求所得的内容存为一个文件的时候提供一个默认的文件名,
// // 文件直接在浏览器上显示或者在访问时弹出文件下载对话框。
// response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
// response.setCharacterEncoding("utf-8");
// while ((length = in.read(buf)) > 0) {
// out.write(buf, 0, length);
// }
// } catch (Exception ex) {
// ex.printStackTrace();
// } finally {
// if (in != null) {
// try {
// in.close();
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
// if (out != null) {
// try {
// out.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// }
/**
* 删除文件
*/
@Override
public String delete(String fileName) {
try {
minioClient.removeObject(minioPro.getBucketName(), fileName);
} catch (Exception e) {
throw new RuntimeException(e);
}
return "删除成功";
}
/**
* 获取文件url预览
*/
@Override
public String getFileUrl(String objectName) {
try {
return minioClient.getObjectUrl(minioPro.getBucketName(), objectName);
} catch (ErrorResponseException e) {
throw new RuntimeException(e);
} catch (InsufficientDataException e) {
throw new RuntimeException(e);
} catch (InternalException e) {
throw new RuntimeException(e);
} catch (InvalidBucketNameException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (InvalidResponseException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (XmlParserException e) {
throw new RuntimeException(e);
}
}
/**
* 获取文件流
*/
@Override
public byte[] uploadFile(String objectName) throws IOException {
InputStream uploadFile = null;
try {
uploadFile = minioClient.getObject(minioPro.getBucketName(), objectName);
} catch (ErrorResponseException e) {
throw new RuntimeException(e);
} catch (InsufficientDataException e) {
throw new RuntimeException(e);
} catch (InternalException e) {
throw new RuntimeException(e);
} catch (InvalidBucketNameException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (InvalidResponseException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (XmlParserException e) {
throw new RuntimeException(e);
}
//流转字节码数组
//byte[] bytes = uploadFile.readAllBytes();\
//return bytes;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = uploadFile.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
}
}
如果需要通过域名➕桶名➕文件名方式访问需要进行如下设置