.net 从Azure存储帐户流式传输

liwlm1x9  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(153)

我有一个.Net Framework 4.7.2 Winforms应用程序,我试图从Azure存储帐户流式传输blob,但在.OpenRead()调用上出现错误“The type initializer for 'PerTypeValues`1'”。我可以正常上传。

Public Function StreamFileFromAzureStorageAcct(ByVal clientLocalFilePath As String, ByVal containerDestName As String, ByRef strReturn As String) As StreamReader

    Dim strReturn As String = String.Empty
    Dim textInStream As Stream = Nothing
    Dim textIn As StreamReader = Nothing
    Dim connectionString As String = Nothing

    Try

        connectionString = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};EndpointSuffix={2}", modConfigSettings.AzureStorageAcctName, modConfigSettings.AzureStorageAcctKey, modConfigSettings.AzureStorageAcctURL)
        Dim Container As BlobContainerClient = New BlobContainerClient(connectionString, containerDestName)

        Dim BlobClient As BlobClient = Container.GetBlobClient(Path.GetFileName(clientLocalFilePath))

        Dim readOptions As BlobOpenReadOptions = New BlobOpenReadOptions(False)
        readOptions.Position = 0

        ''Azure Storage Acct Read call returns a Stream
        textInStream = BlobClient.OpenRead(readOptions)

        '' Pass the Stream to a StreamReader to stay inline with existing code.
        textIn = New StreamReader(textInStream)

        strReturn = "Success"

        Return textIn

    Catch er As Exception
        strReturn = String.Format("Failed to Stream from Storage Account: {0}-File:{1}-connectionString: {2}-{3}", er.Message, Path.GetFileName(clientLocalFilePath), connectionString, er.StackTrace)
    End Try

    Return Nothing
End Function

字符串
无法从存储帐户流式传输:“PerTypeValues”1“的类型初始值设定项引发异常。- at System.SpanHelpers.IsReferenceOrContainsReferencesT at Azure.Storage. Blobs. BlobRestClient. Get_ResponseClassifier200()at Azure.Storage.Blobs.BlobRestClient. GetPropertyRequest(String snapshot,String versionId,Nullable1 timeout,String leaseId,String transitionKey,String transitionKeySha256,StringtransitionAlgorithm,String ifTags,RequestConditions requestConditions,RequestContext context)位于Azure.Storage.Blobs.BlobRestClient.GetProperties(String snapshot,String versionId,Nullable1 timeout,String leaseId,String transitionKey,String transitionKeySha256,String transitionAlgorithm,String ifTags,RequestConditions requestConditions,RequestContext context)位于Azure.Blobs.Specialized.BlobBaseClient.d__124.MoveNext()-从抛出异常的上一个位置开始的堆栈跟踪结束-位于System. System. Blobs. Specialized. Blobs. Services.TaskAwaiter.ThrowForNonSuccess(任务任务)atSystem. token. token服务.任务等待.处理非同步和同步通知(任务任务)atAzure.Blob.Blobs.Specialized.BlobBaseClient.d__98.MoveNext()-从引发异常的上一个位置开始的堆栈跟踪结束- atSystem. System. Blobs. Service.TaskAwaiter.ThrowForNonSuccess(任务任务)位于System. System. Data. DataServices.TaskAwaiter. HandleNoncalling和NoncallinggerNotification(任务任务)atAzure.Storage.Blobs.Specialized.BlobBaseClient.OpenRead(BlobOpenReadOptions选项,CancellationToken cancellationToken)在DBSLOCKObjects.RemFredDataAdmin. StreamFileFromAzured帐户(StringclientLocalFilePath,String containerDestName,String& strReturn)在C:\Users\csale\source\Workspaces\Application Server\FredServer\DBSLOCKObjects\RemFredDataAdmin.vb:行15904

sczxawaw

sczxawaw1#

  • 我对您的代码做了一些更改,并能够从blob文件中读取内容。*
    Azure Manager.vb
Imports Azure.Storage.Blobs
Imports Azure.Storage.Blobs.Models
Imports System.IO

Public Class AzureStorageManager
    Public Function StreamFileFromAzureStorageAcct(ByVal clientLocalPath As String, ByVal containerDestinationName As String, ByRef strReturn As String) As StreamReader
        Dim textInStream As Stream = Nothing
        Dim textIn As StreamReader = Nothing
        Dim connectionString As String = Nothing
        Try
            connectionString = String.Format("<AzureStorageConnectionString>")
            Dim Container As BlobContainerClient = New BlobContainerClient(connectionString, containerDestinationName)
            Dim BlobClient As BlobClient = Container.GetBlobClient(Path.GetFileName(clientLocalPath))
            Dim readOptions As BlobOpenReadOptions = New BlobOpenReadOptions(False)
            readOptions.Position = 0
            textInStream = BlobClient.OpenRead(readOptions)
            textIn = New StreamReader(textInStream)
            strReturn = "Success"
            Return textIn
       Catch er As Exception
         strReturn = String.Format("Failed to Stream from Storage Account: {0}-File:{1}-connectionString: {2}-{3}", er.Message, Path.GetFileName(clientLocalFilePath), connectionString, er.StackTrace)
       End Try
       Return Nothing
    End Function
End Class

字符串

  • Form1.vb*:
Imports System.IO

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim azureManager As New AzureStorageManager()
        Dim filePath As String = "<blobname>" 
        Dim containerName As String = "<containerName>" 
        Dim result As String = "<EmptyString>"
        Dim streamReader As StreamReader = azureManager.StreamFileFromAzureStorageAcct(filePath, containerName, result)
        If streamReader IsNot Nothing Then
            Using streamReader
                Dim content As String = streamReader.ReadToEnd()
                MessageBox.Show("Content read from Azure Storage: " & content)
            End Using
        Else
            MessageBox.Show("Failed to stream file from Azure Storage: " & result)
        End If
    End Sub
End Class

  • 输出 *:


的数据

  • Azure存储 *:

Azure存储中blob的内容如下所示。


相关问题