Obtaining Download Progress
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference
You can call GetObjectRequest.DownloadProgress to register the System.EventHandler callback function to obtain download progress. Sample code is as follows:
// Create an instance of ObsClient.
ObsClient client = new ObsClient("*** Provide your Access Key ***", "*** Provide your Secret Key ***", "https://your-endpoint");
// Download an object.
try
{
GetObjectRequest request = new GetObjectRequest()
{
BucketName = "bucketname",
ObjectKey = "objectname",
};
// Represent the progress by showing how many bytes has been downloaded.
request.ProgressType = ProgressTypeEnum.ByBytes;
// Refresh the download progress each time 1 MB data is downloaded.
request.ProgressInterval = 1024 * 1024;
// Register the download progress callback function.
request.DownloadProgress += delegate(object sender, TransferStatus status){
// Obtain the average download rate.
Console.WriteLine("AverageSpeed: {0}", status.AverageSpeed / 1024 + "KB/S");
// Obtain the download progress in percentage.
Console.WriteLine("TransferPercentage: {0}", status.TransferPercentage);
};
using (GetObjectResponse response = client.GetObject(request))
{
string dest = "savepath";
if (!File.Exists(dest))
{
// Write the data streams into the file.
response.WriteResponseStreamToFile(dest);
}
Console.WriteLine("Get object response: {0}", response.StatusCode);
}
}
catch (ObsException ex)
{
Console.WriteLine("ErrorCode: {0}", ex.ErrorCode);
Console.WriteLine("ErrorMessage: {0}", ex.ErrorMessage);
}
You can obtain the download progress when downloading an object in streaming, partial, asynchronous, or resumable mode.
Last Article: Performing an Asynchronous Download
Next Article: Performing a Conditioned Download
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.