/// <summary> /// 上传视频分片 /// </summary> /// <returns>上传视频分片返回结果的封装类</returns> public UploadVideoFragmentParam uploadVideoFragment() { byte[] buffer; String url; if (remainderSize > 0) { /* 判读是否是最后一片 */ if (remainderSize <= uploadDataSize) { url = uploadHost + "/" + bucket + "/" + objectName + "?offset=" + offset + "&complete=" + "true" + "&version=1.0"; /* 如果是最后一片,申请的数组大小要根据实际情况 */ buffer = new byte[remainderSize]; } else { url = uploadHost + "/" + bucket + "/" + objectName + "?offset=" + offset + "&complete=" + "false" + "&version=1.0"; buffer = new byte[(int)uploadDataSize]; } /* 如果不是第一次传输,需要加入此参数 */ if (null != context) { url = url + "&context=" + context; } int len = fileStream.Read(buffer, 0, buffer.Length); string responseJson = HttpClientBuilder.HttpPostVideo(url, buffer, xNosToken); UploadVideoFragmentParam uploadVideoFragmentParam = JsonHelper.ToObject <UploadVideoFragmentParam>(responseJson); return(uploadVideoFragmentParam); } return(null); }
/// <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="filePath">上传文件路径</param> /// <param name="initParamMap">上传文件的相关信息集合</param> /// <param name="recorder">断点续传时记录上传进度的对象</param> /// <returns>上传完成则返回查询视频ID返回结果的封装类,否则抛出文件未传输完成异常</returns> public QueryVideoIDorWatermarkIDParam uploadVideoAgainWithRecorder(string filePath, IDictionary <String, Object> initParamMap, UploadRecorder recorder) { string[] savedInfo = recorder.getRecorderInfo(); string bucket = savedInfo[1]; string uploadHost = savedInfo[2]; string objectName = savedInfo[3]; long offset = long.Parse(savedInfo[4]); string context = savedInfo[5]; long remainderSize = long.Parse(savedInfo[6]); string xNosToken = savedInfo[7]; /*分片上传视频*/ FileStream fileStream = null; try { fileStream = FileUtil.getFileInputStream(filePath); fileStream.Seek(offset, SeekOrigin.Begin); /*上传文件剩余大小*/ long fileLength = FileUtil.getFileLength(filePath); /*分片上传视频*/ while (remainderSize > 0) { UploadVideoFragmentParam uploadVideoParam = uploadVideoFragment(bucket, uploadHost, objectName, offset, context, fileStream, remainderSize, xNosToken); 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); } 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(objectName); /*查询上传视屏返回结果的封装类*/ QueryVideoIDorWatermarkIDParam queryVideoIDParam = queryVideoID(objectNamesList); return(queryVideoIDParam); } else { throw new VcloudException("文件未传输完成"); } } catch (Exception e) { throw new VcloudException(e.Message); } }
/// <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); } }