0

我正在使用 VB.NET 和 Azure .NET 程序集将文件上传到 Azure blob。由于文件很大,我将它分成块,并使用CloudBlockBlob.PutBlock. 我遇到的问题是,如果我提供的 aBlockID长度超过一个字符,我会收到“指定的 blob 或块内容无效”。来自 的错误PutBLock。如果 blockID 只有一个字符,则可以正常上传。

Dim iBlockSize As Integer = 1024  ' KB
Dim alBlockIDs As New ArrayList
Dim iBlockID As Integer = 10

' Create the blob client.
Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
' Retrieve reference to a previously created container.
Dim container As CloudBlobContainer = blobClient.GetContainerReference("mycontainer")
' Retrieve reference to a blob named "myblob".
Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference("myblob")

Dim buffer(iBufferSize) As Byte
fs.Read(buffer, 0, buffer.Length)
Using ms As New MemoryStream(buffer)
    ' convert block id to Base64 Encoded string 
    Dim b64BlockID As String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(iBlockID.ToString(Globalization.CultureInfo.InvariantCulture)))
    ' write the blob block
    blockBlob.PutBlock(b64BlockID, ms, Nothing)

    alBlockIDs.Add(b64BlockID)
    iBlockID += 1
End Using

如果iBlockID = 1,该PutBlock方法可以正常工作(我仍然需要解决每个块的blockID 长度相同,我稍后会担心)。有什么想法吗?我目前正在使用本地 Azure 存储模拟器进行测试。

4

1 回答 1

1

无法确认,但您的代码中可能有机会在创建这些缓冲区时重叠一些内存。请参考以下工作代码以块中上传 Blob。以下代码使用10240 (10KB)的块大小,但可以轻松更改

Dim fileName As String = "D:\Untitled.png"
Dim fileSize As Long = New FileInfo(fileName).Length
Dim blockNumber As UInteger = 0
Dim blocksize As UInteger = 10240 '10KB
Dim blockIdList As List(Of String) = New List(Of String)

Using fs As New FileStream(fileName, FileMode.Open, FileAccess.Read)

    Do While (fileSize > 0)
        Dim bufferSize As UInteger

        'Last block of file can be less than 10240 bytes
        If (fileSize > blocksize) Then
            bufferSize = blocksize
        Else
            bufferSize = fileSize
        End If

        Dim buffer(bufferSize) As Byte
        Dim br As New BinaryReader(fs)

        'move the file system reader to the proper position
        fs.Seek(blocksize * blockNumber, SeekOrigin.Begin)
        br.Read(buffer, 0, bufferSize)

        Using ms As New MemoryStream(buffer, 0, bufferSize)
            'convert block id to Base64 Encoded string 
            Dim b64BlockID As String = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("BlockId{0}", blockNumber.ToString("0000000"))))
            blockBlob.PutBlock(b64BlockID, ms, Nothing)

            blockIdList.Add(b64BlockID)
        End Using

        blockNumber += 1
        fileSize -= blocksize
    Loop

    blockBlob.PutBlockList(blockIdList)

End Using
于 2012-12-19T10:37:48.890 回答