【问题标题】:Track upload progress in Azure Blob Storage跟踪 Azure Blob 存储中的上传进度
【发布时间】:2020-04-21 14:42:23
【问题描述】:

我正在使用 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.net azure-blob-storage


    【解决方案1】:

    如果想知道上传了多少字节,可以使用sdkMicrosoft.Azure.Storage.Blob中的UploadFromStreamAsync方法。它将处理 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
    

    【讨论】:

      猜你喜欢
      • 2012-04-15
      • 2014-06-03
      • 1970-01-01
      • 2017-09-12
      • 2021-03-02
      • 2021-03-24
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      相关资源
      最近更新 更多