spring boot整合阿里云视频点播上传 删除视频
一、创建视频点播微服务
1、创建微服务模块
Artifact:service-vod
2、pom
(1)service-vod中引入依赖
<dependencies> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> </dependency> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-vod</artifactId> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-sdk-vod-upload</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency> </dependencies>
3、application.properties
# 服务端口 server.port=8003 # 服务名 spring.application.name=service-vod # 环境设置:dev、test、prod spring.profiles.active=dev #阿里云 vod #不同的服务器,地址不同 aliyun.vod.file.keyid=****** aliyun.vod.file.keysecret=******** # 最大上传单个文件大小:默认1M spring.servlet.multipart.max-file-size=1024MB # 最大置总上传的数据大小 :默认10M spring.servlet.multipart.max-request-size=1024MB # nacos服务地址 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
4、启动类
@EnableDiscoveryClient//nacos服务注册 @ComponentScan(basePackages = {"com.atguigu"})//扫描不同模块的配置类 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //不加载数据库相关配置 public class VodApplication { public static void main(String[] args) { SpringApplication.run(VodApplication.class, args); } }
二、整合阿里云vod实现视频上传
1、创建常量类
ConstantPropertiesUtil.java 属性有密钥和id
@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_ID; public static String ACCESS_KEY_SECRET; @Override public void afterPropertiesSet() throws Exception { ACCESS_KEY_ID = keyid; ACCESS_KEY_SECRET = keysecret; } }
2、复制工具类到项目中
public class InitObject { public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException { String regionId = "cn-shanghai"; // 点播服务接入区域 DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); DefaultAcsClient client = new DefaultAcsClient(profile); return client; } }
3 创建service
@Service public class VodServiceImpl implements VodService { @Override public String uploadAlyiVideo(MultipartFile file) { //上传视频到阿里云 String accessKeyId = ConstantVodUtils.ACCESS_KEY_ID; //id String accessKeySecret = ConstantVodUtils.ACCESS_KEY_SECRET; // 密钥 String fileName = file.getOriginalFilename(); //上传文件原始名称 String title = fileName.substring(0, fileName.lastIndexOf(".")); // 上传之后显示的名称 String videoId; try { InputStream inputStream = file.getInputStream(); //上传文件输入流 UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream); UploadVideoImpl uploader = new UploadVideoImpl(); UploadStreamResponse response = uploader.uploadStream(request); if (response.isSuccess()) { videoId = response.getVideoId(); } else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 videoId = response.getVideoId(); } } catch (IOException e) { e.printStackTrace(); return null; } return videoId; } @Override public void removeMoreAlyVideo(List<String> videoIdList) { //删除多个阿里云视频 try { //1 初始化对象 DefaultAcsClient defaultAcsClient = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET); //2 创建删除视频的request对象 DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest(); //3 向request设置视频id //讲videoIdList中的值变为1,2,3的形式 String join = StringUtils.join(videoIdList, ","); deleteVideoRequest.setVideoIds(join); //4 调用初始化对象的方法进行删除 defaultAcsClient.getAcsResponse(deleteVideoRequest); }catch (Exception e){ e.printStackTrace(); } } }
4 创建controller
@RequestMapping("/eduvod/video") @RestController public class VodController { @Autowired VodService vodService; //上传视频到阿里云 @PostMapping("uploadAlyiVideo") public R uploadAlyiVideo(MultipartFile file){ String videoId = vodService.uploadAlyiVideo(file); return R.ok().data("videoId",videoId); } //根据视频id删除阿里云视频 @DeleteMapping("removeAlyVideo/{id}") public R removeAlyVideo(@PathVariable String id){ try { //1 初始化对象 DefaultAcsClient defaultAcsClient = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET); //2 创建删除视频的request对象 DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest(); //3 向request设置视频id deleteVideoRequest.setVideoIds(id); //4 调用初始化对象的方法进行删除 defaultAcsClient.getAcsResponse(deleteVideoRequest); return R.ok(); }catch (Exception e){ e.printStackTrace(); } return R.error(); } //删除多个阿里云视频 @DeleteMapping("delete-bath") public R deleteBath(@RequestParam("videoIdList") List videoIdList){ vodService.removeMoreAlyVideo(videoIdList); return R.ok(); } //根据视频id获取视频凭证 @GetMapping("getPlayAuth/{id}") public R getPlayAuth(@PathVariable String id){ try{ //创建初始化对象 DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET); //创建获取凭证的request对象和response对象 GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest(); //向request设置视频Id request.setVideoId(id); //调用方法 得到凭证 GetVideoPlayAuthResponse response = client.getAcsResponse(request); String playAuth = response.getPlayAuth(); return R.ok().data("playAuth",playAuth); }catch (Exception e){ e.printStackTrace(); } return R.error(); } }