MinIO实现文件上传存储
短信预约 -IT技能 免费直播动态提醒
MinIO
目录
MinIO概念
- MinIO 是在 GNU Affero 通用公共许可证 v3.0 下发布的高性能对象存储。它与 Amazon S3 云存储服务 API 兼容。使用 MinIO 为机器学习、分析和应用程序数据工作负载构建高性能基础架构。
- MinIO是高性能对象存储,什么是对象存储(Object Storage Service),对象存储是支持海量用户远程访问的无限容量廉价存储系统,即是存储系统。
- 它由桶(bucket,对应Windows下的文件夹),组成目录结构,桶中直接存放对象(Object,对应Windwos下的文件),桶中不能再创建桶,但是能创建文件夹。
MinIO下载安装启动
访问官网:https://docs.min.io/
进入官网点击右上角Download
下载
点击Windows下载服务端
安装
将MinIO.exe 拷贝到自己的目录下去,并创建一个data目录
启动
通过命令行启动Minio :
minio server ./data
启动完成后,可以看到访问的路径:http://127.0.0.1:9000
使用提供的账号密码进行登录
账号:minioadmin
密码:minioadmin
MinIO实现上传文件
导入maven坐标
io.minio minio 8.4.3
在application文件添加配置
# MinIO配置minio: endpoint: 127.0.0.1 port: 9000 accessKey: minioadmin secretKey: minioadmin secure: false bucketName: "demo" configDir: "/data/excel"
实现代码
@Data@Component@ConfigurationProperties(prefix = "minio")public class MinioConfig { private final static String HTTP = "http://"; //endPoint是一个URL,域名,IPv4或者IPv6地址 private String endpoint; //TCP/IP端口号 private int port; //accessKey类似于用户ID,用于唯一标识你的账户 private String accessKey; //secretKey是你账户的密码 private String secretKey; //如果是true,则用的是https而不是http,默认值是true private Boolean secure; //默认存储桶 private String bucketName;}
public class AjaxResult extends HashMap{ private static final long serialVersionUID = 1L; public static final String CODE_TAG = "code"; public static final String MSG_TAG = "msg"; public static final String DATA_TAG = "data"; public AjaxResult() { } public AjaxResult(int code, String msg) { super.put(CODE_TAG, code); super.put(MSG_TAG, msg); } public AjaxResult(int code, String msg, Object data) { super.put(CODE_TAG, code); super.put(MSG_TAG, msg); if (StringUtils.isNotNull(data)) { super.put(DATA_TAG, data); } } public static AjaxResult success() { return AjaxResult.success("操作成功"); } public static AjaxResult success(Object data) { return AjaxResult.success("操作成功", data); } public static AjaxResult success(String msg) { return AjaxResult.success(msg, null); } public static AjaxResult success(String msg, Object data) { return new AjaxResult(HttpStatus.SUCCESS, msg, data); } public static AjaxResult error() { return AjaxResult.error("操作失败"); } public static AjaxResult error(String msg) { return AjaxResult.error(msg, null); } public static AjaxResult error(String msg, Object data) { return new AjaxResult(HttpStatus.ERROR, msg, data); } public static AjaxResult error(int code, String msg) { return new AjaxResult(code, msg, null); }}
@RestControllerpublic class CommonController extends BaseController { private static final Logger log = LoggerFactory.getLogger(CommonController.class); @Autowired private ISysFileService fileService; @PostMapping("/common/upload") public AjaxResult uploadFile(MultipartFile file){ try{ // 上传文件路径 return fileService.upload(file); }catch (Exception e){ return AjaxResult.error(e.getMessage()); } }}
public interface ISysFileService {AjaxResult upload(MultipartFile file);}
@Service@Slf4jpublic class SysFileServiceImpl implements ISysFileService{@AutowiredMinioConfig minioConfig;@Overridepublic AjaxResult upload(MultipartFile file) {InputStream inputStream = null;//创建Minio的连接对象MinioClient minioClient = getClient();//获取存储桶String bucketName = minioConfig.getBucketName();try {inputStream = file.getInputStream();// Make bucketName bucket if not exist.boolean found =minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());if (!found) {// Make a new bucket called bucketName.minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());} else {log.info("Bucket " +"["+bucketName+ "]"+" already exists.");}//Uploads given stream as object in bucket.String oldFileName = file.getOriginalFilename();String objectName = new SimpleDateFormat("yyyy/MM/dd/").format(new Date())+IdUtils.simpleUUID()+oldFileName.substring(oldFileName.lastIndexOf("."));minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, file.getSize(), -1).contentType(file.getContentType()).build());AjaxResult ajax = AjaxResult.success();ajax.put("fileName", "/"+bucketName+"/"+objectName);ajax.put("url", "http://"+minioConfig.getEndpoint()+":"+minioConfig.getPort()+"/"+minioConfig.getBucketName()+"/"+objectName);return ajax;}catch(Exception e){e.printStackTrace();return AjaxResult.error("上传失败");}finally {//防止内存泄漏if (inputStream != null) {try {inputStream.close(); // 关闭流} catch (IOException e) {log.debug("inputStream close IOException:" + e.getMessage());}}}}private MinioClient getClient(){MinioClient minioClient =MinioClient.builder().endpoint("http://"+minioConfig.getEndpoint()+":"+ minioConfig.getPort()).credentials(minioConfig.getAccessKey(),minioConfig.getSecretKey()).build();return minioClient;}}
响应数据格式
{"msg":"操作成功","fileName":"/demo/2022/10/14/efcb6266efba42a4b35344181964be36.pdf","code":200,"url":"http://127.0.0.1:9000/demo/2022/10/14/efcb6266efba42a4b35344181964be36.pdf"}
到此,上传文件到MinIO服务器就完成了。
来源地址:https://blog.csdn.net/m0_52388228/article/details/127319556
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341