【RuoYi-Vue-Plus】学习笔记 02 - OSS模块(二)之文件上传(使用MinIO基于Win10环境)

一、概述

本篇文章主要内容是 RuoYi-Vue-Plus 框架中的OSS模块的文件上传内容。

二、功能

0、前提

关于OSS模块使用:文档说明地址

1、Web端页面

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、MinIO页面

在这里插入图片描述

3、本地文件

在这里插入图片描述

三、MinIO安装使用(基于Win10)

0、前提

关于MinIO:
A. 官方网站
B. 中文文档

1、MinIO安装使用

1.1、文件目录

store是自定义的存储路径。
在这里插入图片描述

1.2、版本

在这里插入图片描述

1.3、运行

根据命令行运行:

minio.exe server  D:\dev\minio\store --console-address "127.0.0.1:9000"  --address "127.0.0.1:9090"

指定存储路径 D:\dev\minio\store
指定控制台路径 --console-address "127.0.0.1:9000"
指定API路径 --address "127.0.0.1:9090"

注:没有指定路径登录会报错。
在这里插入图片描述
此时可以通过 http://127.0.0.1:9000 登录控制台。账号密码也已经展示在CMD中。
在这里插入图片描述
登录成功界面如下。
在这里插入图片描述

1.4、可能出现的错误

运行如下命令:

minio.exe server  D:\dev\minio\store 

出现如下结果:
在这里插入图片描述
无法登录页面:
在这里插入图片描述
一开始测试的时候,根据网上的教程没有指定路径,导致无法登录(可能是版本原因)。因此在运行时需要指定路径。

2、配置修改

2.1、修改 MinIO 控制台账号密码

修改文件config.json。
在这里插入图片描述
修改文件中 access_key 以及 secret_key 的值即可。
在这里插入图片描述

2.2、修改项目配置

注:项目配置需要与 MinIO 运行配置匹配,否则无法上传。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、代码实现

上传方法:SysOssController#upload()
	/**
     * 上传OSS对象存储
     */
    @ApiOperation("上传OSS对象存储")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "java.io.File", required = true),
    })
    @PreAuthorize("@ss.hasPermi('system:oss:upload')")
    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/upload")
    public AjaxResult<Map<String, String>> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        SysOss oss = iSysOssService.upload(file);
        Map<String, String> map = new HashMap<>(2);
        map.put("url", oss.getUrl());
        map.put("fileName", oss.getFileName());
        return AjaxResult.success(map);
    }
上传实现类:SysOssServiceImpl#upload()
	@Override
    public SysOss upload(MultipartFile file) {
    	// 获取文件原名称
        String originalfileName = file.getOriginalFilename();
        // 获取文件后缀
        String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
        // 获取存储实例
        ICloudStorageStrategy storage = OssFactory.instance();
        UploadResult uploadResult;
        try {
        	// 文件上传
            uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
        } catch (IOException e) {
            throw new ServiceException(e.getMessage());
        }
        // 保存文件信息
        SysOss oss = new SysOss()
                .setUrl(uploadResult.getUrl())
                .setFileSuffix(suffix)
                .setFileName(uploadResult.getFilename())
                .setOriginalName(originalfileName)
                .setService(storage.getServiceType());
        save(oss);
        return oss;
    }
获取存储实例:OssFactory#instance()
	/**
	 * 获取默认实例
	 */
	public static ICloudStorageStrategy instance() {
		// 获取redis 默认类型
		String type = Convert.toStr(RedisUtils.getCacheObject(CloudConstant.CACHE_CONFIG_KEY));
		if (StringUtils.isEmpty(type)) {
			throw new OssException("文件存储服务类型无法找到!");
		}
		return instance(type);
	}

	/**
	 * 根据类型获取实例
	 */
	public static ICloudStorageStrategy instance(String type) {
		ICloudStorageStrategy service = SERVICES.get(type);
		if (service == null) {
			refreshService(type);
			service = SERVICES.get(type);
		}
		return service;
	}

	private static void refreshService(String type) {
		Object json = RedisUtils.getCacheObject(CloudConstant.SYS_OSS_KEY + type);
		CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class);
		if (properties == null) {
			throw new OssException("系统异常, '" + type + "'配置信息不存在!");
		}
		// 获取redis配置信息 创建对象 并缓存
		ICloudStorageStrategy service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type));
		((AbstractCloudStorageStrategy)service).init(properties);
		SERVICES.put(type, service);
	}
Redis中实例缓存信息:

在这里插入图片描述
在这里插入图片描述

MinIO 文件上传业务实现:MinioCloudStorageStrategy#uploadSuffix()

在这里插入图片描述

	@Override
	public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
		return upload(data, getPath(properties.getPrefix(), suffix), contentType);
	}

	@Override
	public UploadResult upload(byte[] data, String path, String contentType) {
		return upload(new ByteArrayInputStream(data), path, contentType);
	}

	@Override
	public UploadResult upload(InputStream inputStream, String path, String contentType) {
		try {
			minioClient.putObject(PutObjectArgs.builder()
				.bucket(properties.getBucketName())
				.object(path)
				.contentType(StringUtils.blankToDefault(contentType, MediaType.APPLICATION_OCTET_STREAM_VALUE))
				.stream(inputStream, inputStream.available(), -1)
				.build());
		} catch (Exception e) {
			throw new OssException("上传文件失败,请核对Minio配置信息:[" + e.getMessage() + "]");
		}
		return new UploadResult().setUrl(getEndpointLink() + "/" + path).setFilename(path);
	}
  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
如果想将ruoyi-vue-plus项目中的文件至本地,可以按照以下步骤进行操作: 1. 首先,确保你已经成功搭建并运行了ruoyi-vue-plus项目,保证项目正常运行。 2. 在ruoyi-vue-plus项目的前端代码中,找到与文件相关的功能模块。通常,文件的代码会位于项目的"src/views"目录或者相关子目录中。 3. 打开相关文件,找到文件的逻辑代码。一般来说,文件功能会使用到Vue的组件和相关的API。你可以通过搜索关键词如"文件"、"upload"等来快速定位到相关代码。 4. 配置上的目标路径。一般情况下,上文件会被保存在服务器的某个目录下。你需要查阅ruoyi-vue-plus项目的相关文档或代码,找到文件的目标路径配置项,一般是在项目的配置文件中。 5. 修改目标路径为本地路径。将目标路径的配置项修改为你想要保存文件的本地路径。确保该路径是本地文件系统中存在的,并且具备操作权限。 6. 保存修改并重新编译运行ruoyi-vue-plus项目。确保项目启动成功。 7. 现在,当你进行文件操作时,文件将会被上至你所配置的本地路径中。 请注意,将文件至本地需要确保你的本地环境拥有足够的存储空间和相关权限。另外,该操作可能会导致项目的其他功能无法正常运行,请谨慎操作,并备份好原先的项目文件
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MichelleChung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值