首页 > 代码库 > 七牛上传视频并转码

七牛上传视频并转码

   /// <summary>
        /// 上传视频到七牛并转码
        /// </summary>
        /// <param name="saveKey"></param>
        /// <param name="localFile"></param>
        public static void UpLoadVideo(string saveKey, string localFile)
        {
            Mac mac = new Mac(ACCESS_KEY, SECRET_KEY);
            PutPolicy putPolicy = new PutPolicy();
            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;
            putPolicy.Scope = bucket;
            // 上传策略有效期(对应于生成的凭证的有效期)          
            putPolicy.SetExpires(3600);
            putPolicy.PersistentPipeline = "UpLoadVideo";
            //进行视屏转码 (转成 mp4 资源和对原资源进行 HLS 切片)
            putPolicy.PersistentOps = "avthumb/mp4;avthumb/m3u8/noDomain/1/segtime/15/vb/440k";
            // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
            //putPolicy.DeleteAfterDays = 1;
            // 生成上传凭证,参见
            // https://developer.qiniu.com/kodo/manual/upload-token            
            //string jstr = putPolicy.ToJsonString();
            string token = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;
            // 上传完毕事件处理
            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted);
            // 方式1:使用UploadManager
            //默认设置 Qiniu.Common.Config.PUT_THRESHOLD = 512*1024;
            //可以适当修改,UploadManager会根据这个阈值自动选择是否使用分片(Resumable)上传    
            UploadManager um = new UploadManager();
            um.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);
            //HttpResult result = um.uploadFile(localFile, saveKey, token);
        }

        public static void UpLoadImg(string saveKey, string localFile)
        {
            Mac mac = new Mac(ACCESS_KEY, SECRET_KEY);
            PutPolicy putPolicy = new PutPolicy();
            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;
            putPolicy.Scope = bucket;
            // 上传策略有效期(对应于生成的凭证的有效期)          
            putPolicy.SetExpires(3600);
            putPolicy.PersistentPipeline = "UpLoadVideo";
            //进行视屏转码 (转成 mp4 资源和对原资源进行 HLS 切片)
            //putPolicy.PersistentOps = "avthumb/mp4;avthumb/m3u8/noDomain/1/segtime/15/vb/440k";
            // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
            //putPolicy.DeleteAfterDays = 1;
            // 生成上传凭证,参见
            // https://developer.qiniu.com/kodo/manual/upload-token            
            //string jstr = putPolicy.ToJsonString();
            string token = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;
            // 上传完毕事件处理
            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted);
            // 方式1:使用UploadManager
            //默认设置 Qiniu.Common.Config.PUT_THRESHOLD = 512*1024;
            //可以适当修改,UploadManager会根据这个阈值自动选择是否使用分片(Resumable)上传    
            UploadManager um = new UploadManager();
            um.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);
            //HttpResult result = um.uploadFile(localFile, saveKey, token);
        }
        private static void OnUploadCompleted(string key, ResponseInfo respInfo, string respJson)
        {
            Logger.LogHelper.WriteInfoLog(respJson);
            // respInfo.StatusCode
            // respJson是返回的json消息,示例: { "key":"FILE","hash":"HASH","fsize":FILE_SIZE }
        }

        /// <summary>
        /// 七牛空间名
        /// </summary>
        public static string qnSpace
        {
            get
            {
                return ConfigurationManager.AppSettings["qnSpace"];
            }
        }
        /// <summary>
        /// 项目名
        /// </summary>
        public static string itemname
        {
            get
            {
                return ConfigurationManager.AppSettings["itemname"];
            }
        }
        /// <summary>
        /// 外链地址
        /// </summary>
        public static string outlink
        {
            get
            {
                return ConfigurationManager.AppSettings["outlink2"];
            }
        }

 关于一些配置方面需要注意的问题参考http://www.cnblogs.com/chongyao/p/6644970.html

七牛上传视频并转码