/// <summary> /// 获取上传加速节点地址 /// </summary> /// <returns>获取上传加速节点地址返回结果的封装类</returns> public GetUploadHostParam getUploadHost() { string url = "http://wanproxy.127.net/lbs?version=1.0&bucketname=" + this.bucket; string responseJson = HttpClientBuilder.HttpGet(url); GetUploadHostParam getUploadHostParam = JsonHelper.ToObject <GetUploadHostParam>(responseJson); //Console.WriteLine(getUploadHostParam.lbs); return(getUploadHostParam); }
/// <summary> /// 上传视频分片的构造函数 /// </summary> /// <param name="initUploadVideoParam">视频上传初始化返回结果的封装类</param> /// <param name="getUploadHostParam">获取上传加速节点地址返回结果的封装类</param> /// <param name="offset">当前分片在整个对象中的起始偏移量</param> /// <param name="context">上传上下文</param> /// <param name="fileStream">上传文件的输出流</param> /// <param name="remainderSize">上传文件剩余大小</param> public UploadUtil(InitUploadVideoParam initUploadVideoParam, GetUploadHostParam getUploadHostParam, long offset, string context, Stream fileStream, long remainderSize) { this.bucket = initUploadVideoParam.ret.bucket; this.uploadHost = getUploadHostParam.upload[0]; this.objectName = initUploadVideoParam.ret.objectName; this.offset = offset; this.context = context; this.fileStream = fileStream; this.remainderSize = remainderSize; this.xNosToken = initUploadVideoParam.ret.xNosToken; }
/// <summary> /// 简单视频上传 /// </summary> /// <param name="filePath">上传文件路径</param> /// <param name="initParamMap">上传文件的相关信息集合</param> /// <returns>上传完成后查询视频ID返回结果的封装类</returns> public QueryVideoIDorWatermarkIDParam uploadVideo(string filePath, IDictionary <String, Object> initParamMap) { if (!FileUtil.doesFileExist(filePath)) { throw new VcloudException(string.Format("{0} does not exist", filePath)); } /*视频上传初始化*/ /*视频上传初始化返回结果的封装类*/ InitUploadVideoParam initUploadVideoParam = initUploadVideo(initParamMap); if (initUploadVideoParam.code != 200) { throw new VcloudException("[VcloudClient]上传初始化失败 return code : " + initUploadVideoParam.code + " return message: " + initUploadVideoParam.msg); } /*获取上传加速节点地址*/ /*获取上传加速节点地址返回结果的封装类*/ GetUploadHostParam getUploadHostParam = getUploadHost(initUploadVideoParam); if (null == getUploadHostParam || null == getUploadHostParam.upload || getUploadHostParam.upload.Count == 0) { throw new VcloudException("[VcloudClient]获取上传加速节点地址失败 return code: " + getUploadHostParam.code + " return message: " + getUploadHostParam.Message); } try { /*分片上传视频*/ /*当前分片在整个对象中的起始偏移量 */ long offset = 0; /*上传上下文*/ string context = null; /*上传文件的输出流 */ FileStream fileStream = null; fileStream = FileUtil.getFileInputStream(filePath); /*上传文件剩余大小*/ long fileLength = FileUtil.getFileLength(filePath); long remainderSize = fileLength; /*分片上传视频*/ while (remainderSize > 0) { UploadVideoFragmentParam uploadVideoParam = uploadVideoFragment(initUploadVideoParam, getUploadHostParam, offset, context, fileStream, remainderSize); if (null == uploadVideoParam.errMsg || "".Equals(uploadVideoParam.errMsg.Trim())) { context = uploadVideoParam.context; offset = uploadVideoParam.offset; remainderSize = fileLength - offset; // Console.WriteLine("uploadVideoParam.errMsg:" + uploadVideoParam.errMsg); } else { throw new VcloudException("upload videoFragment fail. errMsg: " + uploadVideoParam.errMsg + " requestID: " + uploadVideoParam.requestId); } } if (remainderSize == 0) { /* 查询上传视频的vid*/ List <string> objectNamesList = new List <string>(); objectNamesList.Add(initUploadVideoParam.ret.objectName); /*查询上传视屏返回结果的封装类*/ QueryVideoIDorWatermarkIDParam queryVideoIDParam = queryVideoID(objectNamesList); return(queryVideoIDParam); } else { throw new VcloudException("文件未传输完成"); } } catch (Exception e) { throw new VcloudException("[VcloudClient]上传视频失败。 error message: " + e.Message); } }
/// <summary> /// 文件第一次上传 /// </summary> /// <param name="filePath">上传文件路径</param> /// <param name="initParamMap">上传文件的相关信息集合</param> /// <param name="recorder">断点续传时记录上传进度的对象</param> /// <returns>上传完成则返回查询视频ID返回结果的封装类,否则抛出文件未传输完成异常</returns> public QueryVideoIDorWatermarkIDParam uploadVideoFirstWithRecorder(string filePath, IDictionary <String, Object> initParamMap, UploadRecorder recorder) { /*视频上传初始化*/ /*视频上传初始化返回结果的封装类*/ InitUploadVideoParam initUploadVideoParam = initUploadVideo(initParamMap); if (initUploadVideoParam.code != 200) { throw new VcloudException("[VcloudClient]上传初始化失败 return code : " + initUploadVideoParam.code + " return message: " + initUploadVideoParam.msg); } /*获取上传加速节点地址*/ /*获取上传加速节点地址返回结果的封装类*/ GetUploadHostParam getUploadHostParam = getUploadHost(initUploadVideoParam); if (null == getUploadHostParam || null == getUploadHostParam.upload || getUploadHostParam.upload.Count == 0) { throw new VcloudException("[VcloudClient]获取上传加速节点地址失败 return code: " + getUploadHostParam.code + " return message: " + getUploadHostParam.Message); } /*分片上传视频*/ /*当前分片在整个对象中的起始偏移量 */ long offset = 0; /*上传上下文*/ string context = null; /*上传文件的输出流 */ FileStream fileStream = null; //int count = 0; try { fileStream = FileUtil.getFileInputStream(filePath); /*上传文件剩余大小*/ long fileLength = FileUtil.getFileLength(filePath); long remainderSize = fileLength; /*分片上传视频*/ while (remainderSize > 0) { UploadVideoFragmentParam uploadVideoParam = uploadVideoFragment(initUploadVideoParam, getUploadHostParam, offset, context, fileStream, remainderSize); if (null == uploadVideoParam.errMsg || "".Equals(uploadVideoParam.errMsg.Trim())) { context = uploadVideoParam.context; offset = uploadVideoParam.offset; remainderSize = fileLength - offset; string bucket = initUploadVideoParam.ret.bucket; string uploadHost = getUploadHostParam.upload[0]; string objectName = initUploadVideoParam.ret.objectName; string offsetStr = Convert.ToString(offset); // string context = saveInfo[4]; string remainderSizeStr = Convert.ToString(remainderSize); string xNosToken = initUploadVideoParam.ret.xNosToken; string[] saveInfo = new string[] { bucket, uploadHost, objectName, offsetStr, context, remainderSizeStr, xNosToken }; recorder.saveRecorderInfo(saveInfo); /* * count++; * if (count == 1) * { * Console.WriteLine("******************************第一次强制中断************"); * break; * }*/ } else { throw new VcloudException("upload videoFragment fail. errMsg: " + uploadVideoParam.errMsg + " requestID: " + uploadVideoParam.requestId); } } if (remainderSize == 0) { recorder.deleteRecorder(); /* 查询上传视频的vid*/ List <string> objectNamesList = new List <string>(); objectNamesList.Add(initUploadVideoParam.ret.objectName); /*查询上传视屏返回结果的封装类*/ QueryVideoIDorWatermarkIDParam queryVideoIDParam = queryVideoID(objectNamesList); return(queryVideoIDParam); } else { throw new VcloudException("文件未传输完成"); //return null; } } catch (Exception e) { throw new VcloudException(e.Message); } }
/// <summary> /// 上传视频分片 /// </summary> /// <param name="initUploadVideoParam">视频上传初始化返回结果的封装类</param> /// <param name="getUploadHostParam">获取上传加速节点地址返回结果的封装类</param> /// <param name="offset">当前分片在整个对象中的起始偏移量</param> /// <param name="context">上传上下文</param> /// <param name="fileStream">上传文件的输出流</param> /// <param name="remainderSize">上传文件剩余大小</param> /// <returns>分片上传视频返回结果的封装类</returns> public UploadVideoFragmentParam uploadVideoFragment(InitUploadVideoParam initUploadVideoParam, GetUploadHostParam getUploadHostParam, long offset, string context, Stream fileStream, long remainderSize) { UploadUtil util = new UploadUtil(initUploadVideoParam, getUploadHostParam, offset, context, fileStream, remainderSize); return(util.uploadVideoFragment()); }