I am trying to download files from an azure blob storage. my final aim would be add a progress bar to keep track of the progress.
I am already able to upload in this way:
Dim blobClient As BlobClient = containerClient.GetBlobClient(folderName & "/" & blobName)
' Check if the blob already exists
If blobClient.Exists() Then
Debug.WriteLine($"Blob '{blobName}' already exists in container '{containerName}/{folderName}'")
Else
' Get the length of the local file
Dim localFileLength As Long = New FileInfo(localFilePath).Length
' Create progress handler to track upload progress
Dim progressHandler As New Progress(Of Long)(Sub(bytesTransferred)
' Calculate and display the progress as a percentage
Dim progressPercentage As Double = (CDbl(bytesTransferred) / CDbl(localFileLength)) * 100
'MsgBox($"Uploading: {progressPercentage:F2}%")
progressBar.UpdateProgress(progressPercentage)
End Sub)
' Upload the local file to create the blob with progress tracking
Using fs As FileStream = File.OpenRead(localFilePath)
blobClient.Upload(fs, New BlobUploadOptions With {.ProgressHandler = progressHandler, .TransferOptions = New StorageTransferOptions()})
fs.Close()
End Using
I saw that the method DownloadToAsync(Stream, BlobDownloadToOptions, CancellationToken)
should accept BlobDownloadToOptions but when I try this it pops out an error:
Dim destinationPath As String = "C:YourDestinationPathfile.txt"
Using fs As New FileStream(destinationPath, FileMode.Create)
blobClient.DownloadToAsync(fs, New CancellationToken, New
BlobDownloadOptions With {.ProgressHandler = progressHandler})
End Using
the error says that the cancellation token cannot be converted to BlobDownloadOptions. But I`ve put the parameteres in the correct order. can an
3
Answers
I solved the problem this way.
Doesn’t look like you have them in the correct order
source: https://learn.microsoft.com/en-us/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.downloadtoasync?view=azure-dotnet#azure-storage-blobs-specialized-blobbaseclient-downloadtoasync(system-io-stream-azure-storage-blobs-models-blobdownloadtooptions-system-threading-cancellationtoken)
In addition to MStodd answer, you can also check with the below code which worked for me.
You can use the below code to download the files from blob storage with progress percentage bar using
vb.net
.Code:
Output: