skip to Main Content

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


  1. Chosen as BEST ANSWER

    I solved the problem this way.

    Using fs As New FileStream(destinationPath, FileMode.Create)
    
                    Dim downloadOptions As New BlobDownloadToOptions() ' Create BlobDownloadToOptions
    
                    ' Customize the options as needed
                    downloadOptions.TransferOptions = New StorageTransferOptions()
                    downloadOptions.ProgressHandler = progressHandler
    
                    blobClient.DownloadToAsync(fs, downloadOptions).Wait() ' Use .Wait() if not in an async context
    
                End Using
    

  2. Doesn’t look like you have them in the correct order

    Dim destinationPath As String = "C:YourDestinationPathfile.txt"
    Using fs As New FileStream(destinationPath, FileMode.Create)
        Dim downloadOptions As New BlobDownloadOptions With {.ProgressHandler = progressHandler}
        blobClient.DownloadToAsync(fs, downloadOptions, New CancellationToken)
    End Using
    

    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)

    Login or Signup to reply.
  3. blobClient.DownloadToAsync(fs, New CancellationToken, New BlobDownloadOptions With {.ProgressHandler = progressHandler})

    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:

    Imports Azure.Storage.Blobs
    Imports Azure.Storage.Blobs.Models
    Imports System.IO
    Imports System.Threading
    
    Module Module1
    
        Sub Main()
    
            Dim connectionString As String = "Your-storageaccount-connection-string"
            Dim containerName As String = "test" 'containerName
            Dim blobName As String = "demo326.wav" 'filename
            Dim blobServiceClient As New BlobServiceClient(connectionString)
            Dim containerClient As BlobContainerClient = blobServiceClient.GetBlobContainerClient(containerName)
            Dim blobClient As BlobClient = containerClient.GetBlobClient(blobName)
            Dim progressHandler As New Progress(Of Long)(Sub(bytesTransferred)
                                                             ' Calculate and display the progress as a percentage
                                                             Dim progressPercentage As Double = (CDbl(bytesTransferred) / CDbl(blobClient.GetProperties().Value.ContentLength)) * 100
                                                             Console.WriteLine($"Downloading: {progressPercentage:F2}%")
                                                         End Sub)
            Dim destinationPath As String = "Your-local-path"
            Using fs As New FileStream(destinationPath, FileMode.Create)
                blobClient.DownloadToAsync(fs, New BlobDownloadToOptions() With {.ProgressHandler = progressHandler}, CancellationToken.None).Wait()
            End Using
    
            Console.WriteLine("Download completed.")
    
        End Sub
    
    End Module
    

    Output:
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search