上传回调
OSS在上传文件完成的时,可以提供
回调(Callback)给应用服务器。用户只需要在发送给OSS的请求中携带相应的回调参数,即能实现回调。支持上传回调接口有:PutObject、PostObject、CompleteMultipartUpload。您想了解上传回调的更详细信息,请参考
上传回调。下面以 PutObject 为例说明上传回调的用法。
- using Aliyun.OSS;
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- public static void PutObjectCallback(string bucketName)
- {
- const string key = "PutObjectProgress";
- const string callbackUrl = "http://callback.oss-demo.com:23450";
- const string callbackBody = "bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&" +
- "my_var1=${x:var1}&my_var2=${x:var2}";
- try
- {
- string responseContent = "";
- var metadata = BuildCallbackMetadata(callbackUrl, callbackBody);
- using (var fs = File.Open(fileToUpload, FileMode.Open))
- {
- var putObjectRequest = new PutObjectRequest(bucketName, key, fs, metadata);
- var result = client.PutObject(putObjectRequest);
- responseContent = GetCallbackResponse(result);
- }
- Console.WriteLine("Put object:{0} succeeded, callback response content:{1}", key, responseContent);
- }
- catch (OssException ex)
- {
- Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
- ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
- }
- catch (Exception ex)
- {
- Console.WriteLine("Failed with error info: {0}", ex.Message);
- }
- }
- private static string GetCallbackResponse(PutObjectResult putObjectResult)
- {
- string callbackResponse = null;
- using (var stream = putObjectResult.ResponseStream)
- {
- var buffer = new byte[4 * 1024];
- var bytesRead = stream.Read(buffer, 0, buffer.Length);
- callbackResponse = Encoding.Default.GetString(buffer, 0, bytesRead);
- }
- return callbackResponse;
- }
说明:
- PutObject、CompleteMultipartUpload、ResumableUploadObject支持上传回调,完整代码请参考GitHub。
- 上传回调的调试方法和错误排除,请参考上传回调错误及排除。
进度条
OSS .Net SDK支持进度条功能,指示上传、下载的进度。下面的代码以 PutObject 为例,说明进度条功能的使用方法。
- using Aliyun.OSS;
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- public static void PutObjectProgress(string bucketName)
- {
- const string key = "PutObjectProgress";
- try
- {
- using (var fs = File.Open(fileToUpload, FileMode.Open))
- {
- var putObjectRequest = new PutObjectRequest(bucketName, key, fs);
- putObjectRequest.StreamTransferProgress += streamProgressCallback;
- client.PutObject(putObjectRequest);
- }
- Console.WriteLine("Put object:{0} succeeded", key);
- }
- catch (OssException ex)
- {
- Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
- ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
- }
- catch (Exception ex)
- {
- Console.WriteLine("Failed with error info: {0}", ex.Message);
- }
- }
- private static void streamProgressCallback(object sender, StreamTransferProgressArgs args)
- {
- System.Console.WriteLine("ProgressCallback - TotalBytes:{0}, TransferredBytes:{1}",
- args.TotalBytes, args.TransferredBytes);
- }
说明: PutObject、AppendObject、UploadPart、ResumableUploadObject、GetObject都支持进度功能,完整代码请参考
GitHub。