开发者社区> 问答> 正文

.NET-SDK之如何实现上传文件(六)?


上传回调


OSS在上传文件完成的时,可以提供 回调(Callback)给应用服务器。用户只需要在发送给OSS的请求中携带相应的回调参数,即能实现回调。支持上传回调接口有:PutObject、PostObject、CompleteMultipartUpload。您想了解上传回调的更详细信息,请参考 上传回调。下面以 PutObject 为例说明上传回调的用法。

  1. using Aliyun.OSS;
  2. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  3. public static void PutObjectCallback(string bucketName)
  4. {
  5.     const string key = "PutObjectProgress";
  6.     const string callbackUrl = "http://callback.oss-demo.com:23450";
  7.     const string callbackBody = "bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&" +
  8.                                 "my_var1=${x:var1}&my_var2=${x:var2}";
  9.     try
  10.     {
  11.         string responseContent = "";
  12.         var metadata = BuildCallbackMetadata(callbackUrl, callbackBody);
  13.         using (var fs = File.Open(fileToUpload, FileMode.Open))
  14.         {
  15.             var putObjectRequest = new PutObjectRequest(bucketName, key, fs, metadata);
  16.             var result = client.PutObject(putObjectRequest);
  17.             responseContent = GetCallbackResponse(result);
  18.         }
  19.         Console.WriteLine("Put object:{0} succeeded, callback response content:{1}", key, responseContent);
  20.     }
  21.     catch (OssException ex)
  22.     {
  23.         Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  24.             ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  25.     }
  26.     catch (Exception ex)
  27.     {
  28.         Console.WriteLine("Failed with error info: {0}", ex.Message);
  29.     }
  30. }
  31. private static string GetCallbackResponse(PutObjectResult putObjectResult)
  32. {
  33.     string callbackResponse = null;
  34.     using (var stream = putObjectResult.ResponseStream)
  35.     {
  36.         var buffer = new byte[4 * 1024];
  37.         var bytesRead = stream.Read(buffer, 0, buffer.Length);
  38.         callbackResponse = Encoding.Default.GetString(buffer, 0, bytesRead);
  39.     }
  40.     return callbackResponse;
  41. }

说明:
  • PutObject、CompleteMultipartUpload、ResumableUploadObject支持上传回调,完整代码请参考GitHub
  • 上传回调的调试方法和错误排除,请参考上传回调错误及排除


进度条


OSS .Net SDK支持进度条功能,指示上传、下载的进度。下面的代码以 PutObject 为例,说明进度条功能的使用方法。
  1. using Aliyun.OSS;
  2. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  3. public static void PutObjectProgress(string bucketName)
  4. {
  5.     const string key = "PutObjectProgress";
  6.     try
  7.     {
  8.         using (var fs = File.Open(fileToUpload, FileMode.Open))
  9.         {
  10.             var putObjectRequest = new PutObjectRequest(bucketName, key, fs);
  11.             putObjectRequest.StreamTransferProgress += streamProgressCallback;
  12.             client.PutObject(putObjectRequest);
  13.         }
  14.         Console.WriteLine("Put object:{0} succeeded", key);
  15.     }
  16.     catch (OssException ex)
  17.     {
  18.         Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  19.             ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  20.     }
  21.     catch (Exception ex)
  22.     {
  23.         Console.WriteLine("Failed with error info: {0}", ex.Message);
  24.     }
  25. }
  26. private static void streamProgressCallback(object sender, StreamTransferProgressArgs args)
  27. {
  28.     System.Console.WriteLine("ProgressCallback - TotalBytes:{0}, TransferredBytes:{1}",
  29.         args.TotalBytes, args.TransferredBytes);
  30. }

说明: PutObject、AppendObject、UploadPart、ResumableUploadObject、GetObject都支持进度功能,完整代码请参考 GitHub

展开
收起
青衫无名 2017-10-18 15:01:51 1699 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
从 SDK 到编解码:视频直播架构解析 立即下载
跨平台的云服务SDK需要什么 立即下载
一个跨平台的云服务SDK需要什么 立即下载