springboot 实现阿里云点播系统使用凭证播放

项目需求用到阿里云点播系统,用到一些高级功能,采用凭证播放。提供接口给前端,前端获取凭证就可直接使用。

public GetVideoPlayAuthResponse getVideoPlayAuth(String mediaId) {
        try {
            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                    .accessKeyId(accessKeyId)
                    .accessKeySecret(accessKeySecret)
                    .build());
            AsyncClient client = AsyncClient.builder()
                    .region("cn-beijing")
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    .setEndpointOverride("vod.cn-beijing.aliyuncs.com")
                                    .setConnectTimeout(Duration.ofSeconds(10))//到期秒
                    )
                    .build();
            GetVideoPlayAuthRequest getVideoPlayAuthRequest = GetVideoPlayAuthRequest.builder()
                    .videoId(mediaId)
                    .build();
            CompletableFuture<GetVideoPlayAuthResponse> response = client.getVideoPlayAuth(getVideoPlayAuthRequest);
            GetVideoPlayAuthResponse resp = response.get();
            return resp;
        }catch (Exception e){
            return null;
        }
    }

然后在出个restapi接口给前端就可。

欢迎点赞、报错、转发。

### 集成阿里云视频点播服务(VOD)于Spring Boot #### 项目配置与初始化 为了使Spring Boot能够顺利集成并操作阿里云的视频点播(VOD)功能,在项目的`application.properties`文件内需设置最大请求尺寸以支持较大体积的文件上传,例如: ```properties spring.servlet.multipart.max-request-size=1024MB ``` 此设定允许单次HTTP请求携带的数据量达到1GB大小,从而满足大多数视频文件上传的需求[^4]。 #### 创建Aliyun VOD客户端实例 通过引入阿里云官方提供的Java SDK来简化同VOD API交互的过程。首先应在Maven工程中的pom.xml加入依赖项;之后利用AccessKey ID/Secret构建DefaultAcsClient对象作为访问入口: ```java IClientProfile profile = DefaultProfile.getProfile( "cn-hangzhou", // 地域ID, 可选其他地域节点 "<your-access-key-id>", // 用户身份验证所需的AK/SK组合之一 "<your-access-key-secret>" ); IAcsClient client = new DefaultAcsClient(profile); // 初始化vod service VodService vodService = new VodServiceImpl(client); ``` 上述代码片段展示了如何基于给定的安全凭证建立连接至阿里云的服务通道,并准备好了执行后续命令所需的基础环境[^1]。 #### 实现视频上传逻辑 定义专门用于处理视频提交动作的方法,内部调用阿里云API完成实际数据传输工作: ```java public String uploadVideo(MultipartFile file){ try { UploadStreamRequest request = new UploadStreamRequest("<bucket-name>"); InputStream inputStream = file.getInputStream(); request.setFileContent(inputStream); request.setTitle(file.getName()); request.setDescription("Description of the video"); UploadStreamResponse response = vodService.uploadStream(request); return response.getVideoId(); // 返回新创建资源唯一标识符 } catch (Exception e) { throw new RuntimeException(e.getMessage(),e); } } ``` 这段程序接收来自前端表单控件传递过来的多媒体流,将其封装进UploadStreamRequest实体类里再发送出去等待服务器响应结果返回。成功后会得到一个独一无二的video id用来追踪该条记录的状态变化情况。 对于经过加密保护的内容,则需要额外申请授权令牌才能正常解密播放[^5]。 #### 构建RESTful接口供外部调用 最后一步就是把之前编写的业务函数暴露成为Web Service的一部分,方便远端客户机发起请求触发对应的操作流程: ```java @RestController @RequestMapping("/api/vods") public class VideoController { @Autowired private IVodService vodService; @PostMapping(value="/upload", consumes="multipart/form-data") public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file){ if (!file.isEmpty()) { String vid = this.vodService.uploadVideo(file); return ResponseEntity.ok().body("{\"status\":\"success\",\"data\":{\"vid\":\""+vid+"\"}}"); } return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } } ``` 以上控制器映射路径下监听POST类型的HTTP事务,解析附带参数里的二进制资料部分交给后台去做持久化保存作业。一旦整个过程顺利完成即刻反馈JSON格式的消息告知对方操作成果详情。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值