vb.net - 跟踪 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
解决方案
如果想知道上传了多少字节,可以使用UploadFromStreamAsync
sdk 中的方法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
推荐阅读
- html - 在列中定位图像
- html - 框在新页面中
- python - 替换数据框中的字符串时 Pandas 中的 SettingWithCopyWarning
- python - 如果没有明确的根包或模块,如何从 pytest 访问方法
- django - m2m_changed 信号多对多直通 - 保存
- python - 是否有一个 Python 库函数,给定一个字符串,将生成任何长度的所有排列,并重复?
- javascript - Angular FirebaseError 缺少或权限不足
- three.js - 单个cubeCamera 只反射非反射材料
- sql-server - 使用参数化查询更新链接服务器上的 varchar 列会导致远程扫描和游标获取
- assembly - fs:0x30 在 Linux 中提供了什么?