C# 阿里云视频点播

本文涉及的产品
视频点播 VOD,流量+存储+转码
简介: 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


①:新增角色


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


image.png



②:添加授权


image.png


二:添加存储地址


image.png

image.png



三:请求上传地址和凭证、请求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;
        }



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


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


相关文章
|
C# 对象存储
C#上传阿里云OSS工具类AliOSSTool
C#上传阿里云OSS工具类AliOSSTool
386 0
|
6月前
|
JSON API C#
C# 通过阿里云 API 实现企业工商数据查询
C# 通过阿里云 API 实现企业工商数据查询
|
6月前
|
存储 JSON C#
C# 通过阿里云接口实现人脸属性识别
C# 通过阿里云接口实现人脸属性识别
|
6月前
|
JSON 文字识别 算法
C# 通过阿里云 API 实现企业营业执照OCR识别
C# 通过阿里云 API 实现企业营业执照OCR识别
|
6月前
|
应用服务中间件 API 开发工具
阿里云视频点播功能
阿里云视频点播功能
72 0
|
6月前
|
NoSQL Java 微服务
前后端分离项目知识汇总(阿里云Oss,EasyExcel,视频点播,SpringCloud,Redis,Nuxt)-3
前后端分离项目知识汇总(阿里云Oss,EasyExcel,视频点播,SpringCloud,Redis,Nuxt)
202 1
|
6月前
|
前端开发 NoSQL easyexcel
前后端分离项目知识汇总(阿里云Oss,EasyExcel,视频点播,SpringCloud,Redis,Nuxt)-2
前后端分离项目知识汇总(阿里云Oss,EasyExcel,视频点播,SpringCloud,Redis,Nuxt)
162 0
|
6月前
|
NoSQL 应用服务中间件 对象存储
前后端分离项目知识汇总(阿里云Oss,EasyExcel,视频点播,SpringCloud,Redis,Nuxt)-1
前后端分离项目知识汇总(阿里云Oss,EasyExcel,视频点播,SpringCloud,Redis,Nuxt)
133 0
|
11月前
|
Web App开发 存储 编解码
|
12月前
|
NoSQL Java Nacos
前后端分离项目知识汇总(阿里云Oss,EasyExcel,视频点播,SpringCloud,Redis,Nuxt)-3
前后端分离项目知识汇总(阿里云Oss,EasyExcel,视频点播,SpringCloud,Redis,Nuxt)
213 0