阿里云OSS可免费试用,且正式收费不贵,个人学习使用完全可以接受,如何注册获取阿里云OSS的key等密钥,请自行搜索。以下仅演示Springboot中如何使用OSS,没有在数据库中建表存储,如有需要请自行配置。
注:文件整体结构如下
1.在pom.xml文件中引入阿里云依赖
<!--阿里云oss依赖坐标-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>
2.在yml文件或者properties文件中配置(endpoint需填写正确,后面endpoint判断和url拼接时很重要)
yml:
# 阿里云oss
aliyun:
oss:
endpoint: 你自己阿里云账户的endpoint,例如:https://oss-cn-beijing.aliyuncs.com
accessKeyId: 你自己阿里云账户的accessKeyId
accessKeySecret: 你自己阿里云账户的accessKeySecret
bucketName: 你自己阿里云账户的bucketName
properties:
# 阿里云oss
aliyun.oss.endpoint=你自己阿里云账户的endpoint,例如我这里填oss-cn-beijing.aliyuncs.com才是正确的
aliyun.oss.accessKeyId=你自己阿里云账户的accessKeyId
aliyun.oss.accessKeySecret=你自己阿里云账户的accessKeySecret
aliyun.oss.bucketName=你自己阿里云账户的bucketName
3.在config下新建配置类AliOSSProperties,读取properties文件下的数据
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOSSProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
4.在utils下新建AliOSSUtils类,用于上传文件到oss
@Component
public class AliOSSUtils {
@Autowired//将OSS的配置注入进来,后面需要用到
private AliOSSProperties aliOSSProperties;
/**
* 以下是一个实现上传文件到OSS的方法,并不唯一
*/
public String upload(MultipartFile file) throws IOException {
//获取阿里云OSS参数
String endpoint = aliOSSProperties.getEndpoint();
String accessKeyId = aliOSSProperties.getAccessKeyId();
String accessKeySecret = aliOSSProperties.getAccessKeySecret();
String bucketName = aliOSSProperties.getBucketName();
// 获取上传的文件的输入流
InputStream inputStream = file.getInputStream();
// 避免文件覆盖,需要使用UUID将文件重命名
String originalFilename = file.getOriginalFilename();
if (originalFilename == null) {
throw new IllegalArgumentException("文件名为空");
}
int dotIndex = originalFilename.lastIndexOf(".");
if (dotIndex == -1) {
throw new IllegalArgumentException("文件名不包含扩展名");
}
String fileName = UUID.randomUUID().toString() + originalFilename.substring(dotIndex);
//上传文件到 OSS
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, fileName, inputStream);
// 文件访问路径,可自行更改逻辑,重点是拼接url
String[] endpointParts = endpoint.split("\\.");
if (endpointParts.length != 3) {
throw new IllegalArgumentException("无效的 endpoint 格式");
}
//url拼接
String url = "https://" + bucketName + "." + endpointParts[0] + "." + endpointParts[1] + "." + endpointParts[2] + "/" + fileName;
//文件访问路径,若使用的yml,并配置了正确的endpoint,可使用以下方式
//String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
// 关闭ossClient
ossClient.shutdown();
// 把上传到oss的路径返回
return url;
}
}
5.在controller下新建FileUploadController实现接口(ResponseVO、getSuccessResponseVO和getErrorResponseVO是自己封装的,你可直接返回url)
@RestController
public class FileUploadController extends BaseController {
@Autowired
private AliOSSUtils aliOSSUtils;
@PostMapping("/uploadToOss")
public ResponseVO uploadToOss(@RequestParam("file") MultipartFile file) {
try {
if (file == null) {
throw new IllegalArgumentException("文件为空");
}
//调用阿里云OSS工具类进行文件上传
String url = aliOSSUtils.upload(file);
return getSuccessResponseVO(url);
} catch (Exception e) {
e.printStackTrace();
return getErrorResponseVO("文件上传失败:" + e.getMessage());
}
}
}