如何在 C# 中使用 Azure.Storage.Blobs 以 ByteArray 格式从 Azure 存储 blob 获取文件

How to get file from Azure storage blob in a ByteArray format using Azure.Storage.Blobs in C#

我需要使用新包 Azure.Storage.Blobs 以字节数组格式从 Azure 存储中获取文件。我找不到在 C# 中执行此操作的方法。

public byte[] GetFileFromAzure()
{
byte[] filebytes; 
    BlobServiceClient blobServiceClient = new BlobServiceClient( "TestClient");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("TestContainer");
BlobClient blobClient = containerClient.GetBlobClient("te.xlsx");
    if (blobClient.ExistsAsync().Result)
{
    var response = blobClient.DownloadAsync().Result;
    using (var streamReader = new StreamReader(response.Value.Content))
    {
        var line = streamReader.ReadToEnd();
        //No idea how to convert this to ByteArray
    }
}
return filebytes;
}

知道如何实现获取存储在 Azure blob 存储上的文件字节数组吗?

感谢帮助。

尝试以下操作将您的 Blob 作为流读取,然后将该流转换为 return 上的字节数组:

public byte[] GetFileFromAzure()
{
    BlobServiceClient blobServiceClient = new BlobServiceClient( "TestClient");
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("TestContainer");
    BlobClient blobClient = containerClient.GetBlobClient("te.xlsx");
    
    if (blobClient.ExistsAsync().Result)
    {
        using (var ms = new MemoryStream())
        {
            blobClient.DownloadTo(ms);
            return ms.ToArray();
        }
    }   
    return new byte[];  // returns empty array
}