首页 > 解决方案 > 跟踪 Azure Blob 存储中的上传进度

问题描述

我正在使用 VB.Net 构建到 Microsoft Azure Blob 存储的文件上传功能。有没有办法在不使用 Microsoft 的数据传输库的情况下跟踪数据传输的进度?这是我的代码:

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

标签: vb.netazure-blob-storage

解决方案


如果想知道上传了多少字节,可以使用UploadFromStreamAsyncsdk 中的方法Microsoft.Azure.Storage.Blob。它将StorageProgress在单个操作中处理包含有关请求和响应流的进度数据传输信息的类。

在此处输入图像描述

例如

 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

在此处输入图像描述


推荐阅读