async Task <GetUploadUrlResponse> GetFileUploadUrlAsync(String cloudPath) { if (string.IsNullOrEmpty(cloudPath)) { throw new CloudBaseException(CloudBaseExceptionCode.EMPTY_PARAM, "上传的云端文件路径不能为空"); } Dictionary <string, dynamic> param = new Dictionary <string, dynamic>(); param.Add("path", cloudPath); GetUploadUrlResponse res = await this.core.Request.PostAsync <GetUploadUrlResponse>("storage.getUploadMetadata", param); return(res); }
// 上传文件 public async Task <UploadFileResponse> UploadFileAsync(string cloudPath, string filePath) { if (string.IsNullOrEmpty(cloudPath)) { throw new CloudBaseException(CloudBaseExceptionCode.EMPTY_PARAM, "上传的云端文件路径不能为空"); } if (string.IsNullOrEmpty(filePath)) { throw new CloudBaseException(CloudBaseExceptionCode.EMPTY_PARAM, "上传的本地文件路径不能为空"); } GetUploadUrlResponse metadataRes = await this.GetFileUploadUrlAsync(cloudPath); if (!string.IsNullOrEmpty(metadataRes.Code)) { JObject value = new JObject(); value["code"] = metadataRes.Code; value["message"] = metadataRes.Message; return(new UploadFileResponse(value)); } Dictionary <string, string> param = new Dictionary <string, string>(); param.Add("key", cloudPath); param.Add("signature", metadataRes.File.Authorization); param.Add("x-cos-meta-fileid", metadataRes.File.CosFileId); param.Add("success_action_status", "201"); param.Add("x-cos-security-token", metadataRes.File.Token); var res = await this.core.Request.UploadAsync(metadataRes.File.Url, filePath, param); if (res.IsSuccessStatusCode) { var value = new { requestId = metadataRes.RequestId, fileId = metadataRes.File.FileId }; return(new UploadFileResponse(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(value)) as JObject)); } else { var resStr = await res.Content.ReadAsStringAsync(); JObject value = new JObject(); value["code"] = CloudBaseExceptionCode.STORAGE_REQUEST_FAIL; value["message"] = resStr; return(new UploadFileResponse(value)); } }