C# 阿里云视频点播

视频点播dll,分享地址:
链接:https://pan.baidu.com/s/1TM4xMmLmv61FbPRy7zNzNg 
提取码:uv56

视频点播官方文档地址:

https://help.aliyun.com/document_detail/72085.html?spm=a2c4g.11186623.6.961.418958fcKLHJgt

https://help.aliyun.com/document_detail/60574.html?spm=a2c4g.11186623.6.700.7c5149234IQExZ

第一步:角色授权

临时授权方法:https://help.aliyun.com/document_detail/57114.html

①:新增角色

一定要新增角色,单独授权。不能用默认角色授权

 

②:添加授权

二:添加存储地址

三:请求上传地址和凭证、请求STS

①:获取视频上传地址和凭证

        /// <summary>
        /// 获取视频上传地址和凭证
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResponseModel<UploadVideoResponseModel> GetAuth(RequestModel<GetAuthRequestModel> request)
        {
            var response = new ResponseModel<UploadVideoResponseModel>(request.Callback, request.PostTime);
            
            // 构造请求
            CreateUploadVideoRequest req = new CreateUploadVideoRequest();
            if (!string.IsNullOrWhiteSpace(request.PostContent.Title))
                req.Title = request.PostContent.Title;
            else
                req.Title = Guid.NewGuid().ToString();

            req.FileName = request.PostContent.FileName;

            // 初始化客户端
            DefaultAcsClient client = InitVodClient(VodStaticConfigInfo.Instance.VodAccessKeyId, VodStaticConfigInfo.Instance.VodAccessKeySecret);
            // 发起请求,并得到响应结果
            CreateUploadVideoResponse video = client.GetAcsResponse(req);
            UploadVideoResponseModel info = new UploadVideoResponseModel {
                RequestID = video.RequestId, UploadAddress = video.UploadAddress, UploadAuth = video.UploadAuth, VideoId = video.VideoId
            };
            response.PostContent = info;
            return response;
        }

初始化点播服务接入区域

https://help.aliyun.com/document_detail/72086.html?spm=a2c4g.11186623.6.962.10041552f38BwL

②:STS临时授权

        /// <summary>
        /// 获取临时授权
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        public ResponseModel<VodSimpleStsModel> GetVodSts(RequestModel request)
        {
            var response = new ResponseModel<VodSimpleStsModel>(request.Callback, request.PostTime);
            IClientProfile profile = DefaultProfile.GetProfile(VodStaticConfigInfo.Instance.VodRegion, VodStaticConfigInfo.Instance.VodAccessKeyId, VodStaticConfigInfo.Instance.VodAccessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            // 构造AssumeRole请求
            AssumeRoleRequest arRequest = new AssumeRoleRequest
            {
                RoleArn = VodStaticConfigInfo.Instance.RoleArn, // 指定角色Arn
                RoleSessionName = VodStaticConfigInfo.Instance.RoleSessionName,
            };
            // 可以设置Token有效期,可选参数,默认3600秒;
            //arRequest.DurationSeconds = VodStaticConfigInfo.Instance.DurationSeconds;
            // 可以设置Token的附加Policy,可以在获取Token时,通过额外设置一个Policy进一步减小Token的权限;
            // request.Policy="<policy-content>"
            AssumeRoleResponse arResponse = client.GetAcsResponse(arRequest);
            ////Token过期时间;服务器返回UTC时间,这里转换成北京时间显示;
            VodSimpleStsModel sts = new VodSimpleStsModel();
            sts.AccessKeyId = arResponse.Credentials.AccessKeyId;
            sts.AccessKeySecret = arResponse.Credentials.AccessKeySecret;
            sts.SecurityToken = arResponse.Credentials.SecurityToken;
            sts.Expiration = DateTime.Parse(arResponse.Credentials.Expiration).ToLocalTime().ToString("yyyy/MM/dd HH:mm:ss");
            sts.Region = VodStaticConfigInfo.Instance.VodRegion;
            response.PostContent = sts;
            return response;
        }

③:获取视频播放地址

        /// <summary>
        /// 根据Vid获取视频播放地址
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResponseModel<List<VideoPayInfoResponseModel>> GetPayUrl(RequestModel<GetPayUrlRequestModel> request)
        {
            var response = new ResponseModel<List<VideoPayInfoResponseModel>>(request.Callback, request.PostTime);
            List<VideoPayInfoResponseModel> infoList = new List<VideoPayInfoResponseModel>();
            VideoPayInfoResponseModel info = null;
            // 构造请求
            GetPlayInfoRequest req = new GetPlayInfoRequest();
            req.VideoId = request.PostContent.Vid;
            req.Formats = request.PostContent.Formats;
            req.OutputType = "oss";
            // 初始化客户端
            DefaultAcsClient client = InitVodClient(VodStaticConfigInfo.Instance.VodAccessKeyId, VodStaticConfigInfo.Instance.VodAccessKeySecret);
            // 发起请求,并得到 response
            GetPlayInfoResponse video = client.GetAcsResponse(req);
            List<GetPlayInfoResponse.GetPlayInfo_PlayInfo> playInfoList = video.PlayInfoList;
            foreach (var playInfo in video.PlayInfoList)
            {
                info = new VideoPayInfoResponseModel {
                    Bitrate = playInfo.Bitrate, CreationTime = playInfo.CreationTime,
                    Definition = playInfo.Definition, Format = playInfo.Format,
                    ModificationTime = playInfo.ModificationTime, PlayURL = playInfo.PlayURL.Substring(0,playInfo.PlayURL.LastIndexOf("?")),
                    StreamType = playInfo.StreamType
                };
                infoList.Add(info);
            }
            response.PostContent = infoList;
            return response;
        }
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

playInfo.PlayURL.Substring(0,playInfo.PlayURL.LastIndexOf("?"))

需要截取‘?’后面点参数,不然视频会有失效期。

posted @ 2020-02-14 15:07  仅此而已。  阅读(503)  评论(0编辑  收藏  举报