第一步
打开阿里云官网
www.aliyun.com
-
注册阿里云账号,然后选择如下
-
进入之后,首次进入是立即开通 只要点进去开通就可有如下页面,然后点击管理控制台进入
-
进入之后
-
在右边有Bucket 管理 点击创建Bucket 相当于一个文件夹
这些都可自行选择 -
创建完成后的页面
-
创建完成后可以看到自己的Bucket
-
创建好了之后 在概览页面中可以查看自己的Access Key
-
首次进入需要创建Access Key 根据自己的情况自行创建即可
-
注意保存好自己的Access Key 后面在项目中需要使用到的
第二步 创建springboot项目
我的项目包创建是这样的 大家可做参考
之后我们导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.15.11</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
<version> 1.4.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>
导入aliyun-sdk-vod-upload这个包的时候需要到java上传sdk
自行下载
下载后解压
之后在通过idea手动导入lib包下的aliyun-sdk-vod-upload jar
然后编写utils
@Component
public class ConstantVodUtils implements InitializingBean {
@Value("${aliyun.vod.file.keyid}")
private String keyid;
@Value("${aliyun.vod.file.keysecret}")
private String keysecret;
public static String ACCESS_KEY_SECRET;
public static String ACCESS_KEY_ID;
@Override
public void afterPropertiesSet() throws Exception {
ACCESS_KEY_ID = keyid;
ACCESS_KEY_SECRET = keysecret;
}
}
service
public interface VodService {
//上传视频到阿里云
String uploadVideoAly(MultipartFile file);
}
@Service
public class VodServiceImpl implements VodService {
@Override
public String uploadVideoAly(MultipartFile file) {
try {
//accessKeyId, accessKeySecret
//fileName:上传文件原始名称
String fileName = file.getOriginalFilename();
//title:上传之后显示名称
String title = fileName.substring(0, fileName.lastIndexOf("."));
//inputStream:上传文件输入流
InputStream inputStream = file.getInputStream();
UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
String videoId = null;
if (response.isSuccess()) {
videoId = response.getVideoId();
} else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
videoId = response.getVideoId();
}
return videoId;
}catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
controller
@RestController
@RequestMapping("/vod/video")
@CrossOrigin
public class VodController {
@Autowired
private VodService vodService;
//上传视频到阿里云
@PostMapping("uploadAlyiVideo")
public String uploadAlyiVideo(MultipartFile file) {
//返回上传视频id
String videoId = vodService.uploadVideoAly(file);
return videoId;
}
}
在static包中创建请求页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="http://127.0.0.1:8003/vod/video/uploadAlyiVideo">
<input type="file" name="file"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
最后配置properties
# 服务端口
server.port=8003
# 服务名
spring.application.name=vod
# 环境设置:dev、test、prod
spring.profiles.active=dev
#阿里云 vod
#不同的服务器,地址不同
aliyun.vod.file.keyid=填写自己保存的Access Key
aliyun.vod.file.keysecret=填写自己保存的keysecret
# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB
运行项目 访问项目http://localhost:8003/即可
效果图
这里是返回了视频的id
之后在阿里云里查看上传的视频
这样子的话就上传成功了
https://blog.csdn.net/weixin_43423864/article/details/110527645
大家如果遇到问题可以相互讨论