对象存储服务(Object Storage Service,OSS)是一种海量、安全、低成本、高可靠的云存储服务,适合存放任意类型的文件。容量和处理能力弹性扩展,多种存储类型供选择,全面优化存储成本。
大家如果对一些基础知识不太熟悉的话,就直接看下面的文档
第一步:引入OSS相关依赖文件
修改文件: ruoyi-common -> pom.xml
<!-- 阿里云OSS -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
第二步:编写与引入配置文件
修改文件: ruoyi-admin -> src.main.resource > application.yml
# 阿里云对象存储配置
# 把下面配置直接追加到配置文件中,修改对应的值即可
aliyunoss:
# 地域节点
endpoint: oss-cn-beijing.aliyuncs.com
# AccessKey
accessKeyId: LTAI5tQYvvT**********
# AccessKey 秘钥
accessKeySecret: XJfMBaXqKWBnTGDko**********
# bucket名称
bucketName: zouhu***
# bucket下文件夹的路径
filehost: WisdomLaundry
# 访问域名
url: https://zouhuuoss.***.com
新增文件:ruoyi-common -> src > main > java > com.ruoyi.common > config > AliyunOssConfig.java
package com.ruoyi.common.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* @author zouhuu
* @description 阿里云对象存储 配置
* @date 2022/06/16 14:08:21
*/
@Component
@ConfigurationProperties(prefix = "aliyunoss")
public class AliyunOssConfig {
/**
* 地域节点
*/
private String endpoint;
/**
* AccessKey
*/
private String accessKeyId;
/**
* AccessKey秘钥
*/
private String accessKeySecret;
/**
* bucket名称
*/
private String bucketName;
/**
* bucket下文件夹的路径
*/
private String filehost;
/**
* 访问域名
*/
private String url;
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getAccessKeyId() {
return accessKeyId;
}
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
public String getAccessKeySecret() {
return accessKeySecret;
}
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
public String getFilehost() {
return filehost;
}
public void setFilehost(String filehost) {
this.filehost = filehost;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "AliyunOssConfig{" +
"endpoint='" + endpoint + '\'' +
", accessKeyId='" + accessKeyId + '\'' +
", accessKeySecret='" + accessKeySecret + '\'' +
", bucketName='" + bucketName + '\'' +
", filehost='" + filehost + '\'' +
", url='" + url + '\'' +
'}';
}
}
第三步:实现上传逻辑
新增文件:ruoyi-common -> src > main > java > com.ruoyi.common > util > file > AliyunOssUploadUtils.java
package com.ruoyi.common.utils.file;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.ruoyi.common.config.AliyunOssConfig;
import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* @author zouhuu
* @description 阿里云对象存储上传工具类
* @date 2022/06/16 14:21:12
*/
@Component
public class AliyunOssUploadUtils {
private static AliyunOssConfig aliyunOssConfig;
/**
* 使用构造方法注入配置信息
*/
@Autowired
public AliyunOssUploadUtils(AliyunOssConfig aliyunOssConfig) {
AliyunOssUploadUtils.aliyunOssConfig = aliyunOssConfig;
}
/**
* 上传文件
* @param file
* @return
* @throws Exception
*/
public static String uploadFile(MultipartFile file) throws Exception {
// 生成 OSSClient
OSS ossClient = new OSSClientBuilder().build(aliyunOssConfig.getEndpoint(), aliyunOssConfig.getAccessKeyId(), aliyunOssConfig.getAccessKeySecret());
// 原始文件名称
// String originalFilename = file.getOriginalFilename();
// 编码文件名
String filePathName = FileUploadUtils.extractFilename(file);
// 文件路径名称
filePathName = aliyunOssConfig.getFilehost() + "/" + filePathName;
try {
ossClient.putObject(aliyunOssConfig.getBucketName(), filePathName, file.getInputStream());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
return aliyunOssConfig.getUrl() + "/" + filePathName;
}
}
第四步:替换原有上传逻辑
修改文件:ruoyi-admin -> src > main > java > com.ruoyi.web > controller > common > CommonController.java
/**
* 通用上传请求(单个)
*/
@PostMapping("/upload")
public AjaxResult uploadFile(MultipartFile file) throws Exception {
try
{
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称
String url = AliyunOssUploadUtils.uploadFile(file);
AjaxResult ajax = AjaxResult.success();
ajax.put("url", url);
ajax.put("fileName", FileUtils.getName(url));
ajax.put("originalFilename", file.getOriginalFilename());
return ajax;
}
catch (Exception e)
{
return AjaxResult.error(e.getMessage());
}
}
完成上述步骤后即可在前台实现把文件上传到OSS中去