public void SingleDownload(SingleDownloadReq downloadReq, DefaultApi apiInstance) { // 调用 osdownload API FileOsdownloadReq osdownloadBody = new FileOsdownloadReq(); osdownloadBody = downloadReq; FileOsdownloadRes osdownloadResult = apiInstance.FileOsdownloadPost(osdownloadBody); Console.WriteLine(osdownloadResult); // 根据服务器返回的对象存储请求,向对象存储下载数据 String filePath = downloadReq.SavePath + "\\" + osdownloadResult.Name; if (!Directory.Exists(downloadReq.SavePath)) { //如果不存在,创建它 Directory.CreateDirectory(downloadReq.SavePath); } List <String> headers = new List <String>(); List <String> authRequestList = osdownloadResult.Authrequest; for (int i = 2; i < authRequestList.Count; ++i) { String header = authRequestList[i]; headers.Add(header); } HttpWebResponse ossResult = ossHttpHelper.SendReqToOSS(authRequestList[0], authRequestList[1], headers, null); //写入数据 using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) { Stream fileContent = ossResult.GetResponseStream(); const int bufferLen = 4096; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = fileContent.Read(buffer, 0, bufferLen)) > 0) { fs.Write(buffer, 0, count); } } }
/// <summary> /// 获取文件下载信息 /// </summary> /// <returns>文件下载信息</returns> private string GetFileDownloadResponse(SingleDownloadReq downloadReq, EASAPIModel model) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"{model.Host}/api/v1/file/osdownloadext"); request.Method = "Post"; request.ContentType = "application/json"; request.Headers.Add("Authorization", $"Bearer {model.TokenId}"); var param = JsonConvert.SerializeObject(downloadReq); var bytes = Encoding.UTF8.GetBytes(param); Stream reqStream = request.GetRequestStream(); reqStream.Write(bytes, 0, bytes.Length); reqStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); int resCode = (int)response.StatusCode; // 若为错误返回码则抛出异常 if (resCode < 200 || resCode >= 300) { throw new Exception(); } Stream ResStream = response.GetResponseStream(); string resBody = string.Empty; using (StreamReader reader = new StreamReader(ResStream, Encoding.UTF8)) { resBody = reader.ReadToEnd(); } return(resBody); } catch (Exception ex) { throw ex; } }
/// <summary> /// 获取文件信息(路径) /// </summary> /// <param name="rootPath">文件根目录</param> /// <param name="fileId">文件Id</param> /// <param name="rev">版本</param> /// <returns>文件信息</returns> public string SingleFileDownload(string rootPath, string fileId, string rev, string token) { try { EASAPIModel model = GetAPIModel(token); // 设置传参 SingleDownloadReq body = new SingleDownloadReq(); string savePath = $"{rootPath}\\Files\\{fileId}"; if (!string.IsNullOrEmpty(rev)) { body.Rev = rev; savePath = $"{savePath}/{rev}"; } body.Docid = model.DocId; body.SavePath = savePath; body.Usehttps = false; // 文件信息 string resBody = GetFileDownloadResponse(body, model); var dynamicObj = JsonConvert.DeserializeObject <dynamic>(resBody); string method = dynamicObj["authrequest"][0].Value; string url = dynamicObj["authrequest"][1].Value; string fileName = dynamicObj["name"].Value; string filePath = $"{savePath}\\{fileName}"; //如果不存在,创建它 if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } List <string> headers = new List <string>(); for (int i = 2; i < dynamicObj["authrequest"].Count; ++i) { headers.Add(dynamicObj["authrequest"][i].Value); } OSSHelper ossHttpHelper = new OSSHelper(); HttpWebResponse ossResult = ossHttpHelper.SendReqToOSS(method, url, headers, null); Stream fileContent = ossResult.GetResponseStream(); const int bufferLen = 50 * 1024 * 1024; if (File.Exists(filePath)) { //写入数据 using (FileStream fs = new FileStream(filePath, FileMode.Truncate)) { byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = fileContent.Read(buffer, 0, bufferLen)) > 0) { fs.Write(buffer, 0, count); } fs.Dispose(); } } else { //写入数据 using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) { byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = fileContent.Read(buffer, 0, bufferLen)) > 0) { fs.Write(buffer, 0, count); } fs.Dispose(); } } return(filePath); } catch (Exception e) { throw e; } }