0
votes

I am building a file upload function to Microsoft Azure Blob Storage using VB.Net. Is there a way to track the progress of data transfer without using the Data Transfer Library of Microsoft? Here's my code:

Public Function isUploaded(ByVal filename As String) As Boolean
    Try



        Dim connectionString As String = "Connection String Here"
        Dim containerName As String = "uploads"


        Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(connectionString)
        Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
        Dim container As CloudBlobContainer = blobClient.GetContainerReference(containerName)
        Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(Path.GetFileName(filename).ToString)


        Using FileStream = System.IO.File.OpenRead(filename)
            blockBlob.UploadFromStream(FileStream)
            Return True
        End Using


    Catch ex As Exception
        Return False
        MsgBox(ex.Message)
    End Try
End Function
1

1 Answers

1
votes

If you want to know how many bytes have been uploaded, you can use the method UploadFromStreamAsync in the sdk Microsoft.Azure.Storage.Blob. It will process the class StorageProgress which holds information about the progress data transfers for both request and response streams in a single operation.

enter image description here

For example

 Sub Main()
        Dim fileName As String = "D:\\help.txt"
        Dim result = isUploaded(fileName).Result
        Console.WriteLine(result)
        Console.ReadLine()
    End Sub

    Public Async Function isUploaded(ByVal filename As String) As Task(Of Boolean)
        Try
            Dim connectionString As String = ""
            Dim containerName As String = "test"
            Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(connectionString)
            Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
            Dim container As CloudBlobContainer = blobClient.GetContainerReference(containerName)
            Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(Path.GetFileName(filename).ToString)
// Define the function how to handle the infromation
            Dim handelr As Action(Of StorageProgress) = Sub(progress) Console.WriteLine("Progress: {0} bytes transferred", progress.BytesTransferred)
            Dim progressHandler As IProgress(Of StorageProgress) = New Progress(Of StorageProgress)(handelr)
            Dim cancellationToken As CancellationToken = New CancellationToken()

            Using FileStream = File.OpenRead(filename)
                Await blockBlob.UploadFromStreamAsync(FileStream,
                                                       New AccessCondition(),
                                                       New BlobRequestOptions(),
                                                       New OperationContext(),
                                                       progressHandler,
                                                       cancellationToken)
                Return True
            End Using


        Catch ex As Exception
            Return False
            MsgBox(ex.Message)
        End Try
    End Function

enter image description here