jasonxz
jasonxz

Reputation: 169

How to monitor progress of an asynchronous upload using the Azure BlobServiceClient

This seems like it should be really simple but, for whatever reason, the options just don't make sense to me. I'm instantiating a BlobServiceClient:

this.FileClient = new BlobServiceClient(new Uri(ENDPOINT), credentials);

Then I get an instance of BlobContainerClient:

var container = this.FileClient.GetBlobContainerClient(this.GetContainerName(format));

Then I call UploadBlobAsync:

await container.UploadBlobAsync(filePath, content);

All I want to do is monitor the progress of the upload. If you lookup the BlobClient class, you'll find there's a BlobUploadOptions class that has an ProgressHandler property of type IProgress which is exactly what I'm looking for...but it doesn't seem the BlobContainerClient uses that class in any way (why?). Also, if you look at the UploadAsync methods of the BlobClient class, there are 2 overloads of the method that except a parameter of type BlobUploadOptions....1 allows you to specify where to put the Blob you're uploading and the other allows you to specify the content you're uploading...but there doesn't appear to be an overload that allows you to specify both what you're uploading and, at the same time, where to put it. I'm so confused.

Also...I'm using 1 instance of the BlobServiceClient for the life of the application. Is that proper?

Upvotes: 1

Views: 1189

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30035

First of all, you should use BlobClient to monitor the uploading progress.

For your questions:

but it doesn't seem the BlobContainerClient uses that class in any way (why?)

This is by design. Currently, the BlobContainerClient does not support this parameter. You can raise an feature request in the github page.

Also, if you look at the UploadAsync methods of the BlobClient class, there are 2 overloads of the method that except a parameter of type BlobUploadOptions......

I think you're referring these 2 methods:

UploadAsync(string path, BlobUploadOptions options, CancellationToken cancellationToken = default)

and

UploadAsync(Stream content, BlobUploadOptions options, CancellationToken cancellationToken = default).

You should know when to use them, then you may no confusion.

when using UploadAsync(string path, xxx), you should specify the path(like d:\myfolder\test.txt) for the file you're trying to upload.

when using UploadAsync(Stream content,xxx), it means that you have already got the stream of the file. For example, you use this method var mystream = File.OpenRead("file path") to get the stream of the file content, then pass the stream to this method UploadAsync(Stream content,xxx).

Also...I'm using 1 instance of the BlobServiceClient for the life of the application. Is that proper?

Yes, it's ok.

For the usage of monitoring upload progress by using code, you can refer to this blog for more details.

Please let me know if you still have more issues.

Upvotes: 4

Related Questions